6.5.1 Simple variable use
As elsewhere in MERL, all variables are simply strings; some
operations will allow them to be treated like numbers or collections of lines,
or even collections of objects, and most will also maintain any text formatting,
including Live Code links. (For more on variables as collections of objects, see
“
Iterating over texts as collections
of objects” in
6.3.5.)
MERL supports both global and local variables. Global
variables, denoted with prefix $, have global visibility and scope, and last for
a whole generator run, whereas local variables, denoted by prefix @, are scoped
to a single invocation of a generator they have been defined in. Global and
local variables are in separate namespaces, i.e.
$foo has no relationship to
@foo.
 | There
is also a third, separate category of variables in MetaEdit+: session-persistent
variables. These can only be accessed by command-line string functions
getVar: /
setVar:value: (Section 10.2), callable from MERL with
internal...read/execute
(Section 6.4.8). |
To
assign a simple variable with a fixed string, simple command, chain output
command (e.g. :Name,
explosions), other variable’s
value, or result of a subgenerator call:
$variableName1 = 'variableValue'
$variableName2 = id
@variableName3 = $variableName2
@variableName4 = @variableName5 = @variableName6 = ''
@variableName5 = subgeneratorName()
For cases where the
right hand side would consist of more than one command, a special case of the
last example allows us to use this short syntax: the generator called
“__” simply returns its
argument, which is formed by concatenation:
@variableName5 = __(id ': ' type)
For longer
assignments, it is better (and slightly faster) to use the explicit
variable..write/append..close commands
(Section
6.5.2).
To output the value of a variable, or use it in an
if condition etc., use:
$variableName
@variableName
Local variables are also used to hold the
values of the passed parameters within a subgenerator. They are declared in the
header (Section
6.2.1) with their
@ prefix, and can be used and even
reassigned in the body of the subgenerator. Note that as arguments are passed by
value, this will only affect the local variable in this subgenerator, not the
argument in the calling
generator.