Perl is a programming language that is widely used for developing Web software and for text processing. It is designed to be flexible and practical. The IntelliSurvey software suite uses Perl as its foundation: all conditions, validations, and other statements are evaluated using Perl. Perl expressions are the basis for such IntelliSurvey features as question piping, type checking and validation, pattern matching, and conditional operations where you can change the survey’s flow and display depending on a respondent’s answer.
In general, the more advanced and complex surveys are, the more a basic understanding of Perl commands and syntax is useful. It is not necessary to know Perl in order to work with IntelliSurvey. Without it, it is still possible to create sophisticated and powerful surveys by simply applying the examples. A familiarity with Perl enables survey programmers to do even more, such as write complex expressions complete with programming logic and other operators.
Perl makes simple tasks easy. For simple validation and conditional statements, the expressions look like typical logic statements (for example: $Q1 > 4
). Perl also makes hard tasks possible. Expressions can use the full power of Perl, including loops, branches, and complex data structures.
To include a Perl expression, enclose the code in asterisks and brackets, as shown below.
[* perlexpression *]
When IntelliSurvey sees the brackets and asterisks, it evaluates the expression as Perl.
Formatting expressions
The following Perl formatting operators control how text appears in a survey.
Operator | Description |
---|---|
lc |
Returns a lower case version of the text string. |
uc |
Returns an upper case version of the text string. |
This source text pipes the response from Q1 into Q2, and makes all the text lower case.
Code | Output |
---|---|
1. What is your favorite color? type: radio 1. Red 2. Orange 3. Yellow 4. Green 5. Blue 6. Indigo 7. Violet newpage 2. Why do you like the color [* lc $Q1_text *]? type: textbox |
|
lc
and uc
can also be used to change the casing for a block text sigil:
[* lc(qq|%%TEXT%%|) *]
For more on text piping, see piping.
Conditional expressions
In the below example, an 'if/else' conditional clause is placed in a cvalue
. If the condition is true, option 1 will be punched at Q2. If it is false, option 2 will be punched. Note that the [* *]
brackets are not needed, because cvalue
expects Perl code by default.
1. Which of these best describes how you are employed? type: radio 1. Part Time 2. Full Time 3. Self-Employed 4. Unemployed 2. Working Status type: radio invisible: y cvalue: <<END if (anyChecked($Q1,1,2,3)) {1} else {2} END 1. Working 2. Not Working
In this example, the 'if/else' condition is written inline to insert a conditional string into the question text.
13. What are the main benefits of [* if (anyChecked($Q12,4)) {qq|being unemployed|} elsif (anyChecked($Q12,3)) {qq|working for yourself|} else {qq|having a job|} *]? type: textbox
Tip! In most cases, it is preferable to use set conditioned text
to insert conditional strings instead. The syntax is easier to remember, it can be re-used throughout the survey, and the text that is ultimately shown on the page is recorded in the survey data.
The above example uses elsif
to link multiple 'if/else' statements together. The 'if/elsif/else' structure can be seen more easily below.
if (expression) { return value 1 } elsif (expression) { return value 2 } else { return value 3 }
Pattern matching
The text comparison operators can be useful, but it is also possible to search for patterns in text and not just an exact match using regular expressions (regex). Use the pattern matching operator =~
to facilitate this type of search. In the following example, the condition evaluates as true if the respondent typed the word “excellent” somewhere in an open-end question named Q5.
condition: $Q5 =~ /excellent/
The above returns “true” if Q5 contains the string "excellent" anywhere. It matches “excellent” or “not excellent.” However, it would not match “Excellent”. To make the expression case insensitive, append an 'i'.
condition: $Q5 =~ /excellent/i
To ensure that a respondent types a 6-digit number into a field, use the following expression in a validation.
ID. Please enter your 6-digit ID. type: text size: 6 maxlen: 6 data type: whole validate: $QID =~ /^\d\d\d\d\d\d$/ message: The ID must be 6 digits
The \d
matches any digit. Similarly, \w
matches any word, character, digit, or the underscore character.
There are special characters like this to help handle almost any text matching situation. The ^
above matches the beginning of the string, and the $
matches the end. Therefore, the complete pattern will match if and only the beginning of the string, followed by 6 digits, followed by the end of the string.
Instead of writing \d
six times, curly braces can be used to indicate that an instruction should be repeated. The following expression is the same as the previous example.
validate: $QID =~ /^\d{6}$/ message: The ID must be 6 digits
Learning more about Perl
This introduction to using Perl in IntelliSurvey is not intended to be an exhaustive Perl tutorial. It serves as a brief introduction to the key Perl elements in IntelliSurvey and explains some basic concepts about how IntelliSurvey uses Perl. For more details on the Perl language, consult books, tutorials and other documents that are available either online or at a local bookstore. One good starting point for comprehensive online documentation about Perl is the following Web site: http://perldoc.perl.org/
Comments
0 comments
Please sign in to leave a comment.