Macrosubstitution
A macro can appear anywhere and can be repeated any number of times in an application program.
Each time the software encounters a call to the macro, it substitutes the current character string value of the macro. Substitution takes place one command at a time, before each command is executed. To view these substitutions as they occur, issue a SET LEXTRACE ON command.
Consider a program called ShowSubstitution that contains the following line:
list #<1> Employees # <2> LsatName = ‘#<3>‘ #
If the commands shown below were used to execute ShowSubstitution
let
ShowSubstitution 2 where Smith
then the LIST command shown above would be interpreted as
list 2 Employees where LastName = ‘Smith’ Sorted by DeptNum
Avoiding Macro Substitution
To use a character literal that could be mistaken for a macro – that is, the character literal contains the number sign and the macro name delimiters (#<1> or #
For example, in the command
output ‘The #
#
output ‘The in box is:’
If the escape character precedes the number sign, the number sign is taken as a literal character. For #<1> and #
output ‘The #
Nested Macros
The character string that a macro represents can contain references to other macros.
The software automatically performs multiple rounds of substitution when macros are nested outside quoted strings. If a nested macro appears inside a quoted string, macro substitution occurs only one.
Example
Consider two macros
let
Note the use of the backslash () to avoid macro substitution in the assignment to
The values of the macros becomes
With this arrangement, the command
#
becomes
out#
which in turn becomes
output ‘Hello’
On the other hand, the command
output ‘#
is processed as
output ‘ out#