Perl is a versatile programming language used extensively within the IntelliSurvey software. While basic Perl knowledge is not required to use our software, understanding its syntax and features unlocks additional capabilities for customizing and enhancing surveys.
16A. How satisfied were you with your experience signing up for Peacock on your device?
type: radio
5. Extremely satisfied
4. Very satisfied
3. Somewhat satisfied
2. Not very satisfied
1. Not at all satisfied
16B. Why were you [* lc($Q16A_text) *] with the sign-up experience on this device?
type: textbox
Details
- To include a Perl expression, "wrap" the code in asterisks and brackets
[* *]
as seen in Q16B above. - When IntelliSurvey sees the Perl wrapper, it evaluates the expression as Perl. This is necessary for response piping in question texts and text widgets. However, this is not needed when used in a condition,
showif
, orcvalue
, which expect Perl expressions by default. - When programming in IntelliBuilder, avoid using the Perl wrapper in question text, as it will display as
[logic statement]
instead of$Q1_text
. - IntelliSurvey software also supports several Perl packages.
Tip! To learn more about Perl, please see Perl Documentation.
Conditional expressions
if/else
In the below example, we have an "if/else" conditional statement that 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. Employment status
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
You can place an "if/else" statement inline with question text to dynamically adapt content based on a respondent's prior answers.
- If the respondent selected option 4 at Q1, the text will display "being unemployed."
- If they selected option 2, it will display "working for yourself."
- If neither condition is met, it defaults to "having a job."
13. What are the benefits of [* if (anyChecked($Q1,4)) {qq|being unemployed|} elsif (anyChecked($Q1,3)) {qq|working for yourself|} else {qq|having a job|} *]? type: textbox
Note that the wrapper [* *]
is needed because we are using Perl within question text.
if/elsif/else
Use elsif
to connect multiple 'if/else' statements. The logic is processed in the order it is programmed. In the example below, elsif
is used twice to evaluate four conditions. If the first condition (noneChecked($QTOTAL_CAP))
is true, the respondent is assigned '1' (Over total quota). If not, the next condition is checked, and so on. If none are true, the respondent is assigned '4' (Other quota).
OVERS. Over quotas
type: radio
invisible: y
cvalue: <<END
if (noneChecked($QTOTAL_CAP)) {1}
elsif (anyChecked($QSAMPLE) and noneChecked($QSAMPLE_QUOTA)) {2}
elsif (anyChecked($QSUPPLIER) and noneChecked($QSUPPLIER_QUOTA)) {3}
else {4}
END
0. Passed quota checks
1. Over total quota
2. Over supplier quota
3. Over sample type quota
4. Other quota
Pattern matching
Perl and RegularExpressions (RegEx) can be leveraged to enable sophisticated text searches.
Basic example: Checks if the word "excellent" appears in Q5. This is case sensitive.
condition: $Q5 =~ /excellent/
Case-insensitive match: Checks if the word "excellent" appears in Q5. This is not case sensitive.
condition: $Q5 =~ /excellent/i
Validation for a 6-digit number:
Ensures that the respondent enters exactly six digits.
-
^
matches the beginning of the string. -
\d{6}
matches exactly six digits. -
$
matches the end of the string.
validation: $QID =~ /^\d{6}$/
message: The ID must be 6 digits
Piping
Response piping is used to customize a respondent's survey experience by inserting a previous question's response into a subsequent question or location. This can be achieved using Perl or the get_answer
method.
Formatting piped text
You can use Perl operators to manipulate text strings and pull in a respondent's previous answers with text piping.
Operator | Description | Example |
lc |
Converts text to lowercase. | [* lc($Q1_text) *] |
uc |
Converts text to uppercase. | [* uc($Q1_text) *] |
q |
Corresponds to single quotes. They allow you to use any non-alphanumeric character, or any matching pair of brackets or parentheses. | [* (q|You spent $$Q5_text on your phone|) *] |
qq |
Corresponds to double quotes. They allow you to use any non-alphanumeric character, or any matching pair of brackets or parentheses. | [* (qq|%%TEXT%% in a block|) *] |
Tip! lc
and uc
can also be used within a block. Place the Perl operator outside the parentheses, for example, [* lc(qq|%%TEXT%%|) *]
.
Piping examples
Pluralizing piped text
Using Perl to pipe in a response often looks like this: $Q1_text
. However, if you need to pluralize a piped response or change the case, you should place the code with a Perl wrapper, [* *]
. This protects your code from any text or characters around the variable.
How many [* $Q3_text *]s are there in your area?
The above would render as something like the following:
How many Krogers are there in your area?
Using the 'join' function
Use the Perl join
function to pipe in responses from a multiselect (checkbox) question and include a joining word between selections such as 'and' or HTML tags for formatting.
In the example below, the settext
Q38PREFACE uses HTML to create an ordered list of rows from Q35 where the respondent selected "less".
35. How has your usage of spread, e.g., Country Crock, evolved over the last 3 years across different occasions?
type: radio table
1. Have stopped using altogether
2. Use but have significantly reduced usage
3. Use but have slightly reduced usage
4. Usage has stayed the same
5. Use but have slightly increased usage
6. Use but have significantly increased usage
rows:
1. Spreading
2. Baking
3. Topping (e.g., melting on vegetables, etc.)
4. Frying/Roasting
rowpunchsummary: Q35MORE {if anyChecked($Q35R[id],5,6)} {desc: Q35 occasions using more spread}
rowpunchsummary: Q35LESS {if anyChecked($Q35R[id],1..3)} {desc: Q35 occasions using less spread}
settext: Q38PREFACE
translate: n
text: <p>You said you are using <b>less</b> spread for:</p> <ul><li>[* join('</li><li>', @$Q35LESS_text) *] </li></ul>
Piping the number of checked options
Use the countChecked
function to indicate the number of options selected in a previous question.
6. Which brands do you use to clean your wooden floors?
type: checkbox
1. Bona Hardwood Floor Cleaner
2. Rejuvenate Floor Cleaner
3. Murphy concentrated wood Cleaner
4. Swiffer WetJet Wood Floor Cleaner
5. Lysol Multi-surface Cleaner
new page
You said you use [* countChecked($Q6) *] brand(s).
If you checked two brands. The above code would be displayed as follows:
You said you use 2 brand(s).
Using Perl with checkbox question piping
To reference the options in a multiselect question, or array, you need to reference the option's index position. An array of selected options is a zero-based index, meaning that the first item is denoted as '0', the second item is denoted as '1', and so on. The array only contains the options that were selected in the question.
For example, to pipe in the first selected response, use the following Perl method to access the "0th" list element from the respondent's answers. See below.
setlist: STORELIST
1. Safeway
2. Kroger
3. Trader Joe's
4. Whole Foods
3. At which of these stores have you ever shopped?
type: checkbox
optsfrom: STORELIST
4. How many times have you shopped at [* $Q3_text->[0] *] in the past month?
type: integer
size: 3
range: 0-31
postfix: times
Comments
0 comments
Please sign in to leave a comment.