part 2 - perl - sawtooth software › webinar › sawtooth_software_scripting… · © 2017...

33
Webinar Part 2 - Perl The Lighthouse Studio Scripting Series

Upload: others

Post on 04-Jul-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Part 2 - Perl

The Lighthouse Studio Scripting Series

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar 2

HTML

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar 3

CSS

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar 4

JavaScript

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar 5

jQuery (enhanced JavaScript)

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar 6

Perl

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Sawtooth Script

Scripting language provided by Sawtooth Software

Consists of various functions or commands documented in the help

Code that runs on server, not in your browser like JavaScript

Simple

7

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Examples

Display previous answers[%Q1%]

Build constructed listsADD(List1, 1, 4)

Perform functions, similar to Excel[%Round(Q1/Q2)%]

8

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Sawtooth Script becomes Perl

Sawtooth Script is written in Perl behind the scenes.

Sawtooth Script:[%Q1 + 3%]

is translated to Perl code:[%VALUE(“Q1”) + 3;%]

9

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Sawtooth Script becomes Perl

All Sawtooth Script functions are available in Perl

Make sure to CAPITALIZE Sawtooth Script functions

Question names are in quotes[%VALUE(“Q1”) + 3;%]

10

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Perl

Perl is a powerful programming language

Custom Perl code can be used for additional flexibility in Sawtooth Script

Perl code is executed on the server before it is sent to the browser

Custom Perl (Unverified Perl) is straight Perl code that you enter into Lighthouse. You are on your own. We do not check it for errors.

11

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

DEMO

12

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Perl Basics

Variables “if” statements “for” loops Functions Etc.

13

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Where can you use Perl?

Perl can be used anywhere Sawtooth Script can be used: Question Text Constructed Lists Skip Logic Quota Logic

14

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Begin Unverified Perl

if(VALUE("Q1") == 5) {

AIC("Q3"); }

End Unverified

Perl Code

15

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my $days = 12;my $total_seconds = 0;

$total_seconds = $days * 24 * 60 * 60;

return $total_seconds;

Perl Variables

Variables can hold single values such as a number or text

Must always begin with a “$” sign

All variables in Perl must be declared using the word "my"

16

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my $base_name = “Quest”;

#Building question names on the fly can be useful.return “Result is: ” . VALUE($base_name . “5”);

Perl Strings

Variables can store text

Text or strings need to have “” surrounding them

To join two strings together use a period

17

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my $base_name = “Quest”;

#Building question names on the fly can be useful.return “Result is: ” . VALUE($base_name . “5”);

Perl Comments

Comments allow you to document your code

Comments are ignored by the computer

Comments start with “#” sign

18

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my @scores = (35, 57, 13, 29, 68); my $total = 0;

$total = $scores[0] + $scores[1] + $scores[2];

35 57 13 29 68

Perl Arrays

Arrays are variables that can hold a collection of items

Arrays start with “@”, and are accessed with $ArrayName[number]

First item is accessed with “0” (0 based)

19

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Perl Hashes

Hashes are variables that can hold a collection of items that consist of key value pairs. Great for quick look up based on a key.

my $found = 0;my %zip_codes = (); %zip_codes = (

“98382” => “Sequim”,“84032” => “Heber”, “02108” => “Boston”

);

return $zip_code{“84032”}; # returns “Heber”

#Checking to see if the zip code entered existsif(exists $zip_codes{VALUE(“QZip”)}){

$found = 1;}

20

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Perl Flow Control

The "if" statement can be used to conditionally execute code Only one AIC command below is executed

if(VALUE("Q1") == 1) {

AIC("Q3"); } elsif(VALUE("Q1") == 2) {

AIC("Q2"); }elsif(VALUE("Q1") == 3){

AIC("Q7");} else {

AIC("Q5"); }

21

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my $q1 = VALUE("Q1");

if($q1 == 1) {

AIC("Q3"); } if($q1 == 2) {

AIC("Q2"); } if($q1 == 3) {

AIC("Q5"); } …

Example

22

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my $q1 = VALUE("Q1");

if($q1 == 1) {

AIC("Q3"); } if($q1 == 2) {

AIC("Q2"); } if($q1 == 3) {

AIC("Q5"); } …

Don’t do this!

23

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my $q1 = VALUE("Q1");

if($q1 == 1) {

AIC("Q3"); } elsif($q1 == 2) {

AIC("Q2"); } elsif($q1 == 3) {

AIC("Q5"); } …

Use elsif

24

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

if (VALUE("Q1") == 3 && (VALUE("Q2") > 4 || VALUE("Q2") < 100)) {

AIC("Q5"); }

&& AND > Greater than.

|| OR < Less than.

== Numeric compare for equal. eq Text compare for equal.

!= Numeric compare for NOT equal.

ne Text compare for NOT equal.

Perl Conditional Logic

25

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my $num_quests = 50;my $total = 0;my $i = 0;

#Initialize, test, incrementfor($i = 1; $i <= $num_quests; $i++){

$total = $total + VALUE(“Q” . $i);}

Perl Looping

The “for” loop allows you to execute code over and over again

26

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

my @scores = (55, 22, 4, 17); my $total_score = 0; my $single_score = 0;

foreach $single_score (@scores) {

$total_score = $total_score + $single_score; }

Looping - foreach

27

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

sub AddNums {

my ($num1, $num2) = @_;

my $sum = $num1 + $num2;

return $sum;}

return AddNums(4, 5);

Perl Functions

Functions are useful for packaging up code so that you can run it over and over again

28

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

[%SetValue(v1, 5)%]

[%GetValue(v1)%]

GETVALUE() – SETVALUE()

In Sawtooth Script you can use: SetValue(QuestionName, value) GetValue(QuestionName) QuestionName can be any variable. Recommend using pass-in variables

for this.

29

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

Perl Debugging

If you get a Script Error check the error_log.cgi file. Available from the Admin Module.

Simplify complex problems. Comment out portions of your code by placing a “#” in front of each line.

Use “return” statements to print out debug values at different spots in the code. Only works for inline script.

30

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

sub DebugMsg{my($msg) = @_;open (DEBUGFILE, ">> ../admin/debug.txt");print DEBUGFILE localtime() . ": " . $msg . "\n";close (DEBUGFILE);

}

//To call itmy $x = 13;DebugMsg($x); //admin folder will have debug.txt in it.

Perl Debugging

To debug in List Building, Skip Logic, or Quotas include this function in your Unverified Perl:

31

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

DEMO

32

© 2017 Sawtooth Software, Inc. | www.sawtoothsoftware.com

Webinar

QUESTIONS?

33

Megan PeitzIngenuity Ambassador

[email protected]

Justin LusterProduct Manager

[email protected]

www.sawtoothsoftware.com+1 801 477 4700@sawtoothsoft