Scripting in StreamServe > StreamServe scripting language specifics > Variables > Global variables

Global variables
Global variables are used in scripts, function files, and in the Design Center tools. For example, you can add global variables to a StoryTeller page.
The scope is within the execution of the job and the variables are cleared when the job is completed.
You can create field variables in Events. For example, you can assign the global variable $countryCode to the field CountryCode. You can also create global variables in scripts.
Examples
Example 1
if($countryCode = "USA")
{
$overlay = "USA.lxf";
}
 
Example 2
Click to enlarge
 
Example 3
In this example, the global variables $amount1 and $amount2 are created as field variables in an Event. The global variable $total in this script calculates the sum of $amount1 and $amount2.
$total = Num($amount1) + Num($amount2);
Result
If $amount1 = 10 and $amount2 = 12, $total = 22.
 
Example 4
In this example, the global variables $firstName and $surName are created as field variables in an Event. The global variable $fullName in this script concatenates the strings $firstName and $surName.
$fullName = $firstName + " " + $surName;
Result
If $firstName = Mike and $surName = Jones, $fullName = Mike Jones.
 
Example 5
In this example, the global variable $name = “Mr Mike Jones” is used as input to create the global variables $nameSur (should contain “Jones”) and $nameFirst (should contain “Mike”).
The global variable $name is a string variable containing 13 characters. The string and position of the characters are shown in the table below.
The global variable $nameSur is a substring of $name, starting with the character in position 9 (J) and ending with the last character in the string (s):
$nameSur = $var1(9);   //$nameSur contains "Jones".
The global variable $namefirst is a substring of $name, starting with the character in position 4 (M) and is four characters long:
$nameFirst = $var1(4,4);  //$nameFirst contains "Mike".
 
$ prefix
Global variable names must begin with $:
$<variable name>
Valid characters
A global variable name can consist of letters (A-Z, a-z), digits (0-9) and underscore (_). The name must begin with a letter or underscore.
Declaration of global variables
You do not have to declare a global variable before using it. By default, global variables are initiated to an empty string.
Field variables
You can create global variables ($<variable name>) for any field in an Event. For example, if the Event contains the field countryCode, you can create a field variable called $countryCode. When the data is processed, $countryCode will get the current value of the field countryCode. The field variable can be used in scripts as well as in the Design Center formatting tools.
Note that creating field variables affects performance, so you should only create field variables when necessary. If you only need to retrieve the field value in a script, you can use field references instead. See Field references.
Global variables within strings
You can use curly brackets to separate variables from adjacent text.
Example 6
$name = "Tom";
$typeof = "tri";
$result = Eval ("Dear $name, you may use the ${typeof}cycle");
$result will contain "Dear Tom, you may use the tricycle"
If $typeof has the value motor, $result will contain "Dear Tom, you may use the motorcycle"
 
Drawbacks of global variables
The following are issues that you could have when using global variables. To avoid it, one possibility is to use local variables. See Local variables.
Performance
To use a global variable in a numerical expression, or as an argument to a function that expects a numeric argument, you must convert the string type to a numerical value, which affects performance.
Project errors
To re-use a global variable within the scope of a job, you must first assign a value to the variable, or clear it, which are tasks that could introduce errors to your Project.
Function file portability
When you import a function file to your Project, the variables used within the function becomes part of the global scope. This introduces a risk that they interfere with variables used in existing scripts. Thus the portability of function files is limited.
OpenText StreamServe 5.6 Updated: 2013-03-01