Signiant Support

What is the best way to assign a Signiant prompted value to Perl variables in a solution? Print


Question

When I use a prompted Signiant variable that can accept free-form text from the user (a text box, for example) in a Perl script, the substitution performed by the agent prior to the script's execution can cause Perl to throw syntax errors if the value of the variable contains quote characters.  

What is the best way to assign a Signiant prompted values to Perl variables in a solution?

Answer

To understand the fix, you must first understand the problem.  When a script is executed as part of a template, the agent will first scan the script for special tokens.  These are Signiant variables and keywords (i.e., items with a form of '%SomeString%').  For Signiant variables, the token is replaced by the value of the variable.  This occurs before the script is executed.

Thus, if the value entered is file'name (note the single quote), and you want to assign this value to a Perl variable, the actual code that is executed is:

my $varFoo = 'file'name';

Which will, quite correctly, result in a syntax error at run time.

This is best handled through the use of a Perl "here document", as follows:

#!perl
# Use 'here-document' to assign Signiant
# variable value. Note the leading space
# before the Signaint variable name.
my $variable=<<'EndOfAssignment';
 %SigniantVariable%
EndOfAssignment
# strip the leading space from the variable
$variable =~ s/^\s//;

%SigniantVariable% is a prompted variable, and will be expanded before Perl executes the code. Because it's in a here document, and the tag is itself single quoted (ie, 'EndOfAssignment') the text will not be interpreted in any way (no backslash escapes and no variable substitution). The variable is given the value of the prompt verbatim. The reason this works is because the tag takes the quotes, not the text in the here-document, so there is no opportunity for a user-entered quote character to prematurely terminate a string.

Note that the %SigniantVariable% is itself indented with some white space.  This is to cover the unlikely, yet possible, case where the prompted value is equal to the tag used for the here-document.  The white space can then be trimmed after the value has been assigned to the Perl variable.

Copyright © 2014 Signiant Inc, all rights reserved.