A Quick Rexx Overview
By Howard Fosdick (originally published at InformIT)
Rexx is a simple "power" language. Wait, isn't that a contradiction in terms? Not really. While Rexx is easy to learn and simple to use, it nonetheless provides some strong capabilities that make it a widely-used programming language.
This quick overview introduces you to Rexx scripting. Rexx is a platform-independent, standards-based scripting language that runs on a wide variety of systems.
This article introduces “classic” or procedural Rexx. There are also object-oriented Rexx and Java- compatible Rexx implementations. You can learn all about them, and find free tutorials, downloads, tools, and more, at the Rexx Info website.
Rexx is a "power" language, so this tutorial just gets you started. Let's go.
The Rexx language consists of a nucleus of operators – comparison, logical, arithmetic, and string. Rexx features a small set of only a couple dozen instructions, surrounded by a large group of built-in functions:
The language is easily extended by using external functions, available in hundreds of free Rexx function libraries. The Figure above shows the basic parts of the Rexx language. You code external functions in Rexx in the same manner as you do any built-in function (after issuing a statement or two to load and access the external function library).
Rexx variables are strings—character, bit, or hex. Or they may contain a string that represents a number, as shown in this table:
Table 1: Strings that Represent Numbers
|Whole or integer||‘3’ or ‘+6’ or ‘989349829’|
|Decimal or fixed point||‘0.3’ or ‘17.3465’|
|Exponential, real, or floating-point||'1.235E+11’ or ‘171.123E+4'|
Rexx variables are not typed. They contain strings that represent either string or numeric values. If the variable contains a string that can be understood as a number, your script can perform numeric operations with that variable.
A Simple Example
Here’s an easy first example program to show what Rexx looks like:
You can see that Rexx requires absolutely minimal syntax. It’s a free-form language, so you can space between language elements and indent lines however you like. Rexx is also not case sensitive, so you can refer to a variable like end_this_game as End_This_Game or END_THIS_GAME. It doesn’t matter; Rexx treats it as the same variable.
Enclose comments between the delimiters ( /* and */ ). Comments can reside on separate lines or appear on lines that contain executable statements. Comments can even span lines.
Variables don’t have to be initialized, as the first do while statement in the example shows. An uninitialized variable defaults to its name in uppercase, so in that first do while, the variable end_this_game contains the value END_THIS_GAME. The first time through the loop, the script continues because the literal value N is not equal to the string value END_THIS_GAME. Rexx features many such quick and convenient coding shortcuts— though, as when using any "power" language, you can always easily override any of those shortcuts.
In larger Rexx programs, you’ll probably want to initialize all variables (unlike the shortcut I took here). This is fine — Rexx is flexible. Rexx provides a mechanism to identify or trap the situation when you unintentionally refer to an uninitialized variable. This is called the novalue exception or condition. Rexx provides a number of different exception conditions you can trap, as shown in this table:
Table 2: The Exception Conditions Rexx Traps
|Exception Condition or Trap:||Use:|
|novalue||Raised by references to unitialized variables|
|error||Raised when a command issued to an external environment indicates an error upon return|
|failure||Raised when a command issued to an external environment fails|
|halt||Raised by an external interrupt to the script (such as a Ctrl+C)|
|notready||Raised by an unready I/O device|
|syntax||Raised by a syntax or runtime error in the script|
|lostdigits||Raised when an arithmetic operation loses digits|
These exception conditions provide an easy way to divert the script to special routines coded to handle problems. (Rexx has other ways to manage errors too, of course.)
Notice that several of the conditions refer to errors that occur when your script issues commands to external environments (such as the operating system or other programming interfaces). Strong at manipulating strings, Rexx is especially useful for issuing commands to operating systems, and managing their results.
Built-in and External Functions
Let’s get back to the example script. This line generates a random number between 1 and 5 inclusive. It places that value into the variable named the_number:
the_number = random(1,5)
This code uses the built-in function called random to generate the random number. In one of its very few syntax requirements, Rexx requires that you code the left parenthesis immediately after the function name (without any intervening space). If the function has no input arguments, code empty parentheses like this:
the_number = random()
Much of the power of Rexx resides in its 70-odd built-in functions. The functions are categorized into these groups:
- String manipulation (for character, bit, and hex strings)
Many Rexx add-ons and tools come in the form of external function libraries, or packages with additional functions. Since you code external functions with the same style and syntax as built-in functions, this makes it very easy to code and learn language extensions.
Thus Rexx is an extensible language with hundreds of free external functions available.
The example script uses the say and pull instructions to write information to the screen, and to read user input, respectively. As in any power language, these instructions have many variable-manipulation and formatting options, and they manage input/output for any of many different devices. The example script illustrates simple output to the screen and input from the keyboard.
Part of Rexx’s simplicity is that it has only a couple dozen instructions. They include all those necessary for controlling program logic, as shown in this table:
Table 3: Rexx Control Instructions
|call||Invokes a routine or enables an error condition|
|do||All forms of the DO statement (DO...WHILE, DO...UNTIL, DO n times, etc)|
|exit||Ends a program|
|if||All forms of the IF statement (IF...THEN, IF...THEN...ELSE, etc.)|
|iterate||Alters the flow of control by passing control directly to a DO instruction|
|leave||Alters the flow of control by passing control out of a DO loop|
|return||Returns control to the caller|
|select||Selects a branch from a set of alternatives (implements a “case” construct)|
|signal||Transfers control to a label (similar to a “go to”), or enables or disables an error condition|
Rexx offers the full set of programming constructs required for structured programming. But it also includes the common unstructured constructs (such as the GO TO), should you want them.
Other Rexx instructions manage the environment and perform other tasks, as shown in this table:
Table 4: The Other Rexx Instructions
|address||Dictates the external environment to which the script sends commands|
|arg||Reads input arguments, or parses arguments in a template|
|drop||Unassigns one or more variables|
|interpret||Dynamically evaluates instructions provided in an expression|
|numeric||Controls precision and other aspects of numeric calculation|
|options||Passes commands to the Rexx interpreter (controls the interpreter)|
|parse||Assigns values to variables as a result of controlled parsing|
|procedure||Controls the scoping of variables across routines|
|pull||Reads input and applies parsing rules|
|push||Adds a line to the external data queue or stack in last-in, first-out (LIFO) order|
|queue||Adds a line to the external data queue or stack in first-in, first-out (FIFO) order|
|say||Evaluates an expression and writes a line to the default output stream|
|trace||Manages the built-in debugging facility|
Power Through Simplicity
There’s much more to Rexx than I can summarize here, so let’s conclude with one last fascinating feature. Rexx features content addressable memory (sometimes called associative memory).
This simply means that you can create compound variables, variables consisting of names separated by periods, like these:
You can create compound variables with any number of levels or components. This enables you to create any data structure by using them. You can create lists, arrays, tables, balanced or unbalanced trees, key-value pairs, queues, dequeues, or any other data structure. All with this simple, consistent syntax.
This illustrates how Rexx is designed to give you "power through simplicity." Compound variables can be used to create nearly any data structure, yet they require no complex or specialized syntax.----------------------------------