The INTERPRET instruction causes whatever appears after the INTERPRET keyword to be interpreted as some REXX instructions. Think of INTERPRET's argument as a self-contained REXX script embedded inside of your REXX script. INTERPRET is especially useful in conjunction with a variable whose value is one (or more) REXX instructions.

Suppose you have a variable named inst which contains the string "var=var+1" (ie, that's its value). You can execute that string itself as an instruction, by typing:

INTERPRET inst
Therefore, the variable named var is now incremented.

The INTERPRET instruction may be used to execute a Rexx instruction entered by the user, or to assign values to variables whose names are not known in advance.

/* Get the desired name of a variable from
   the user, and set that variable to 42 */
SAY "Enter a variable name >"
PARSE PULL var
INTERPRET var "=" 42
INTERPRET can also be useful if you need to figure out what label to jump to based upon a variable whose value is the name of the desired label. For example, suppose that you have the labels, Label1, Label2, and Label3 in your program. You then want to jump to one of those labels depending upon the value of the variable WhereTo:
/* Get the desired label name from
   the user, and jump there */
SAY "Enter a label name to jump to >"
PARSE PULL name
INTERPRET "SIGNAL" name
You could likewise call a subroutine:
/* Get the desired function name from the user */
SAY "Enter a subroutine name to call, and its args,"
SAY "separated by commas (example: MYSUB, arg1, arg2) >"
PARSE PULL name ',' args
INTERPRET "CALL" name args
Or a function that returns a value:
/* Get the desired function name from the user */
SAY "Enter a function name to call, and its args,"
SAY "separated by commas (example: LENGTH, 'my string') >"
PARSE PULL name ',' args
INTERPRET "MyVariable =" name || '(' || args || ')'

INTERPRET limitations

But INTERPRET is not the same as calling some function/subroutine within your script, or even running an external REXX script. There are a few limitations to it.

One of the biggest limitations is that your string cannot contain any labels. So you can't signal from one place within the string to another place in the string. For example, this is not legal:

INTERPRET 'here: SIGNAL here'
What this also means is that your INTERPRET string can't contain an embedded subroutine or function.

As another limitation, most interpreters will also not allow you to have a RETURN or EXIT instruction within the string.

A final limitation is that most interpreters will not allow you to jump (ie, SIGNAL) to some label outside of the interpret string (ie, to some other place in your script).

These last two limitations mean that you can't abort execution of the interpret string until you get to the last instruction within it.

In conclusion, the INTERPRET string is itself like one, single subroutine that is unable to call any other subroutines and is locked into executing each instruction from beginning to end without being able to SIGNAL, RETURN, or EXIT at any point (unless some condition is raised within the string and you have trapped that condition via SIGNAL ON -- only then is the string aborted and execution jumps to your condition handler).

Note: Reginald allows an INTERPRET string to SIGNAL to some label outside of the string. Reginald also allows a RETURN or EXIT instruction within the string. A RETURN aborts the execution of the interpret string and resumes at the instruction after the INTERPRET statement. An EXIT aborts your script.

Note: If using REXX Dialog, the RxRunRAMScript() function overcomes all of the above limitations of the INTERPRET instruction.