ON THIS PAGE
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
:
var $location = $dot/@location; var $message = "We are in " _ $location _ " now.";
The XSLT equivalent is:
<xsl:variable name="location" select="$dot/@location"/> <xsl:variable name="message" select="concat('We are in ', $location, ' now.')"/>
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:
var $count = 1; match / { call update-count($myparam = $count); } template update-count($myparam) { expr $count _ ", " $myparam _"\n"; if ($myparam != 4) { call update-count($myparam = $myparam + 1) } }
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.
1, 1 1, 2 1, 3 1, 4 1, 5
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
:
mvar $location = $dot/@location; mvar $message = "We are in " _ $location _ " now.";
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>
:
mvar $block; set $block = <block> "start here";
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.
version 1.1; var $list := { <list> { <grocery> { <name> "milk"; <type> "nonfat"; <brand> "any"; <size> "gallon"; } <grocery> { <name> "orange juice"; <type> "no pulp"; <brand> "any"; <size> "half gallon"; } <drugstore>{ <name> "aspirin"; <brand> "any"; <size> "50 tablets"; } } } match / { mvar $mylist; set $mylist = <item> { <name> "coffee"; <size> "1 lb"; } for $item ($list/list/grocery) { append $mylist += <item> { <name> $item/name; <size> $item/size; } } <grocery-short-list> { copy-of $mylist; } }
The output from the script is:
<?xml version="1.0"?> <grocery-short-list> <item> <name>coffee</name> <size>1 lb</size> </item> <item> <name>milk</name> <size>gallon</size> </item> <item> <name>orange juice</name> <size>half gallon</size> </item> </grocery-short-list>