Next: , Previous: , Up: Directives   [Contents][Index]


7.3 Eval

Sometimes you will need to compose the name of a variable and get its value. Such a need may arise, for example, when writing some complex loop. This is possible using the $$eval block.

Directive: $$eval

Marks start of the text that should be evaluated after expansion. The end is marked with $$end. The text between $$eval and $$end is read and expanded, and then processed again. It is this second pass that creates output.

To illustrate that, suppose that the following variables are defined:

X2=two
I=2

and the following input is given to xenv:

$$eval
The value is \$X$I.
$$end

This directive is processed in two passes. On the first pass, the text between $$eval and $$end is expanded. This pass produces:

The value is $X2

This text is stored away and expanded second time, which produces the following expansion:

The value is two.

When writing evaluation blocks, care should be taken to escape everything that should be expanded in the second pass. If the block is large, this can be a tedious process.

The following loop will consider variables VAR_0 through VAR_7 and print those of them that are defined:

$$range I 0 7
$$eval
\$\$ifset VAR_$I
Expand VAR_$I = \$VAR_$I;
\$\$endif
$$end
$$end