REXX Workshop - Part 1

Introduction

  • Restructured Extended Executor
  • Developer: Michael Cowlishaw
  • Human-oriented programming language
  • Flexible and feature rich
  • A REXX program is called an exec.
  • REXX was intended to replace the EXEC, EXEC2, and CLIST languages.
  • REXX execs can be interpreted or compiled.
  • REXX is both a scripting language and a procedureal language.

Scripting Example

RENAME a file, issue FILELIST command

                                                                                                                                                 
/* This is a comment; REXX programs start with comments. */                                                                                           
                                                                                                                                                      
Address 'CMS'                   /* Send all non-rexx     */                                                                                           
                                /* instructions to CMS   */                                                                                           
                                /* for processing.       */                                                                                           
                                                                                                                                                      
'RENAME MY FILE A YOUR FILE A'  /* Rename the file       */                                                                                           
'EXEC FILELIST'                 /* Invoke FILELIST EXEC  */                                                                                           
                                                                                                                                                      
Exit                            /* End program           */                                                                                           

Procedureal Example

Receive two numbers from the keyboard and write out the sum.

                                                                                                                                                 
/* Get two numbers from the keyboard, write out the sum. */                                                                                           
                                                                                                                                                      
Say "Enter a number"            /* Prompt the user.      */                                                                                           
Pull n1                         /* Receive input.        */                                                                                           
Say "Enter another number"      /* Prompt again.         */                                                                                           
Pull n2                         /* Receive second input. */                                                                                           
                                                                                                                                                      
sum = n1 + n2                   /* Calculate the sum.    */                                                                                           
                                                                                                                                                      
Say n1 "+" n2 "=" sum           /* Write out the answer. */                                                                                           
                                                                                                                                                      
Exit                            /* End program.          */                                                                                           

Quick History

  • 1979 - Defined/developed
  • 1983 - Introduced to VM/CMS as "System Product Interpreter"
  • 1985 - Personal REXX for DOS (Mansfield Software)
  • 1988 - Introduced for MVS/TSO
  • 1990 - Introduced for OS/2
  • 1991 - Introduced for UNIX (?)
  • 1992 - Introduced for Windows (?)
  • 1996 - NetRexx and Object Rexx Introduced

With the introduction of NetRexx and Object Rexx, the original language is now referred to as Classic Rexx.

REXX is available on virtually every computing platform from mainframes to microcomputers, but it is most commonly associated with IBM mainframe operating systems.


Language Elements

  • Comments
  • Strings
  • Variables
  • Operators
  • Expressions
  • REXX Instructions
  • Assignment Statements
  • Labels
  • Commands

Comments

  • REXX comments start with "/*" and end with "*/" .
  • REXX comments are for human consumption; for the most part, they are ignored.
  • REXX execs in CMSmust start with a comment.

The comments in the example below are highlighted in red.

                                                                                                                                                 
         /* My First REXX Exec */                                                                                        
         Say "What is your name?" /* Prompt for name. */                                                                 
         Pull name                                                                                                                                    
         Say "Hello," name"!"     /* Greet the user.  */                                                                 
         Exit                                                                                                                                         


Exercise: Using Xedit, create a file with the name MYFIRST EXEC A and enter in the preceding REXX program. File the exec. Run the exec by entering its name on the CMS command line.


Strings

  • Strings can be any characters surrounded by quotation marks.
  • Strings can be surrounded by single or double quotation marks.
  • Shorter strings can be concatenated into longer strings by placing the strings together or by using the concatenation operator ("| |").
  • Multiple blanks between strings are reduced to one blank.
  • Strings can be concatenated with variables, in which case the value of the variable is substituted into the string.

The strings in the example below are highlighted in red.

                                                                                                                                                 
         /* My First REXX Exec */                                                                                                                     
         Say 'What is your name?' /* Prompt for name. */                                                                 
         Pull name                                                                                                                                    
         Say "Hello," name||"!"   /* Greet the user.  */                                                                 
         Exit                                                                                                                                         

Variables

  • Variables are assigned values; referencing a variable retrieves its value.
  • Variable names consist of alphanumeric characters, and must start with a letter A through Z, or the special characters ! @ # $ _ or ? .
  • Case is ignored for variable names (the variable AAA is the same as the variable aaa)
  • The value of an unassigned variable is equal to the name of the variable in upper case.

The variables in the example below are highlighted in red.

                                                                                                                                                 
         /* Add two numbers and report the sum. */                                                                                                    
         Say "Enter a number"                                                                                                                         
         Pull n1                                                                                                         
         Say "Enter another number"                                                                                                                   
         Pull n2                                                                                                         
         sum = n1 + n2                                         
         Say n1 "+" n2 "=" sum                                 
         Exit                                                                                                                                         

Operators

  • The arithmetic operators are
    • addition ( 1 + 2 )
    • subtraction (3 - 1 )
    • multiplication ( 5 * 2 )
    • normal division ( 4 / 2 )
    • exponentiation ( 3 ** 2 )
    • division, returning the integer result ( 7 % 2 )
    • division, returning the remainder ( 7 / / 2 )
  • The string operators are
  • concatenation with one intervening blank ( "hello," name )
  • concatenation with no intervening blanks ( "hello," | | name )
  • concatenation with no intervening blanks ( "hello," | | "there" )
  • concatenation with no intervening blanks ( "hello,"name )

Expressions

  • Expressions are combinations of constants, variables, strings, and operators. (num + 3 | | 5 / 2 )
  • Expressions are evaluated left to right.
  • Normal arithmetic operator precedence is in effect. ( **, *, /, +, - )
  • (Sub)expressions within parentheses are evaluated first.
  • Arithmetic operators can be used with strings if the strings represent proper numbers.

Valid expressions:

                                                                                                                                                 
         4 + 3 - 1             results in   6                                                                                                         
                                                                                                                                                      
         4 * 3 - 1             results in   11                                                                                                        
                                                                                                                                                      
         4 * (3 - 1)           results in   8                                                                                                         
                                                                                                                                                      
         "red" "dog"           results in   "red dog"                                                                                                 
                                                                                                                                                      
         "red" || "dog"        results in   "reddog"                                                                                                  
                                                                                                                                                      
         "red" 4               results in   "red 4"                                                                                                   
                                                                                                                                                      
         "red" 4+5             results in   "red 9"                                                                                                   
                                                                                                                                                      
         4 * 2 || 5            results in   "85"                                                                                                      
                                                                                                                                                      
         4 * 2 5               results in   "8 5"                                                                                                     
                                                                                                                                                      
         4 * (2 || 5)          results in   100                                                                                                       
                                                                                                                                                      
         4 * "1.2"             results in   4.8                                                                                                       


Exercise: Write a short REXX exec to display the results of some of the expressions listed above. Run the exec. Alter the expression and run it again. Try some different expressions. For example,

                                                                                                                                                 
         /* Write out the value of expressions. */                                                                                                    
         Say 4 + 5 / 2                                                                                                                                
         Exit                                                                                                                                         

REXX Instructions

  • REXX instructions are statements which begin with specific keywords.
  • REXX keywords may be entered in upper, lower, or mixed case.
  • REXX keywords are not reserved; these keywords can be used as variables or labels.

The REXX keywords in the example below are highlighted in red.

                                                                                                                                                 
         /* Examples of REXX Instructions */                                                                                                          
         Say "Enter a number"                                                                                            
         Pull n                                                                                                          
         If n = 0 Then Exit                                                                 
         Do n                                                                                                            
            Say "Hello there!"                                                                                           
            End                                                                                                          
         Exit                                                                                                            

Assignment Statements

Assignment statements are used to assign values to variables.

                                                                                                                                                 
         n    = 5               stores "5" in n                                                                                                       
         var  = 6 + 1           stores "7" in var                                                                                                     
         var2 = var + 2         stores "9" in var2                                                                                                    
         name = "Bill"          stores "Bill" in name                                                                                                 
         x    = "Bill"||5 + 7   stores "Bill12" in x                                                                                                  

Expressions to the right of the equal sign are evaluated and the result is stored in the variable to the left of the equal sign.


Labels

  • Labels identify specific REXX statements or locations within a REXX exec.
  • Label names consist of alphanumeric characters, must start with a letter A through Z or the special characters ! @ # $ _ or ?, and must end in a colon.
<>·········Refer to a label by name only, without the trailing colon. 
<>············A label may be followed by a comment, REXX statement, assignment statement, or command. 
<>············

Commands

  • Any statement which REXX does not recognize as a valid comment, REXX instruction, assignment, or label is evaluated and passed to the supporting command environment for processing. For example, if the REXX interpreter found the following statement,
<>··it would 

 

  1. determine if "EXEC" or "FILELIST" were variables and make appropriate substitutions (let's assume they are "uninitialized variables" and therefore contain values equal to their names, in uppercase letters);
  2. concatenate the values to form the string "EXEC FILELIST";
  3. determine that "EXEC FILELIST" is not a valid REXX comment, instruction, assignment, or label; and
  4. pass the string to the underlying command environment for processing.


The underlying command environment would process the string as if it were a command.

  • When using VM, the most common command environments are CMS, CP, COMMAND, and XEDIT. On other systems the command environment might be TSO, DOS, OS/2, UNIX and so forth.
  • Use the REXX instruction "ADDRESS" to direct unresolved strings to a specific command environment. For example,
<>·······To direct a single command to an alternate environment, use the following format: 
<>·········

The Variable RC

If a command completes successfully it signals the REXX exec which invoked it by placing (usually) a zero ("0") into the special REXX variable "RC". If a command encounters errors, it usually places some other, non-zero numeric value into the RC variable. Execs can test the RC variable to determine whether or not the command succeeded.

                                                                                                                                                 
         .                                                                                                                                            
         .                                                                                                                                            
         .                                                                                                                                            
         'EXEC FILELIST'                                                                                                                              
         If rc = 0                                                                                                                                    
            Then Say 'All is well.'                                                                                                                   
            Else Say 'Filelist may have erred.'                                                                                                       
         .                                                                                                                                            
         .                                                                                                                                            
         .                                                                                                                                            
  

More information about the IF REXX instruction will be supplied later.


Miscellaneous Syntax Notes

  • A REXX program in VM/CMS must start with a comment.
  • Blank lines can be inserted into a program to improve readability, but have no affect on the program itself.
  • Blank characters can be inserted between keywords, constants, variables, strings, and operators without affecting the program; all instances of multiple blank characters (outside specific strings) are reduced to single blanks.
  • Several short REXX statements can be entered on the same physical line as long as they are separated from one another with semicolons. For example,
<>··A long REXX statement can be continued onto subsequent lines by coding a comma as the last character on the line which is to be continued. For example, 
<>··
<>·
<>·
  

Online VM/CMS Help

For online help for REXX, CP commands, CMS commands, or Xedit subcommands, enter one of the following commands on the CMS or Xedit command line:

                                                                                                                                                 
         HELP REXX MENU                                                                                                                               
         HELP CP MENU                                                                                                                                 
         HELP CMS MENU                                                                                                                                
         HELP XEDIT MENU                                                                                                                              

Exercise: invoke HELP for REXX and read a few entries.

The following BookManager books may be consulted for more complete discussions, if REXX is running under VM/CMS.