Mainframe Automation: Using REXX
Mainframe Automation: Using REXX
Using REXX
Agenda
Mainframe Automation
What is REXX?
REXX - The Basics
Input/Output Statements
Conditional Processing
Iterative Loops
String Parsing
Built-in Functions
Interacting with subsystems
Trapping command Output
Handling data sets
System Variables
REXX - Performance Perspective
Limitations of REXX
Next Steps - Extensions to REXX
Questions
References
Mainframe Automation
Program Format
Starts with a commented line containing the word “REXX”
The “EXIT” keyword marks the end of the EXEC.
No restrictions on program format
A line can have more than one clause
A clause can span over more than one line
Allows any indentation to improve program readability
A semicolon delimits an instruction, but can be implied by line-end
A comment starts with characters /* and ends with */
Variables do not need explicit declaration
All the below operators have their usual meaning:
+ - * / % = > < >= <= <>
SAY A displays A
A = 100; SAY A displays 100
SAY 'A = ' A displays A = 100
PULL – reads a string from the Input Queue
SAY 'Do you want to delete the file? Answer Yes or No'
PULL Ans
IF Ans = 'No' THEN SAY 'File will not be deleted.'
Select
When Balance > 100 Then say 'You have enough Balance.'
When Balance < 100 Then say 'Your Balance is low!'
When Balance < 10 Then say 'Your Balance is critically low!'
When Balance = 0 Then say 'Your Balance is zero. Please recharge.'
Otherwise say 'Your Balance is negative. Please recharge.'
End
Iterative Loops
Simple DO group – It just groups a set of instructions. Does not do repetitive processing
Do 5 Do i = 3 to -3 by -1
Say 'Hello' Say i
End End
j = 1 j = 1
Do while (j < 10) Do until (j < 10)
Say j Say j
j = j + 1 j = j + 1
End End
String Parsing
Parsing - splits up the data in a source string and assigns pieces of it into the variables.
Parse Value:
Parse value ’Thick and Thin’ with var1 var2 var3
Say var1 displays 'Thick'
Say var2 displays 'and'
Say var3 displays 'Thin'
Parse Var:
String = 'Bread and Butter'
Parse var String str1 str2 str3
Say Str3 str2 str1 displays 'Butter and Bread'
Stem Variables, also called Compound Variables, are a special category of variables that hold
multiple lines of data.
Compound variables contain a period in their names and allow for indexing.
They hold an array of numbers or strings.
The below piece of code retrieves all the lines from a stem variable.
do j = 1 to stemVar.0
say stemVar.j
j = j + 1
end
User-defined Functions
REXX allows users to create their own functions (sub-routines) within the exec
A list of frequently executed instructions are grouped under a sub-routine
The sub-routines can be invoked using call statements whenever needed
Here is an example:
/* Rexx */
j = 1
do while j < 5
call Printj
j = j + 1
end
exit
Printj:
Say 'J = ' j
return
Built-in Functions
A REXX exec can include a variety of commands that are specific to different host
environments. For example, a REXX exec can issue
But before issuing such commands, the correct command environment has to be established.
The “ADDRESS ” instruction directs the commands to the right host environment.
Connecting to DB2
OUTTRAP – Traps the output of a command into a stem variable.
This external TSO function is highly used to capture the system response for a command issued
from an exec.
The below piece of code illustrates its syntax.
X = OUTTRAP('Var.')
… REXX/TSO Command …
Y = OUTTRAP('Off')
Here is an example.
Drop var.
x = OUTTRAP('var.')
"REPRO INFILE(INFILE) OUTFILE(OUTFILE)"
Y = OUTTRAP('off')
Do i = 1 to var.0
Say var.i
i = i + 1
End
Handling data sets
REXX provides an easy way of handling data sets with minimal set of I/O instructions
The command EXECIO controls data set I/O. It can both read from and write to data sets.
The I/O data set must be either sequential or a single member of a PDS.
The EXECIO command does not perform data set allocation. Before it can perform I/O to or
from the data set, the data set must be allocated to a file specified in EXECIO statement.
Data sets have to be allocated before they could be used in the exec. Similarly, they should be
closed and deallocated after using them.
Allocate a File:
Write to a File
Write the contents of the stem variable into outfile and close the file:
"EXECIO * DISKW OUTFILE (FINIS STEM WVAR."
Close and deallocate a file
System variables in REXX give us the ability to retrieve and use system & session information
in execs. REXX offers two sets of system variables
SYSVAR
SYSVAR returns information about MVS, TSO/E, and the current session, such as
levels of software available, your log on procedure, and your user ID.
The information returned depends on the arg_name value specified on the function
call.
SYSVAR can only be used in TSO/E environments
Say "ISPF STATUS : " SYSVAR(SYSISPF)
displays ISPF STATUS: NOT ACTIVE
MVSVAR
MVSVAR returns information about MVS, TSO/E, and the current session, such as the
symbolic name of the MVS system
MVSVAR(SYSPLEX) displays the MVS sysplex name as found in the COUPLExx
or LOADxx member of SYS1.PARMLIB
System Variables
Here are a performance concerns/tips that can help you code efficient REXX execs
Drop large stem arrays (compound variables) when they are no longer needed
DROP stemvar.
Literals work better than variables. Prefer them whenever you can.
if Firstname = 'Alfred' works better than
strName = 'Alfred'
if Firstname = strName
Instead use:
strName = STRIP(strName)
strName = LEFT(strName,6)
REXX in a performance view-point
Do not issue functions like DATE and TIME repetitively within loops. Instead issue
them once outside the loop and store them in a variable and use the variable within
the loop.
Calling external subroutines may not be a great idea if you are within a loop. If at all a
need arises, code the routine inside your exec
Code subroutines as close to the call as possible
When there are more than one subroutine, code the frequently used routine first.
Consider using a subroutine when a large number of statements are needed for
something that is often NOT executed.
Avoid placing comments inside loops. A single long comment is efficient than multiple
smaller comments.
REXX – The other side
Of course, REXX has a few limitations just like any other programming language.
REXX is an interpreted language. Though it offers ease of debugging, its execution
speed is much slower and less efficient compared to compiled languages.
Consumes more processor resource
Slower File I/O speed
Slower decimal arithmetic and relatively lesser mathematical precision
Lack of native support for file access methods like VSAM
However, the REXX execs can be compiled as well. There are IBM REXX compilers available for
purchase that can greatly improve performance and reduce system load. Compiled REXX
programs run faster than interpreted programs. Because a program has to be compiled only one
time, system load is reduced and response time is improved when the program is run
frequently.
Next Steps – Extensions to REXX
ISPF Dialogs
ISPF Dialogs (Panels) are used to develop interactive REXX tools
Dialogs can gather input, validate them at real-time and decide program path
The dialogs have to be developed using ISPF panel definitions before invoking them
through REXX
Edit Macros
A collection of TSO/ISPF commands packaged to perform a specific activity
Tailor-made macros can perform a host of house-keeping activities
File Tailoring
It is a way of generating formatted reports using predefined templates called skeletons
It can be used to tailor JCL skeletons and submit jobs automatically
Questions?
References