Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

SLAX Variables Overview

SLAX variables can store any values that you can calculate or statically define. This includes data structures, XML hierarchies, and combinations of text and parameters. For example, you could assign the XML output of an operational mode command to a variable and then access the hierarchy within the variable.

You can define both local and global variables. Variables are global if they are defined outside of any template. Otherwise, they are local. The value of a global variable is accessible anywhere in the script. The scope of a local variable is limited to the template or code block in which it is defined.

Version 1.0 of the SLAX language supports immutable variables, which are declared using the var statement. Version 1.1 of the SLAX language, which is supported starting with Junos OS Release 12.2, introduces mutable variables, which are declared using the mvar statement. Mutable and immutable variables are discussed in the following sections:

Immutable variables

In version 1.0 of the SLAX language, you declare variables using the var statement. Variables declared using the var statement are immutable. You can set the value of an immutable variable only when you declare it, after which point the value is fixed.

In the declaration, the variable name is prefixed with the dollar sign ($), which is unlike the XSLT declaration, where the dollar sign does not prefix the value of the name attribute of the <xsl:variable> element. Once you declare a variable, you can reference it within an XPath expression using the variable name prefixed with a dollar sign ($). You initialize a variable by following the variable name with an equal sign (=) and an expression.

The following example declares and initializes the variable location, which is then used to initialize the variable message:

The XSLT equivalent is:

Variables declared using the var statement are immutable. As such, you can never change the value of the variable after it is initialized in the declaration. Although you cannot directly update the value of the variable, you can mimic the effect by recursively calling a function and passing in the value of the variable as a parameter. For example:

Executing the op script in the CLI produces the following output in the log file. Although the count variable must remain fixed, myparam is updated with each call to the template.

Mutable variables

Version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases, introduces mutable variables. Unlike variables declared using the var statement, the value of a mutable variable can be modified by a script. You can set the initial value of a mutable variable at the time you declare it or at any point in the script.

You declare mutable variables using the mvar statement. In the declaration, the variable name is prefixed with the dollar sign ($). Once you declare a mutable variable, you can reference it within an XPath expression using the variable name prefixed with a dollar sign ($). You initialize the variable by following the variable name with an equal sign (=) and an expression.

The following example declares and initializes the mutable variable location, which is then used to initialize the mutable variable message:

Mutable variables can be initialized or updated after they are declared. To initialize or update the value of a mutable variable, use the set statement. The following example declares the variable, block, and initializes it with the element <block>:

For mutable variables that represent a node set, use the append statement to append a new node to the existing node set. The following example creates the mutable variable $mylist, which is initialized with one <item> element. For each grocery item in the $list variable, the script appends an <item> element to the $mylist node set with the name and size of the item.

The output from the script is: