CL Programming - 1
CL Programming - 1
CL Programming - 1
Programming
P.S. Nair 1
CLP Structure
• PGM Command
• Declare Commands
• CL Processing Commands
• Logic Control Commands
P.S. Nair 2
CLP Structure
• Built-in Functions
• Program Control Commands
• The ENDPGM Command
• Comment Lines
• Labels in CLP
P.S. Nair 3
The PGM Command
• Each statement in CLP is CL Command
• The PGM command used to Identify start of
CLP
• Valid only within CLP
• Must be the First Command in the Program
• Example : PGM PARM(CL-variable name)
• PARM indicates one or more parameters
passed to this pgm from a CALLING pgm.
P.S. Nair 4
Declare Commands
• All Variables and Files used in PGM
should be Declared
• DCL is used to Declare Variables
• DCLF is used to Declare File
• Declare Commands should precede all
other Commands except PGM
P.S. Nair 5
CL Processing Commands
• CL Processing Commands used for
– Manipulating Variables
– Copy Files, Sort Files
– Create Duplicate Objects
– Override with Database Files
– Send Program Messages
– Send and Receive Display Files
– Send User Messages
P.S. Nair 6
Most Frequently used CL
Commands for CL Processing
• CHGDTAARA
• CHGSYSVAL
• CHGVAR
• CPYF
• CRTDUPOBJ
• MONMSG
• RTVSYSVAL
P.S. Nair 7
Examples
P.S. Nair 8
Examples
• Example for CHGSYSVAL - Changing the System Value of a
Character
P.S. Nair 9
Examples
• Example for CHGVAR - Changing Decimal
Variables
• CHGVAR &A &B
• The value of variable &A is set to the
value of the variable &B. If &B has a
value of 37.2, then the value of &A
becomes 37.2 also.
P.S. Nair 10
Examples
• Example for CPYF - Physical File to
Physical File
• CPYF FROMFILE(PERSONNEL/PAYROLL)
TOFILE(TESTPAY/PAYROLL)
MBROPT(*ADD) CRTFILE(*YES) ERRLVL(10)
• This command copies all of the records in
the physical file named PAYROLL in the
PERSONNEL library to the file PAYROLL in
the TESTPAY library.
• P.S. Nair 11
Examples
• Example for CRTDUPOBJ – Creating
Duplicate Objects
• CRTDUPOBJ OBJ(FILEA) FROMLIB(LIB1)
OBJTYPE(*FILE) TOLIB(LIB2) DATA(*YES)
The file named FILEA in library LIB1 is
duplicated and stored in library LIB2.
Authorities granted for FILEA are granted to
the new FILEA in LIB2, and data associated
with FILEA is copied to the newly created
FILEA in LIB2.
P.S. Nair 12
Examples
• Example for MONMSG – Sending Error
Messages
P.S. Nair 16
Comment Lines
• Character pair /* and */ are used as
Delimiters
• Example :
• /* This is a Commented Line */
P.S. Nair 17
Labels in CLP
• Label Identifies the Statements in CLP to
which Processing is Directed
• Used along with GOTO statement
•
• Example :
GOTO END
END: ENDPGM
P.S. Nair 18
CL VARIABLES
P.S. Nair 19
Declaring Variables
• Declaration of Variable means
– Declaration of Name of Variable
– It’s Type
– It’s Length
– It’s Initial Value
• DCL VAR(&variable name)
• TYPE(*DEC, *CHAR, or *LGL)
• LEN(length) VALUE(initial-value)
• Examples:
• DCL VAR(&PAR1) TYPE(*CHAR) LEN(15) VALUE(134.5)
• DCL VAR(&PAR2) TYPE(*CHAR) LEN(15 ) VALUE(134.5 )
• DCL VAR(&VAR1) TYPE(*DEC ) LEN(15 5) VALUE(134.5 )
P.S. Nair 20
Declaration of Files
• File used in CLP must be declared
• DCLF command is used for Declaration of File
• DCLF FILE(library-name/file-name)
RCDFMT(*ALL or record-format-name)
• Only ONE DCLF command is allowed in CLP
• DCLF is valid only within CLP
• Declare a File
• DCLF FILE(LIB/File name)
RCDFMT(RCD1)
P.S. Nair 21
Changing Variables
• CHGVAR command is used for changing the
value of Variable.
• To a Constant :
• CHGVAR VAR(&CITY) VALUE(‘Pune’)
• To the Value of Another Variable
• CHGVAR VAR(&A) VALUE(&B)
• To the Value of an Expression
• CHGVAR VAR(&A) VALUE( &A + 20 )
P.S. Nair 22
CL EXPRESSIONS and
OPERATORS
P.S. Nair 23
EXPRESSIONS
• Expression is a Group of Symbols used to
express certain Values
P.S. Nair 26
Using Logical Operators
P.S. Nair 28
Using Character Operators
• Character Operators are used for
Concatenating 2 character string
expressions
P.S. Nair 32
Built-In Functions
• %SWITCH function is Used to test Job
Switches that Condition Processing in CLP
P.S. Nair 33
% SWITCH in CLP
The 8 job Switches in CLP match the External Indicators U1 -
U8 in RPG
• % SWITCH can be specified in COND parameter of IF
command
• Example1:
IF COND ( %SWITCH ( 1XXXXXX1 ) )
THEN ( CALL RPGPGM1 )
• Example2:
• PGM DCL &A *LGL
CHGVAR VAR(&A) VALUE(%SWITCH(10000001))
.
.
ENDPGM
P.S. Nair 34
%Substring Built-in Function
• %SST function can be used to Manipulate a
character string in a Character Variable to
form a Substring
• To use a %SST function, variable should be
declared as Character
• Example :
%SST ( &DATE 1 4 )
1 : Starting Position
4 : Length of Characters to be Extracted
P.S. Nair 35
% SST In CLP
• %SST is usually specified in COND parm of IF command
• Example:
• IF COND( %SST ( &Variable 1 7 ) *EQ ‘CARITOR’)
• % SST is usually used in VAR and VALUE parameters of CHGVAR
command
• Example1:
• CHGVAR VAR ( %SST ( YYMM 3 2 ) ) VALUE ( % SST (&SYSDATE
12))
• Example 2:
If Var5 = ‘JDEdwards’ -> After applying below logic, Var2 will have
‘JD’
• CHGVAR VAR(&VAR2) VALUE(%SST(&VAR5 1 2 ))
P.S. Nair 36
CONTROL STRUCTURES
P.S. Nair 37
Controlling CLP Processing
• A CLP is processed in a Consecutive
sequence unless it is altered by Logic
Control Commands
• Logic Control Commands can be
Conditional or Unconditional
• Unconditional Branching alters Processing
regardless of Conditions
• Conditional Branching alters Processing
under certain conditions
P.S. Nair 38
Unconditional Branching
• GOTO command is classic example of Unconditional Branching
• BACK:
• IF COND(&VAR1 *LE 10) THEN(DO)
• CHGVAR VAR(&VAR1) VALUE(&VAR1 + 1)
• GOTO CMDLBL(BACK)
• ENDDO
P.S. Nair 39
Conditional Branching
• IF and ELSE commands are Conditional Branching
commands
• The IF command evaluates the condition of an Expression.
• If the condition is met, certain commands are executed
P.S. Nair 40
ELSE command
• The IF command may be paired with the ELSE command.
• ELSE command indicates what Commands to Run if the
Condition is not met
• Example:
• IF COND(&VAR1 *GT &VAR4) THEN(SNDPGMMSG
MSG(EREWREREW))
• ELSE CMD(SNDPGMMSG MSG('Hi, Varibale 2 is
greater'))
P.S. Nair 41
Using DO group
• The DO command triggers the Execution of a Group of Commands
• The commands listed after the DO command are executed until
ENDDO command is reached
• Each DO command must have ENDDO command.
• ENDDO command terminates the command group
• Example:
• IF COND(&VAR1 *GT &VAR4) THEN(DO)
• SNDPGMMSG MSG('Hi, Variable 1 is Greater')
• ENDDO
• The commands between the DO and ENDDO are often termed as DO
group
• DO group is executed consecutively when the DO command is
triggered
P.S. Nair 42
CALLING PROGRAMS and
PASSING PARAMETERS
P.S. Nair 43
Processing Jobs
• One of the most Important functions of CLP is to Process
Jobs and CALL programs
P.S. Nair 44
Calling Programs
• The CALL command Runs a Program and
Passes control to that Program
• Optionally, CALL command passes
parameter values to the CALLED program
to be processed
• Syntax: CALL PGM(Library / Program Name)
PARM(Parameter Value)
P.S. Nair 45
Using CALL command
• Qualify an Object with the CALL command
– CALL PGM ( USRLIB / FBRCALLR )
P.S. Nair 46
Transferring Control
• TFRCTL command calls the Program specified in
Command
P.S. Nair 48
Transferring Control
P.S. Nair 49
Returning Control
• The RETURN command ends the
Program
P.S. Nair 50
Submitting Jobs
• SBMJOB command submits a Job to a
JOB QUEUE to be Processed later as a
BATCH JOB
• Example:
• SBMJOB CMD(CPYF FROMFILE(LIB1/FILE1) TOFILE(LIB2/FILE1) +
• MBROPT(*ADD) CRTFILE(*YES)) JOB(TESTJOB) JOBQ(QINTER) +
• JOBPTY(1) HOLD(*YES)
P.S. Nair 52
Processing Database Files
P.S. Nair 53
Working with Database Files
• CL programs also Process and Manipulate Database
Files
P.S. Nair 54
List of CL Commands to
Process Database Files
P.S. Nair 55
Create Physical File Command
• The Create Physical File (CRTPF) command creates a
physical file in the database.
P.S. Nair 57
Create Logical File Command
P.S. Nair 59
Overriding Database Files
• OVRDBF can direct the system to Process a file other than
the file named in the called program
• OVRDBF can be used to change the attributes of the file
that is being Processed
• Syntax for OVRDBF
• OVRDBF file ( overriden - file-name)
• tofile ( library / file name )
• frcratio ( no of records being
• forced as a block )
• nbrrcds ( no of records being
• moved as a block )
• share ( *yes or *no )
• seqonly ( *yes or *no )
P.S. Nair 60
Using OVRDBF
• OVRDBF is used to qualify the Library where a File is located
• OVRDBF file ( APMAST ) tofile ( TESTLIB / APMAST )
• To override the file named in the called program, and instead to use the
file specified in TOFILE parameter of OVRDBF command
P.S. Nair 61
Deleting Overrides
• When a File is overridden by the OVRDBF command,
the Override remains effective in CLP until it is explicitly
deleted
P.S. Nair 62
CLRPFM
• The Clear Physical File Member (CLRPFM) command
removes all the data (including deleted records) from the
specified member of a physical file.
P.S. Nair 63
DLTF
• The Delete File (DLTF) command deletes the
specified database files or device files from the
system. Deleting the files also frees the storage
space allocated to the file.
P.S. Nair 64
Processing DISPLAY FILES In
Interactive CLPs
P.S. Nair 65
Concept of Display File
• Display file is set of Formatted Screens
P.S. Nair 66
Using Messages in CLP
• CL uses Messages to Establish
Communication between Programs and
Users
P.S. Nair 67
System Defined Messages
• System Defined Messages are Predefined by the System and Stored in
Messages Files
P.S. Nair 68
User Defined Messages
• User Defined Messages are Defined and Created by Users
– Immediate Messages
P.S. Nair 69
CL Commands for Sending and
Monitoring Messages
• SNDMSG : Send Message
• SNDMSG command is used by a Workstation User to send a User Defined,
Immediate Message to One or More Message Queues
• The message sent can be Informational or for Inquiry
• An Inquiry message requires reply from User .
•
SNDMSG MSG ( ‘message text’ ) TOUSR ( ‘user profile name’) +
TOMSGQ ( ‘message queue name’) +
MSGTYPE ( *INFO or *INQ ) +
RPYMSGQ ( message queue name )
• SNDMSG command can be used in Interactive Mode and in CL Program
• SNDMSG is more commonly used in Interactive Mode
• The Receivers of the Messages must Use DSPMSG command to display
the Messages
P.S. Nair 70
Send Break Message
• SNDBRKMSG : Send Break Message
• The SNDBRKMSG command sends Immediate messages to
Workstation Message Queues in Break Mode
• The Break Mode interrupts active work station Users and Displays
the message to be displayed
• SNDBRKMSG command can be used in Interactive Mode and in CL
Program
• SNDBRKMSG is more commonly used in Interactive Mode
P.S. Nair 72
SNDPGMMSG
• SNDPGMMSG command is used by CLP to send a Program
• Message to a :
– Workstation User
– System Operator
– User Message Queue
• SNDPGMMSG MSG(‘ message text’) MSGID ( message - ID ) +
• MSGF ( message-file-name ) +
• TOUSE ( user profile name ) +
• TOMSGQ ( message queue name ) +
• MSGTYPE ( *INFO or *INQ ) +
• RPYMSGQ ( message queue name )
P.S. Nair 73
Monitoring Messages
• MONMSG : Monitor Message
• MONMSG command is used to monitor the anticipated
messages in a CLP and to take actions accordingly
• To provide an Escape when an Error Message is
Encountered
• To specify actions to be taken when certain messages occur
during the execution of CLP
P.S. Nair 75
RTVJOBA
P.S. Nair 76
Advanced CL
P.S. Nair 77
Open Query File
• Most powerful CL command
• Creates temporary Access Path to a file
• This access path can be used only by the
job which executes the OPNQRYF
command
• A HLL can be used to access the records
from the derived file (file containing a
subset of records of original file)
P.S. Nair 78
Uses of Open Query File
• Select a subset of available records
• Order records by one or more fields in the
record
• Join records from different files into one
access path
• Group records together
• Calculate new fields that do not exist in the
physical file
P.S. Nair 79
Data Area
• Definition - Data Area is the Single Field File
with just Single record
• A Data Area is an AS/400 object (*DTAARA)
which can store a small amount of information.
• It is used to store simple and frequently
changing information/data.
• A Data Area can be changed, displayed and
retrieved by CL commands, CL programs or HLL
programs like RPG/400.
P.S. Nair 80
Data Area - Uses
• Passing information between programs
• Passing information within a program
• Supplying reference data that are
processed by programs
– Example: Current Interest Rates which can
change from day to day or Stock Rates which
can change from time to time.
P.S. Nair 81
Data Area - Processing
• Data areas can be processed using CL commands
• These commands can be executed from the command line or from
any CL programs
• Creating a data Area (CRTDTAARA)
• Data Areas are created using the CL command CRTDTAARA
(Create Data Area)
– Syntax : CRTDTAARA DTAARA(library-name/data-area
name) TYPE(data-area-type)
LEN(data-area length decimal-positions)
VALUE(‘initial value’)
TEXT(‘description’)
•
P.S. Nair 82
Data Area - Processing (Cont . .
.)
– Parameters
• DTAARA parameter specifies the name of the data area.
• TYPE parameter specifies the type of the data area.
• Valid types are,
• *CHAR - for character type
• *LGL - for logical type and
• *DEC - for decimal type
• LEN parameter specifies the length of the data area.
• Max.length of decimal is 15 positions
before the decimal point and 9 positions
after it. Max. length of character type is 2000.
• VALUE is a optional parameter is used to specify the initial
value to be present in the data area.
P.S. Nair 83
Data Area - Processing (Cont .)
• Printing or displaying contents of Data Area
• Contents of a Data Area can be printed or displayed using
DSPDTAARA (Display Data Area ) command
– Syntax: DSPDTAARA DTAARA(library-name/data-area-
name) OUTPUT(display-or-
print)
OUTFMT(output-format)
• Parameters
• OUTPUT parameter can either contain a *, if you want to
display the data area on the screen(default) or *PRINT if you
want to print the output.
• OUTFMT parameter lets you specify the format of the output
, either the default *CHAR format or *HEX format
P.S. Nair 84
Data Area - Processing (Cont .)
• Retrieving the contents of a data area (RTVDTAARA)
• The contents of the data area or a portion of *char data area
can be
• retrieved into a CL variable using the RTVDTAARA command.
– Syntax : RTVDTAARA DTAARA(library-name data-area-name
(substring-start-position substring-length)) RTNVAR(&CL-
variable name)
– Parameters
• DTAARA parameter specifies the name of the data area
whose value is to be retrieved.
• RTNVAR parameter specifies the CL variable name into
which the contents of the data area is to be retrieved.
• The default of the copy is *ALL unless you specify the length
and starting position of the data area content to be copied
P.S. Nair 85
Data Area - Processing (Cont .)
• Changing the content of Data Area (CHGDTAARA)
• The entire contents of the entire data area or a portion of the
*CHAR data area can be changed by using CHGDTAARA(change
data area
• command)
– Syntax: CHGDTAARA DTAARA(library-name data-area-name
(substring-start-position substring-length)) VALUE(new
value)
• Deleting a Data Area
• A Data Area can be deleted using the DLTDTAARA(Delete
Data Area)
• command.
– Syntax: DLTDTAARA DTAARA(library-name/ data-area-name)
P.S. Nair 86
Local Data Area
• Automatically provided by AS/400 for each job in the system.
• Not a permanent Object
• Created when a job is started and disappears when the job ends
• RTVDTAARA,CHGDTAARA and DSPDTAARA can be used on
Local Data Area using the DTAARA(*LDA)
• Explicit Creation/Deletion of LDA is not possible
• When using SBMJOB the submitting job’s *LDA is copied to the
submitted job’s LDA. After the job is submitted , its *LDA exists
separately from that of the submitting job
• *LDA can be used to pass values from one program to another
P.S. Nair 87