CL Programming
Introduction to CL Programs
When you write a program in RPG, it's easy to find your program's name and the. 'funny' thing is that you have to change the source type from CLP to CLLE.
- object type *PGM, attribute CLLE
- series of CL commands to accomplish a task
- like a script in UNIX/Windows or a batch file in DOS
- Task Categories:
- User Interface
Make the system easy to use for normal humans. - Operations
automate a series of job steps - Job Attributes
change/manage the job environment
- User Interface
- basic programming logic structures supported:
- sequential
- selection or decision
- iteration or looping
- programs can call other programs with bi-directional parameter passing
- can be tested with the Source Debug utility (STRDBG)
- Create the Source Physical File, QCLLESRC
- Create a source member with the name of the program. Source Type = CLLE
- Type in the CL Code - Use F4 to prompt for command parameters
- Save, Compile, & Test.
- Three types:
- *CHAR (Character)
- *DEC (Decimal)
- *LGL (Logical)
- Variables must always start with an ‘&’
- DCL - Declares a variable
- DCLF - Declares a File
- RCVF - Reads a record from any file
- SNDF - Sends a record to a DSPF
- SNDRCVF - Sends a record to a DSPF, waits for the user, and reads
- MONMSG - error checking
- RTV... - various Retrieve commands to fetch information from OS/400
- Only one *FILE per program
- Display file or Database File
- Cannot update Database Files
CL character string processing, built in substring function
When you need to extract part of a character variable (a string), use the substring function:
%SUBSTRING or %SST
The substring function is used in expressions and operates on a character string which is usually contained in a CL character variable.
%SUBSTRING or %SST
The substring function is used in expressions and operates on a character string which is usually contained in a CL character variable.
The %SUBSTRING or %SST syntax is:
%SST(&character-variable-name starting-position length )
This built-in function returns a substring from the contents of the specified CL character variable. The substring begins at the specified starting position within the variable and continues for the length specified. For example:
%SST(&TEST 5 3)
In this example, a portion of the variable &TEST is referenced. The returned substring is 3 characters long beginning at the 5th character position. If &TEST contains ABCDEFGHIJ, the resulting substring will be EFG.
%SST(&TEST 5 3)
In this example, a portion of the variable &TEST is referenced. The returned substring is 3 characters long beginning at the 5th character position. If &TEST contains ABCDEFGHIJ, the resulting substring will be EFG.
CL variables can also be used to specify the starting position and the length values in the function. For example,
CHGVAR VAR( &X ) VALUE( %SST(&TEST &S &L) )
if S=5 and L=3, then the variable X will contain 'EFG'
CHGVAR VAR( &X ) VALUE( %SST(&TEST &S &L) )
if S=5 and L=3, then the variable X will contain 'EFG'
Interactive programming
- Declare any variables not defined in the screen's record format
- prepare screen indicators and fields for display
(All variables are automatically initialized at the start of the program to neutral valued: blanks, zeros, or false.) - Do an Infinite Loop, that's right, infinite:
- SNDRCVF (Send / Receive File command)
- check Function key indicators which bypass normal screen behaviour
e.g. if F3, LEAVE the loop
e.g. if F21, CALL QUSCMDLN and ITERATE - turn off indicators for data validation and field attributes/conditioning
- reset to neutral values any output fields dependent on input fields
- edit each screen field for validity
> if there is a problem,
turn on validation indicator to issue message - if any validation indicator is on, ITERATE another loop
- perform screen function using valid data
- check Function key indicators which add to normal screen behaviour
e.g. if F9 is True, call a program passing valid data fields as parameters and/or clear fields - end of Do Loop
- end this program: run commands not requiring user interface and return
Calling other programs
Any *PGM object on the iSeries can CALL another *PGM.
A useful OS/400 program to call is the Display Command Line Window (QUSCMDLN) which displays a window containing a command line. The user can: * Enter commands. * Retrieve commands and run programs. * Prompt for commands. * Use help for CL commands. SEU calls QUSCMDLN when you press F21.
Passing parameters between CL programs
- Parameters are passed by reference (a pointer), not by value or by copy. Therefore, multiple programs refer to the same variable's contents.
- Programs are called dynamically and memory addresses are resolved at run time.
- Good News: great flexibility
- Bad News: high overhead initiating jobs.
- Bad News: no compile time checking that calling/called programs have matching parameter parameter lists. System trusts the programmer.
- parameter lists must have the same number of variables and be in the same sequence
- each variable in the list must have the same attributes (data type, length) in both programs.
- With great power comes great responsibility.
respective variables are same data type and length | ||||||||
*CHAR 3 | *DEC (5 2) | *LGL | ||||||
CALL PGM(PGMB) PARM( | &VAR1 | &VAR2 | &VAR3 | ) | ||||
variables share same memory, must match on type & length |
|
|
| parameter lists have same number and sequence of variables | ||||
PGMB: PGM PARM( | &FLD1 | &FLD2 | &FLD3 | ) |
The calling program sets the state of its variables (or fields). In the above example, the calling program refers to these by the names &VAR1, &VAR2, &VAR3. When the calling program passes these to Program B (PGMB), the operating system allows Program B to share those variables' memory locations. Program B gets what the calling program had.
Program B can refer to those variables using its own names. Program B can change the value of those variables. When Program B ends, the calling program resumes after the CALL cmd with the current value of the same variables. That is, the calling program gets whatever Program B changed. See below.
respective variables are same data type and length | ||||||||||||||
*CHAR 3 | *DEC (5 2) | *LGL | ||||||||||||
CALL PGM(PGMB) PARM( | &VAR1 | &VAR2 | &VAR3 | ) | ||||||||||
variables share same memory, must match on type & length |
|
|
|
|
|
| <= | parameters lists have same number and sequence of variables | ||||||
PGMB: PGM PARM( | &FLD1 | &FLD2 | &FLD3 | ) | ||||||||||
IF (&FLD1 = 'ABC') CHGVAR &FLD1 'XYZ' | ||||||||||||||
IF (&FLD2 = 12.34) CHGVAR &FLD2 (&FLD2 + 55.55) | ||||||||||||||
IF (*NOT &FLD3) CHGVAR &FLD3 '1' | ||||||||||||||
RETURN |
Prompting for parameters in a CL program
When a question mark precedes the command name, a prompt display is presented. This display is of the same format as that presented when pressing the F4 key from the command entry display.
e.g. ?CHGLIBL
e.g. ?CHGLIBL
Selective Prompting
Selective prompting allows you to identify individual command parameter(s) to be defaulted with custom values, prompted for user input or just displayed.
Parameter Prompt Codes | Description |
?? | The parameter is displayed and input-capable. Marked with '>' symbol on prompt screen if a value is supplied. |
?< | The parameter is displayed and is input-capable but never marked with '>' symbol on prompt screen. Unless the user inputs something, the value is ignored and the system default is used. This can be very confusing for the user. |
?& | The parameter is not displayed until F9=All parameters is pressed. Once displayed, it is input-capable. |
?* | The parameter is displayed but is not input-capable. |
?% | The parameter is not displayed until F9=All parameters is pressed. Once displayed, it is not input-capable. |
?- | The parameter is not displayed. (use this when you must pass a parameter that you do not want the user to see) |
![Retrieve Retrieve](/uploads/1/2/5/2/125294857/207454464.png)
- thanks, but I was already aware of IBM's 'stand' on this issue .... I was wondering if there was anyone out there that had found a way to get around this ??
report - There's a third party tool, RTVILESRC, available free at http://www.geocities.com/~alex_nubla/tips.htm As long as you can see the source in debug mode then you can retrieve it using the utility. Hope it helps Jonathan
report - 10 pointsBadges:
report - astradyne, that link seems to have expired.
report - that link seems to have expired. If you run STRDBG over the program, is a *LISTING view available? If it is, at worst, you should be able to copy/paste from the screen. If it isn't, then a valid link won't help. You might even try STRCPYSCN to a file, and then scroll through the *LISTING view in debug. Not sure if the two work together. I think iSeries Access emulators allow some kind of auto-copy/paste with append. The RTVILESRC function might be available somewhere, but the few Google references I tried all failed. That's why I try not to paste links to sites that don't have long histories of access. Tom
report