Variable names are never confused for keywords in the langur scripting language, as they are qualified with a dot or underscore, such as .var or _var. Technically, _var is a shortcut for ._var. System variable names begin with an underscore and user-defined variable names do not.

Variable names may use ASCII letters, numbers, and underscores, and may start with a number. Some might find it liberating to use Unicode for identifiers, but I think that would be a headache. A single underscore is not a variable, but the no-op token.

system variables

system variable description
_rev the langur revision number string, such as 1.7.3
_env a hash of environment variables with keys and values as strings
_args an array of strings of the arguments following the script name
_script the name and path of the script being executed

declarations and assignments

Immutable declarations use a val token and mutable declarations use a var token.

You should use immutable declarations for most things.

An immutable declaration must be combined with an assignment.

Mutable declaration without assignment will set the variable implicitly (to something such as null).

val .x = 123 immutable declaration and assignment
var .y = 123 mutable declaration and assignment
var .z mutable declaration without explicit assignment
.y = 789 assignment to mutable variable (previously declared)

assignment expressions

Assignment, with or without declaration, is an expression and is right-associative. This allows you to use assignment within an expression. It also allows you to write a chained assignment, such as the following.

var .x = .y = val .z = 7 # sets all to 7 ... # ... 2 values mutable ... # ... (1 previously declared) ... # ... and 1 immutable

See the operators page about combination operators.

multi-variable assignment/declaration

You can assign multiple items from the right to the same number of items on the left, using parenthesized groups. This can be used with val and var declarations. You can also use parenthesized groups with var with no assignment.

The result of a multi-variable assignment expression is the last value on the right.

# swap values (.x, .y) = (.y, .x)

decoupling assignment/declaration

If you assign a single item from the right to multiple items on the left, it is assumed to be a decoupling assignment. This only works if the value on the right can be indexed numerically.

Use the no-op (underscore) to skip an index.

A decoupling assignment returns a Boolean indicating whether the values could be assigned or not (whether there were enough or not). It will succeed if the number of items on the left is the same or less than the number of items from the right.

# using the fact that submatch() returns an empty array for no match... if var (.alias, .name) = submatch($re/^(\.idregex;)\\s*;\\s*(\.idregex;)/, .row) { # success (2 or more values in array returned from submatch function) # use .alias and .name here ... }

If decoupling assignment fails (without throwing an exception) on a declaration, the declared variables will be null.

variable scope

There are 7 things which may create variable scope.

  1. functions
  2. function calls
  3. generic curly braces { }
  4. for loops
  5. catch blocks (but not the implicit try section)
  6. if expressions
  7. given expressions

Not all use of curly braces indicates a new scope.

Function scope does not allow reassignment of variables from their enclosing scope, so that functions are pure in this way. Other scopes may reassign variables that are mutable.