AS/400 : Control Language
AS/400 Control Language
1
Control Language
• Set of all commands with which a user can
request system functions
• Single CL statement is called a Command
How CL Command is entered to system:
• Individually from a work station
• As part of batch jobs
• As source statements to create a CL program
AS/400 Control Language
2
Command Syntax
Each command is made up of :
• Command Label (optional)
• Command name (mnemonic)
• Parameters
Example :
Parameter
LABEL1: CLRLIB LIB(A)
Value
Keyword
Command name
Command label
AS/400 Control Language
3
Command Syntax
Command Label
•Identifies particular commands in a CL program for
branching and debugging purposes
• Can contain maximum of 10 characters
•EX- Label 1: CMDX
Command Name
•Identifies the function performed by the program that
is called when the command is run
•Is made up of a verb (or action), followed by a noun
(or phrase) that identifies the receiver of the action
AS/400 Control Language
4
Command Syntax
Example :
CRTLIB is a command name
CRT: abbreviation for verb CREATE
LIB : abbreviation for noun LIBRARY
Same rule is followed for all CL commands.
Try to guess the functions performed by the
following commands:
WRKLIB DSPUSR CHGJOB
DSPFD SNDNETF STRINFSKR
AS/400 Control Language
5
Command Syntax
Parameter:
•Identifies an individual value or group of values
used by the command( optional or required
depending on command)
•Can be specified in two forms:
•Keyword form
•Positional form
Parameters in keyword form- can be specified in any
order.
Example: Keyword form
CRTLIB LIB(MYLIB) TYPE(*TEST)
AUT(*EXCLUDE)
AS/400 Control Language
6
Command Syntax
Parameters in positional form:
•Does not have its keyword coded;
•Each command has one specific positional
order for its parameters
•Function of each value specified on the
command line is determined by its position in
the parameter set for that command
•Parameter values are delimited by one or
more blanks
AS/400 Control Language
7
Command Syntax
•*N (null) can be entered if one do not want to
enter the value for parameter
Example :
CRTLIB TYPE(*TEST) LIB(MYLIB)
AUT(*EXCLUDE)
CRTLIB MYLIB *TEST *EXCLUDE
AS/400 Control Language
8
CL Program
A CL program is a group of CL commands which
tell the system where to get the input, how to process it
and where to place the results.
CL programs are created in two steps:
•Source creation using SEU
•Program object creation (using CRTCLPGM)
AS/400 Control Language
9
Main Commands used in CL
•CL program limits :
PGM , ENDPGM
•CL program logic :
IF , ELSE, DO, ENDDO, GOTO
•CL program variables :
CHGVAR , DCL
•Conversion:
CHGVAR, CVTDAT
•Data areas :
CHGDTAARA, CRTDTAARA,
DLTDTAARA, DSPDTAARA,
RTVDTAARA
AS/400 Control Language
10
Main Commands used in CL
•Files
ENDRCV, DCLF, RCVF, SNDF,
SNDRCVF, WAIT
•Messages
MONMSG, RCVMSG, RMVMSG,
RTVMSG, SNDPGMMSG, SNDRPY,
SNDUSRMSG, SNDBRKMSG
• Miscellaneous
CHKOBJ, PRTCMDUSG, RTVCLSRC,
RTVJOBA, RTVSYSVAL, RTVUSRPRF,
ALCOBJ, DLTOVR, OVRDBF
• Program commands
CRTCLPGM, DLTPGM
AS/400 Control Language
11
Structure of a CL program
• PGM Command
• Declare Commands (DCL, DCLF)
• Program Function
• CL Processing Commands
• (CHGVAR, DLTF etc.)
• Logic Control Command (IF ELSE etc.)
• Built in Functions (%SST, %SWITCH
etc.)
• Program Control Commands(CALL etc.)
• ENDPGM Command
AS/400 Control Language
12
Structure of a CL program
Example :
PGM PARM (&VAR1) /*startup*/
DCL VAR1 CHAR 10
IF &VAR1 *EQ 'CALLA' THEN(DO)
CALL PGM(A)
ENDDO
ELSE DO
CALL PGM(B)
CHGVAR VAR1 'CALLC'
ENDDO
ENDPGM
AS/400 Control Language
13
Variable Declaration
•All variables must be declared using the DCL
command before they can be used
•Variable name must begin with an ampersand (&)
followed by a maximum of 10 characters
•The first character following the '&' must be
alphabetic and remaining characters alphanumeric
e.g. &P1VAR
•All parameters must be specified as variables
AS/400 Control Language
14
Variable Declaration
Variable Type Max Length Default Value
Character(*CHAR) 9999 Blanks
Decimal (*DEC) 15 digit & max 9 0
decimal positions
Logical (*LGL) ‘1’ or ‘0’ 0 or False
True or False
AS/400 Control Language
15
Variable Declaration
Command:
*CHAR
DCL VAR(Variable TYPE {*DEC} LEN(LENGTH)
Name)
Example:
DCL VAR(&LIB1) TYPE(*CHAR) LEN(10)
VALUE('QGPL')
DCL VAR(&PRICE) TYPE(*DEC) LEN(10 2)
DCL VAR(&READY) TYPE(*LGL) VALUE('1')
AS/400 Control Language
16
Changing value of variable
Command:
CHGVAR VAR(Variable name) VALUE(expression)
Example:
DCL VAR(&A) TYPE(*DEC) LEN(5 2)
DCL VAR(&B) TYPE(*CHAR) LEN(7)
CHGVAR VAR(...) VALUE(...)
VAR VALUE RESULT
&A 2.7 &A = 002.70
&A -12.75 &A = -012.75
&A 5869 ERROR
&B 'WONDERFUL' &B = 'WONDERF'
AS/400 Control Language
17
Using a variable to specify a List
Specifying a list as parameter value:
Example:
DCL &LIB1 *CHAR 10 'QTEMP'
DCL &LIB2 *CHAR 10 'QGPL'
DCL &LIB3 *CHAR 10 'MYLIB'
CHGLIBL LIBL(&LIB1 &LIB2 &LIB3)
•Variables cannot be specified in a list as a
character string. For example:
DCL &LIBS *CHAR 20 'QTEMP +
QGPL MYLIB'
CHGLIBL LIBL(&LIBS)
is wrong
AS/400 Control Language
18
Using a variable to specify a List
Using a variable to specify a qualified name:
•Specifying a qualified name as a parameter
value. For example:
DCL &LIB *CHAR 10 'MYLIB'
DCL &PGM *CHAR 10 'MYPGM'
DLTPGM PGM(&LIB/&PGM)
• Program and library name cannot be specified in
one variable. For example:
DCL &PGM *CHAR 20 'MYLIB/MYPGM'
DLTPGM PGM(&PGM)
is wrong
AS/400 Control Language
19
Variable type casting
• Used to convert the argument value to the specified
target data type
• For a cast operand that is the keyword NULL or a
parameter marker the target data type can be any data
type.
Example
The following CAST specification converts the
SALARY (defined as DECIMAL(9,2)) column to
INTEGER.
SELECT EMPNO, CAST(SALARY AS INTEGER)
FROM EMPLOYEE
AS/400 Control Language
20
Delimiters
Used to separate a character string in to individual part
that form a command. They are as following:
‘ ‘ - beginning and end of a quoted character
string
/* */ - beginning and end of a comment line
Blank - for separating parts of a commands
: - ending command label
“ “ - start of a quoted object name
/ - connects parts of qualified names or path
names
// - used as a default delimiter on inline data
file
AS/400 Control Language
21
Command Continuation
• If any part of the command continues on the next line,
a continuation character (+ or -) must be specified .
Example:
GRTOBJAUT OBJ(MYLIB/MYPGM) +
OBJTYPE(*PGM) +
USER(*PUBLIC)
AS/400 Control Language
22
Comments in a CL program
Comments can be written between the character pair
'/*' and '*/'
Example:
PGM /* ORDPGM Order program */
DCLF FILE(ORD01)
START: SNDRCVF RCDFMT(MENU)
IF (&OPT *EQ 1) THEN(CALL PGM10)
ELSE
IF (&OPT *EQ 2) THEN(CALL PGM20) /* Print Order */
ELSE
RETURN
GOTO START
ENDPGM
AS/400 Control Language
23
Program Control Commands
Program flow can be controlled by two type of
commands:
•Unconditional commands
e.g. GOTO
•Conditional Commands
e.g. IF...THEN...ELSE
AS/400 Control Language
24
GOTO command
• GOTO provides unconditional branching
• Label identifies the statement
• Label can have max. 10 characters and must be
followed by colon(:)
GOTO CMDLBL(Label)
Example:
PGM
.
START:SNDRCVF RCDFMT(MEU)
IF (&OPT *EQ 1THEN(CALL PGM10)
.
GOTO CMDLBL(START)
.
ENDPGM
AS/400 Control Language
25
IF command
Syntax:
IF COND(logical-expression)
THEN(CL-command)
ELSE (CL-command)
Examples:
•IF COND(&OPT = 1) THEN(CALL PGM10)
•IF (&A *EQ &B) THEN(GOTO START)
•IF (&A *EQ &B) GOTO START)
•IF (&A=&B) THEN(CALL PGM10)
ELSE CMD(CALL PGM20)
AS/400 Control Language
26
DO Command
• DO command lets you process a group of commands
between the DO and ENDDO commands
• DO groups can be nested
Example:
IF (&A=&B) THEN(DO)
CALL PGM10
CHGVAR &C &E
ENDDO
ELSE CMD(DO)
CALL PGM20
CHGVAR &C &D
CALL PGM30
ENDDO
AS/400 Control Language
27
IF and DO Commands
Nested DO groups
Nested IFs
•Max. 10 levels of nesting allowed with IF
commands
Example:
IF ...THEN...
IF ...THEN(DO)
IF ... THEN(DO)
.
.
ENDDO
ELSE DO
IF ... THEN(DO)
.
.
ENDDO
ENDDO
ENDDO
ELSE IF ... THEN ...
IF ... THEN ...
IF ... THEN ...
AS/400 Control Language
28
Expressions
•A simple form of an expression consists of single
constant, a variable, or a built-in function
• Two or more expressions can be combined to form
complex expression
• Types of expressions
• Arithmetic
• Character string
• Logical
• Relational
AS/400 Control Language
29
Arithmetic Expressions
Arithmetic operators:
• + Addition
• - Subtraction
• * Multiplication
• / Division
AS/400 Control Language
30
Arithmetic expressions
Examples:
• (&A + 1)
• (&A - &F)
• (&A + (-&B))
• (&A + &B - 15)
• ((&A + &B - 15) /&C)
AS/400 Control Language
31
Character String Expressions
Character String Operators:
• *CAT || Concatenation
• *BCAT |> Concatenation with blank
insertion
• *TCAT |< Concatenation with trailing
blank truncation
AS/400 Control Language
32
Character String Expressions
Examples:
&AA 'GOOD '
&BB 'JOB'
&CC 'ALSO GOOD'
&DD 'METHOD’
Expression Result
(&AA *CAT &BB) GOOD JOB
(&AA || &BB) GOOD JOB
(&CC |> &DD) ALSO GOOD
METHOD
(A *CAT MOUSE) AMOUSE
('A ' *CAT MOUSE) A MOUSE
(A *BCAT MOUSE) A MOUSE
('FAST ' *CAT MOUSE) FAST MOUSE
('FAST ' *TCAT MOUSE) FASTMOUSE
AS/400 Control Language
33
Character String Expressions
Example:
DCL VAR(&MSG) TYPE(*CHAR) LEN(100)
.
CHGVAR &MSG('Customer' *BCAT &CUSNAME +
*BCAT 'Account Number' *BCAT +
&CUSNAM *BCAT 'is overdue by' +
*BCAT &DAYS *BCAT 'days.')
The resulting message would be:
'Customer ABC Account Number 12345 is
overdue by 4 days.’
All the variables should be of type *CHAR
AS/400 Control Language
34
Relational Expressions
Relational operators:
• *LT < less than
• *GT > greater than
• *EQ = equal to
• *LE <= less than or equal to
• *GE >= greater than or equal
• *NL ¬< not less than
• *NG ¬> not greater than
• *NE ¬= not equal to
AS/400 Control Language
35
Logical Expressions
Logical Operators:
• *AND Ampersand &
• *OR Vertical bar !
• *NOT ¬
• Logical operators can only be used with
logical variables
AS/400 Control Language
36
Logical Expressions
Examples:
• PGM
DCL &A *LGL
DCL &B *LGL
DCL &C *LGL
.
IF (&A *OR &B *OR &C) THEN(CALL
PGMA)
ENDPGM
• IF ((&C *LT 1) *AND (&TIME *GT 1430) +
THEN…
• IF (&A *AND *NOT (&B=1)) THEN...
AS/400 Control Language
37
Priority of Operators
Priority of operators when evaluating the
expressions:
Priority Operators
1 Signed(+ and -) decimal values,
*NOT
2 *,/
3 +,- (when used between two operands)
4 *CAT, *BCAT, *TCAT
5 *GT, *LT, *EQ, *LE, *NE, *NG, *NL
6 *AND
7 *OR
•Left to right when operators with same priority
•Use parentheses to control the order of operation
AS/400 Control Language
38
Built in Functions
• SUBSTRING or %SST
• SWITCH
AS/400 Control Language
39
Built in Functions
%SUBSTRING or %SST
%SUBSTRING(Variable Starting-position Length)
• Variable must be a character
• *LDA can be coded in place of variable
• Sum of starting position and the length can not
be greater than length of variable or LDA
• Position and length can be variables
AS/400 Control Language
40
Built in Functions
%SUBSTRING or %SST:
Example:
PGM
DCL &NAME *CHAR 10 'RAJIV'
DCL &DATA *CHAR 10 'ABCDEE1234'
CHGVAR VAR(%SST(&NAME 4 1)) +
VALUE(%SST(&DATA 5 1))
CHGVAR VAR(%SST(&DATA 1 6)) +
VALUE(%SST(&DATA 1 4))
ENDPGM
AS/400 Control Language
41
Built in Functions
%SWITCH:
• %SWITCH (8-character-mask)
• compares one or more of eight switches with eight
switch setting for the job
• Returns a logical value of '1' or '0' i.e. True or False
• Switch settings
0 Off
1 On
X Don't care
• Job's initial switch setting is done through
JOBD commands (Defaults 00000000)
AS/400 Control Language
42
Built in Functions
%SWITCH:
Examples:
PGM
DCL &A *LGL
CHGVAR VAR(&A) +
VALUE(%SWITCH(10000001))
.
IF COND(%SWITCH(0X111XX0)) +
THEN(GOTO START)
.
ENDPGM
AS/400 Control Language
43
Demo
• Demonstrate a simple CL program to show
how to define variables and to use control
statements and built in functions.
AS/400 Control Language
44
Retrieval of System & Job Values
System Values:
• RTVSYSVAL Command
• CVTDAT Command
Job Attributes:
• RTVJOBA Command
User Profile Data:
• RTVUSRPRF Command
AS/400 Control Language
45
Retrieving System Values
RTVSYSVAL SYSVAL(System-Value Name) +
RTNVAR(CL-Variable-Name)
System Values that can be retrieved
QDATE Date
QDATFMT Date format
QDAY Day
QMonth Month
QYEAR Year
QTIME Time
QHOUR Hour
QMINUTE Minutes
QSECOND Seconds
QPRTDEV Printer Device
QSYSLIBL System library list
and, many more
AS/400 Control Language
46
Retrieving System Values
Example:
PGM
DCL VAR(&PDTIME) TYPE(*CHAR) LEN(6) +
VALUE ('162500’)
DCL VAR(&TIME) TYPE(*CHAR) LEN(6)
LOOP: RTVSYSVAL SYSVAL(QTIME) +
RTNVAR(&TIME)
IF (&TIME *GT &PDTIME) THEN(DO)
SNDPGMMSG('Powering down in +
5 minutes. Please sign off.')
ELSE DO
DLYJOB JOB(300) /* Wait 5 minutes */
GOTO LOOP
ENDPGM
AS/400 Control Language
47
Date Format Conversion
CVTDAT DATE(date-to-be-converted) +
TOVAR(variable) FROMFMT(old-format) +
TOFMT(new-format) TOSEP(new-separator)
• Variable lengths must be compatible with the
date format
• Available date formats
*DMY Date-month-year
*MDY Month-date-year
*YMD Year-month-date
*JUL Julian
AS/400 Control Language
48
Date Format Conversion
Example:
PGM
DCL &DATE6 *CHAR 6
DCL &DATE5 *CHAR 5
RTVSYSVAL QDATE RTNVAR(&DATE6)
CVTDAT DATE(&DATE6) TOVAR(&DATE6) +
TOFMT(*JUL) TOSEP(*NONE)
ADDPFM LIB1/FILEX +
MBR('MBR' *CAT &DATE5)
.
ENDPGM
AS/400 Control Language
49
Retrieving Job Attributes
RTVJOBA Command:
•Retrieves the attributes of the job which
issues the command
Job attributes that can be retrieved:
JOB Job name
USER User profile name
NBR Job number
LOGLVL Message logging level
OUTQ Output Queue
DATE Date of start of job
SWS Eight job switches
RUNPTY Run Priority
CURLIB Current library
and, many more
AS/400 Control Language
50
Retrieving Job Attributes
Example:
PGM
DCL &LIBL *CHAR 275
DCL &USER *CHAR 10
DCL &JOB *CHAR 10
RTVJOBA USRLIBL(&LIBL) +
USER(&USER) +
JOB(&JOB)
.
.
ENDPGM
AS/400 Control Language
51
Retrieving User Profile Attributes
RTVUSRPRF Command:
•Retrieves attributes of a User profile (except
Password)
Attributes that can be retrieved:
USRPRF User profile name
SPCAUT Special Authority
INLPGM Initial Program
JOBD Job description name
GRPAUT Group authority
MSGQ Message Queue
OUTQ Output Queue
USRCLS User class
PRTDEV Printer Device
and, many more
AS/400 Control Language
52
Retrieving User Profile Attributes
Example:
RTVUSRPRF USRPRF(*CURRENT) +
RTNUSRPRF(&USR) +
MSGQ(&USRMSGQ) +
MSGQLIB(&USRMSGLIB)
AS/400 Control Language
53
Message Handling
CL Program can
•Send Messages
SNDPGMMSG, SNDUSRMSG
• Receive Messages
RCVMSG
• Retrieve Messages
RTVMSG
• Monitor Messages
MONMSG
AS/400 Control Language
54
Monitor
Messages(MONMSG)
• MONMSG command is used to monitor escape, notify, and
status messages in the program.
• When the MONMSG command is compiled in a CL
program, it establishes a monitor for the arrival of the
specified messages.
• Up to 1000 MONMSG commands can be specified in a
program
• Completion and diagnostic messages can not be monitored
Contd.
AS/400 Control Language
55
MONMSG
Command level MONMSG command
• A MONMSG command that is not placed at the beginning of
the program applies only to the immediately preceding
command; this is called a command-level MONMSG
command
• The command-level MONMSG command monitors
only messages sent by the previous command.
AS/400 Control Language
56
MONMSG
Program level MONMSG command
• If one or more MONMSG commands are placed at the
beginning of the program, immediately following the declare
commands or the PGM command if there are no declare
commands, they monitor messages sent by all of the
commands in the program (maximum of 100). This is called
a program-level MONMSG.
AS/400 Control Language
57
MONMSG
EXAMPLES
Example 1: Monitoring Messages Sent by any
Command
PGM
MONMSG MSGID(CPF0001 CPF1999)
EXEC(GOTO EXIT2)
Example 2: Monitoring Messages Sent by a Single
Command
CHGVAR VAR(&A) VALUE(&A / &B)
MONMSG MSGID(MCH1211)
EXEC(CHGVAR VAR(&A) VALUE(1))
AS/400 Control Language
58
Demo
• Demonstrate a program showing
how to retrieve date format and
other system values.
• Example for message handling.
AS/400 Control Language
59