The XECUTE command makes an entry in the GT.M invocation stack and executes the argument as GT.M code.
The format of the XECUTE command is:
X[ECUTE]:tvexpr expr[:tvexpr][,...]
The optional truth-valued expression immediately following the command is a command postconditional that controls whether or not GT.M executes the command.
The required expression specifies a fragment of GT.M source code. The maximum length of the expression is 8192 bytes.
The optional truth-valued expression immediately following the argument expression specifies the argument postconditional and controls whether GT.M performs an XECUTE with that argument.
An indirection operator and an expression atom evaluating to a list of one or more XECUTE arguments form a legal argument for an XECUTE.
Run-time errors from indirection or XECUTEs maintain $STATUS and $ZSTATUS related information and cause normal error handling but do not provide compiler supplied information on the location of any error within the code fragment.
An explicit or implicit QUIT within the scope of the XECUTE, but not within the scope of any closer DO, FOR, XECUTE or extrinsic, returns execution to the instruction following the calling point. This may be the next XECUTE argument or another command. At the end of the code specified by the XECUTE argument expression, GT.M performs an implicit QUIT.
Because XECUTE causes run-time compilation in GT.M, and because it tends to obscure code, use XECUTE only when other approaches clearly do not meet your particular requirement.
GT.M compiles XECUTE <literal> at compile time when the literal is valid GT.M code that has minimal impact on the M virtual machine. An XECUTE literal containing GOTO, NEW, QUIT, (nested) XECUTE and indirection can't be precompiled because of the interaction of those features with the stack architecture of the M virtual machine. Precompiled XECUTE literals do not show up in $STATCK() as having a separate stack level, but rather "disappear" into the stack level of the original XECUTE. Please observe the following cautions:
ensure you compile with the same GT.M version, $gtm_chset, $gtm_local_collate, $gtm_patnumeric, $gtm_pattern_file and $gtm_pattern_table values (or lack thereof) as those used to run your application.
If the application changes the run time values controlled by those environment variables, use variable operands or indirection, rather than literals for operands with pattern match (?) or sorts-after (]]).
Note that indirection almost always performs better than an XECUTE that can't be precompiled. Note also that adding a QUIT at the end of an XECUTE that does not contain a FOR will leave it for run time compilation.
Example:
GTM>Xecute "Write ""HELLO""" HELLO GTM>
This demonstrates a simple use of Xecute.
Example:
Set x="" For Set x=$Order(^%x(x)) Quit:x="" Xecute x
This $ORDER() loop XECUTEs code out of the first level of the global array ^%x. Note that, in most cases, having the code in a GT.M source file, for example TMPX.m, and using a Do ^TMPX improves efficiency.