Accessing Variable Values [ Command Interpreter Access and Variables Programmer's Guide ] MPE/iX 5.0 Documentation
Command Interpreter Access and Variables Programmer's Guide
Accessing Variable Values
The value of a variable can be accessed by dereferencing the variable by
name. This substitutes the value of the variable for the variable name.
Variables can be dereferenced in three ways: implicitly, explicitly, and
recursively.
Implicit Dereferencing
Implicit dereferencing is simply the substitution of the variable's value
for the variable name. Only certain commands use implicit dereferencing.
Variables used in expressions in the IF, WHILE, SETVAR, and CALC commands
are dereferenced implicitly.
In these commands, any non-numeric or unquoted string in any expression
is assumed to be a variable name. (Exceptions to this are the boolean
values TRUE and FALSE, and other special cases, such as WARN, FATAL, and
SYSTEM.) In the following example, the variable CM_NAME is set implicitly
with the contents of the variable CM_FIRST_NAME, set in a previous
example.
:SETVAR CM_NAME CM_FIRST_NAME
:SHOWVAR CM_NAME
CM_NAME = CAROL
Explicit Dereferencing
Any variable can be dereferenced explicitly by preceding the variable
name with an exclamation point. Explicit dereferencing is available with
every CI command. The variable's value rather than its name is
substituted in the statement or expression. Explicit dereferencing is
often used to include a variable value in an expression.
:SETVAR X "Blue"
:SETVAR Y "!X is the best color."
:SHOWVAR X,Y
X = Blue
Y = Blue is the best color.
In the preceding example, the value of variable X is substituted when the
value of variable Y is set. The word "Blue" is inserted in the
expression where the variable name is dereferenced.
Recursive Dereferencing
Recursive dereferencing is used to set a variable with the dynamic value
of another variable. The existing variable's value is not loaded into
the new variable until the new variable itself is explicitly
dereferenced, guaranteeing that the current value is inserted. Recursive
dereferencing is valid only when explicit dereferencing is being used.
To specify recursive dereferencing, precede the variable name with two or
more exclamation points.
In the following example, the ECHO command demonstrates how a variable is
dereferenced. ECHO requires explicit dereferencing to display the
contents of a variable. The SHOWVAR command displays the variable name
and the value it contains. Using the variables X and Y set in the
previous example, notice how recursive dereferencing provides the most
current value.
:SETVAR Y "!X is the best color."
:SETVAR Z "!!X is best."
:SHOWVAR Y
Y = Blue is the best color.
:SHOWVAR Z
Z = !X is best.
:ECHO !Y
Blue is the best color.
:ECHO !Z
Blue is best.
:SETVAR X "Red"
:SHOWVAR Y
Y = Blue is the best color.
:SHOWVAR Z
Z = !X is best.
:ECHO !Y
Blue is the best color.
:ECHO !Z
Red is best.
Note that X was not dereferenced fully when the variable Z was set
because recursive dereferencing of the variable was specified. The value
of X was substituted only when the ECHO command was executed, requiring
the dereferencing of Z itself.
For the variable Y, however, X was dereferenced when its value was loaded
to Y. The value of Y always remained the same and did not reflect any
change when X was reset to a new value.
The predefined variable HPPATH is the default search path used to locate
UDCs and command files when file names are not fully qualified. By
default, it is composed of the current group, the current account's PUB
group, and the PUB.SYS group. The current group is identified as the
dereferenced value of the variable, HPGROUP. The following example shows
that by using recursive dereferencing, the value of HPGROUP remains
current.
:SHOWVAR HPPATH
HPPATH = !HPGROUP,PUB,PUB.SYS
:SETVAR HPPATH "!!HPGROUP,SCRIPTS.SYS,PUB.SYS"
:SHOWVAR HPPATH
HPPATH = !HPGROUP,SCRIPTS.SYS,PUB.SYS
If the user changes groups, the search path automatically adjusts to the
current location. The variable value is not substituted until the
variable is explicitly dereferenced at command execution.
MPE/iX 5.0 Documentation