JUNOS & SLAX scripting, how to return values from functions and assign to variables

JUNOS commit / op / event scripts are great, but the language they are written - be it XSLT or SLAX is perhaps not the most intuitive on earth.

I intend to write a couple of short entries with tips and tricks for thee who want to start out with scripting. Today we look at handling return values.

Pretty simple, once you know it. To return a value you simply use expr or similar;

/* This will return 1 if input is "true" or 0 otherwise */
template return_bool ($input) {
    if ($input == "true") {1}
    else {0}
}

/* This will return the first and second argument concatenated. */
template concatenate($first, $second) {
    expr $first _ $second;
}

And how do we assign these returned values to a variable?

var $my_var = { call return_bool("true"); }

Observe how the semi-colon is inside the {}.

Wasn't too difficult, once you know it.