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.


Rexx Basics

The Rexx language consists of a nucleus of operatorscomparison, 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 Design of Rexx

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


Number: Examples:
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:

Sample Rexx Program

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:
novalueRaised by references to unitialized variables
errorRaised when a command issued to an external environment indicates an error upon return
failureRaised when a command issued to an external environment fails
haltRaised by an external interrupt to the script (such as a Ctrl+C)
notreadyRaised by an unready I/O device
syntaxRaised by a syntax or runtime error in the script
lostdigitsRaised 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)
  • Numeric
  • Input/output
  • Conversion
  • Environmental
  • Date/time
  • Miscellaneous

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.


Instructions

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


Instruction: Use:
callInvokes a routine or enables an error condition
doAll forms of the DO statement (DO...WHILE, DO...UNTIL, DO n times, etc)
exitEnds a program
ifAll forms of the IF statement (IF...THEN, IF...THEN...ELSE, etc.)
iterateAlters the flow of control by passing control directly to a DO instruction
leaveAlters the flow of control by passing control out of a DO loop
returnReturns control to the caller
selectSelects a branch from a set of alternatives (implements a “case” construct)
signalTransfers 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


Instruction: Use:
addressDictates the external environment to which the script sends commands
argReads input arguments, or parses arguments in a template
dropUnassigns one or more variables
interpretDynamically evaluates instructions provided in an expression
nopNo operation
numericControls precision and other aspects of numeric calculation
optionsPasses commands to the Rexx interpreter (controls the interpreter)
parseAssigns values to variables as a result of controlled parsing
procedureControls the scoping of variables across routines
pullReads input and applies parsing rules
pushAdds a line to the external data queue or stack in last-in, first-out (LIFO) order
queueAdds a line to the external data queue or stack in first-in, first-out (FIFO) order
sayEvaluates an expression and writes a line to the default output stream
traceManages 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:

this.compound.variable
this.compound.variable.1
my_array.1
my_array.2
my_tree.level.one
my_tree.level.two
my_tree.level.two.a
my_tree.level.two.b
my_tree.level.three

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.

----------------------------------

For more information on Rexx, including free downloads, tools, tutorials, and reference material visit the RexxInfo web site. There you can download the Rexx Programmers Reference for free.