REXX That's Sensitive To Where It's Called From

Автор: Martin Packer

Дата: 26.05.2013

Источник: IBM developerWorks

I have REXX code that can be called directly by TSO (in DD SYSTSIN data) or else by another REXX function. I want it to behave differently in each case:

  • If called directly from TSO I want it to print something.
  • If called from another function I want it to return some values to the calling function.

So I thought about how to do this. The answer’s quite simple: Use the parse source command and examine the second word returned.

Here’s a simple example, the function myfunc.

/* REXX myfunc */
interpret "x=1;y=2"
parse source . envt .
if envt="COMMAND" then do
  /* Called from top level command */
    say x y
end
else do
  /* Called from procedure or function */
  return x y
end

The interpret command is a fancy way of assigning two variables (and really a leftover from another test). It works but you would normally code

x=1 y=2

The parse source command returns a number of words but it’s the second one that is of interest - and is saved in the variable envt.

The following lines test whether envt has the value “COMMAND” or not. If so the function’s been called directly from TSO. If not it hasn’t. In the one case the variable values are printed. In the other they’re returned to the calling routine.

The calling routine might call have a line similar to

parse value myfunc() with x y

Which would unpack the two variables.

(The original interpret "x=1;y=2" might look silly but interpret "x='1 first';y='2 second'" doesn’t - as a way of passing strings with arbitrary spaces in them back from a routine.)

This is a simplified version of something I want to do in my own code. There might be a few people who will find this useful so why keep it to myself? :-) (It’s probably a standard technique nobody taught me.) :-)