Executes specified commands.
Synopsis
XECUTE:pc xecuteargument,...
X:pc xecuteargument,...
where xecuteargument is
expression:pc
Arguments
pc Optional — A postconditional expression.
expression One or more valid Caché ObjectScript commands.
Description
XECUTE executes Caché ObjectScript commands that result from the process of expression evaluation of the specified argument. Each XECUTE argument must evaluate to a string containing Caché ObjectScript commands. The string must not contain a tab character at the beginning or a <Return> at the end. The string must be no longer than a valid Caché ObjectScript program line.
In effect, each XECUTE argument is like a one-line subroutine called by a DO command and terminated when the end of the argument is reached or a QUIT command is encountered. After Caché executes the argument, it returns control to the point immediately after the XECUTE argument.
Each invocation of XECUTE places a new context frame on the call stack for your process. The $STACK special variable contains the current number of context frames on the call stack.
Arguments
pc
An optional postconditional expression. If the postconditional expression is appended to the command, Caché executes the XECUTE command if the postconditional expression is true (evaluates to a non-zero numeric value). Caché does not execute the XECUTE command if the postconditional expression is false (evaluates to zero). If the postconditional expression is appended to an argument, Caché evaluates the argument only if the postconditional expression is true (evaluates to a non-zero numeric value). If the postconditional expression is false, Caché that argument and evaluates the next argument (if one exists) or the next command. For further details, refer to Command Postconditional Expressions in Using Caché ObjectScript.
expression
A single expression or a comma separated list of expressions. Each expression must contain at least one valid Caché ObjectScript command. The expression can evaluate to a null string (""). In this case, Caché performs no action and continues execution with the next XECUTE argument or the next command.
Examples
In this example, the XECUTE command references the local variables x and y. x and y each contain a string literal consisting of three separate Caché ObjectScript commands that XECUTE invokes.
   SET x="SET id=ans QUIT:ans="""" DO Idcheck"
   SET y="SET acct=num QUIT:acct="""" DO Actcheck"
   XECUTE x,y
The following example uses XECUTE with a $SELECT construction.
   XECUTE "SET A=$SELECT(A>100:B,1:D)"
The following example executes the subroutine that is the value of A.
   SET A="WRITE ! FOR I=1:1:5 { WRITE ?I*5,I+1 }"
   XECUTE A
 
2 3 4 5 6
Notes
XECUTE and Objects
You can use XECUTE to call object methods and properties and execute the returned value, as shown in the following examples:
   XECUTE patient.Name
   XECUTE "WRITE patient.Name"
XECUTE and FOR
If an XECUTE argument contains a FOR command, the scope of the FOR is the remainder of the argument. When the outermost FOR in an XECUTE argument is terminated, the XECUTE argument is also terminated.
XECUTE and DO
If an XECUTE command contains a DO command, Caché executes the routine or routines specified in the DO argument or arguments. When it encounters a QUIT, it returns control to the point immediately following the DO argument.
For example, in the following commands, Caché executes the routine ROUT and returns to the point immediately following the DO argument to write the string “DONE”.
   XECUTE "DO ^ROUT WRITE !,""DONE"""
XECUTE and GOTO
If an XECUTE argument contains a GOTO command, Caché transfers control to the point specified in the GOTO argument. When it encounters a QUIT, it does not return to the point immediately following the GOTO argument that caused the transfer. Instead, Caché returns control to the point immediately following the XECUTE argument that contained the GOTO.
In the following example, Caché transfers control to the routine ROUT and returns control to the point immediately following the XECUTE argument to write the string “FINISH”. It never writes the string “DONE”.
   XECUTE "GOTO ^ROUT WRITE !,""DONE""" WRITE !,"FINISH"
XECUTE and QUIT
There is an implied QUIT at the end of each XECUTE argument.
XECUTE with $TEXT
If you include a $TEXT function within an expression, it designates lines of code in the routine that contains the XECUTE. For example, in the following program, the $TEXT function retrieves and executes a line.
A
   SET H="WRITE !!,$PIECE($TEXT(HELP+1),"","",3)"
   XECUTE H
   QUIT
HELP
   ;; ENTER A NUMBER FROM 1 TO 5
Running routine A extracts and writes “ENTER A NUMBER FROM 1 TO 5”.
Nested Invocation of XECUTE
Caché ObjectScript supports the use of XECUTE within an XECUTE argument. However, you should use nested invocation of XECUTE with caution because it can be difficult to determine the exact flow of processing at execution time.
Execution Time for Commands Called by XECUTE
The execution time for code called within XECUTE can be slower than the execution time for the same code encountered in the body of a routine. This is because Caché compiles source code that is specified with the XECUTE command or that is contained in a referenced global variable each time it processes the XECUTE.
Implementing Generalized Operations
A typical use for XECUTE is to implement generalized operations within an application. For example, assume that you want to implement an inline mathematical calculator that would allow the user to perform mathematical operations on any two numbers and/or variables. To make the calculator available from any point in the application, you might use a specific function key (say, F1) to trigger the calculator subroutine.
A simplified version of the code to implement such a calculator might appear as follows.
Start  SET ops=$CHAR(27,21)
  READ !,"Total amount (or F1 for Calculator): ",amt
  IF $ZB=ops { DO Calc 
    ; . . .
  }
Calc  READ !,"Calculator"
  READ !,"Math operation on two numbers and/or variables."
  READ !,"First number or variable name: ",inp1
  READ !,"Mathematical operator (+,,*,/): ",op
  READ !,"Second number or variable name: ",inp2
  SET doit="SET ans="_inp1_op_inp2
  XECUTE doit
  WRITE !,"Answer (ans) is: ",ans
  READ !,"Repeat? (Y or N) ",inp
  IF (inp="Y")!(inp="y") { GOTO Calc+2 }
  QUIT
When executed, the Calc routine accepts the user inputs for the numbers and/or variables and the desired operation and stores them as a string literal defining the appropriate SET command in variable doit. The XECUTE command references doit and executes the command string that it contains. This code sequence can be called from any number of points in the application, with the user supplying different inputs each time. The XECUTE performs the SET command each time, using the supplied inputs.
XECUTE and ZINSERT
You use the XECUTE command to define and insert a single line of executable code from within a routine. You can use the ZINSERT command from the programmer prompt to define and insert by line position a single line of executable code into the current routine. You can use the ZREMOVE command from the programmer prompt to delete by line position one or more lines of executable code from the current routine.
An XECUTE command cannot be used to define a new label. Therefore, XECUTE does not require an initial blank space before the first command in its code line. ZINSERT can be used to define a new label. Therefore, ZINSERT does require an initial blank space (or the name of a new label) before the first command in its command line.
See Also