Chapter 36. Assignments



var = valueassign
var += valueappend (assign if not set yet)
var ?= valueassign only when not set yet
var $= valueassign, evaluate when used
var $+= valueappend, evaluate when used
var $?= valueassign only when not set, evaluate when used

Assignment with "+=" or "$+=" appends the argument as a separate item. This is actually done by inserting a space. But when the variable wasn't set yet and when it is empty it works like a normal assignment:

        VAR += something

is equal to:

        @if globals().get("_no.VAR"):
        @   VAR = _no.VAR + " " + "something"
        @   VAR = "something"

Assignment with "?=" only does the assignment when the variable wasn't set yet. A variable that was set to an empty string also counts as being set. Thus when using "aap VAR=" the empty value overrules the value set with "?=".

        VAR ?= something

is equal to:

        @if not globals().has_key("_no.VAR"):
            VAR = something

When using "$=", "$+=" or "$?=" variables in the argument are not evaluated at the time of assignment, but this is done when the variable is used. The expansion is done in the scope where it is used, thus the result may depend on when and where the variable is used..

        VAR = 1
        TT $= $VAR
        VAR = 2
        :print $TT

prints "2".

A variable with delayed evaluation cannot be used directly in Python code, because it is set the the class ExpandVar. See the var2string() function for expanding the variable in Python code.

When first setting a variable with "$=" and later appending with "+=" the evaluation is done before the new value is appended:

        VAR = 1
        TT $= $VAR
        TT += 2
        VAR = 3
        :print $TT

prints "1 2"

Note that evaluating a python expressions in `` is not postponed.

Block Assignment

The normal assignment command uses a single line of text. When broken into several lines they are joined together, just like with other commands. $BR can be used to insert a line break. Example:

        foo = first line$BR
                second line$BR
                third line $BR

The block assignment keeps the line breaks as they are. The same example but using a block assignment:

        foo << EOF
          first line
          second line
          third line 

The generic format is:

        {var} << {term}

{term} can be any string without white space. The block ends when {term} is found in a line by itself, optionally preceded by white space and followed by white space and a comment.

The amount of indent to be removed from all the lines is set by the first line. When the first line should start with white space use $( ).

All the variations of the assignment command can be used:

var << termassign
var +<< termappend (assign if not set yet)
var ?<< termonly assign when not set yet
var $<< termevaluate when used
var $+<< termappend, evaluate when used
var $?<< termonly when not set, evaluate when used