langur

variables

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 the underscore, and may start with a number. A lone underscore is the no-op token, not a variable name.

declarations and assignments

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

An immutable declaration must be combined with an assignment.

Mutable declaration without assignment will set the variable implicitly to 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)
.y += 789 assignment to mutable variable using a combination operator

multi-variable assignment/declaration

In statement context, you can assign multiple items from the right to the same number of items on the left. This can be used with val and var declarations.

The result of a multi-variable assignment 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 list for no match... if val .alias, .name = submatch(re/(abcd).+(1234)/, .row) { # success (2 or more values in list 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.

expansion on decoupling assignment

You can use expansion on the last variable on decoupling assignment. Upon success, the last variable will be set to an list of 0 or more elements.

if val .x, .y, ... .z = .somelist { /* do something */ } # .z takes all remaining values

variable scope

Langur limits declaration scope to statements and to the tests of if expressions. The following may create variable scope.

  1. script level
  2. functions
  3. generic curly braces { } (scope blocks)
  4. loops
  5. catch blocks (but not the implicit try section)
  6. the optional else section of a catch block
  7. if expressions
  8. switch 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.

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 list of strings of the arguments following the script name
_script the name and path of the script being executed