Structure of a CL Program

Structure of a CL Program
Part 6
Presentation © Copyright 2008, Bryan Meyers, www.bmeyers.net
Entering CL Source Statements
• Free format statements
– No columnar restrictions
– Use indentation to improve readability
• Uppercase and lowercase characters allowed
– Use lowercase to improve readability
– Prompter returns uppercase
• Except on quoted strings
• Blank lines allowed
• Keyword vs positional notation
– Positional notation may be more readable
– Prompter returns keyword notation
Comments
• Comments begin with /* and end with */
– May be in line with command
/* Program Name..... PROGRAM1
/* Author........... Joe Programmer
/* Date Completed... June 30, 20xx
/* Program Purpose.. To illustrate the use of comments
/* Service Request#. NONE
GOTO LOOP /* Comment AFTER command */
/* Comment BEFORE command */ GOTO LOOP
/* Comment BEFORE command */ GOTO LOOP /* and AFTER command
IF
(&var1 = &var2) GOTO LABEL1
/* comment within IF/ELSE group */
ELSE GOTO LABEL2
*/
*/
*/
*/
*/
*/
Continuing a Command
• Command may continue to next line
– Use + to continue with first non-blank character in next
line
– Use – to continue at first position of next line
• Even if it’s blank
• Rarely used
• Align continuation characters to improve readability
SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG)
+
MSGDTA('The message has been sent')
+
MSGTYPE(*COMP)
CHGVAR
&alphabet
+
('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij+
klmnopqrstuvwxyz')
Labels
• Labels provide extra documentation for a
command or a command block
• Can be used as a target of a GOTO command
• Up to ten characters, followed by colon
MONMSG
LOOP1:
IF
ERROR:
RETURN
ENDPGM
CPF0000 EXEC(GOTO ERROR)
(&number < 3) DO
SNDMSG MSG(&text) TOUSR(&user) MSGTYPE(*INQ) +
RPYMSGQ(&sender)
CHGVAR &number (&number + 1)
GOTO
LOOP1
ENDDO
Sections of a CL Program
• Program information section
– Program documentation
– Optional
• Program linkage section
– Marks beginning of the program
– Identifies parameters passed to program
• Declarations section
– Sets processing options for program
– Defines files and variables used in program
• Procedure section
– Specifies processes the program will perform
– Usually includes error handling segment
Program Information Section
• Program documentation
• Contains only comments
/* ------------------------------ Program Information Section
/*
/* Program Name ...... APPGM1
/*
/* Author ............ Joe Programmer
/* Date Completed .... December 31, 20xx
/*
/* Program Purpose ... This program sends a message to a
/*
user, optionally requesting a reply
/* Parameters ........ (1) User id
/*
(2) Reply requested? (Y/N)
/*
(3) Message text
/*
/* ----------------------------------------------------------
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
Program Linkage Section
• PGM command
– Marks beginning of program
– Identifies parameters received when program
executes
/* ---------------------------------- Program Linkage Section */
PGM PARM(&user &reply &text)
Variables to hold incoming parameters
Declarations Section
• Sets processing options for the program
• Declares (defines) any variables that the
program will use
• Declares any files that the program will use
/* ------------------------------------- Declarations Section */
DCLPRCOPT TEXT('Send a message to a user')
COPYRIGHT 'Copyright Acme Corp. 20xx. All rights reserved.'
DCL &reply
*CHAR
1
DCL &sender *CHAR 10
DCL &text
*CHAR 255
DCL &user
*CHAR 10
DCLF Myfile
Declarations Section
• DCLPRCOPT
– Sets processing options
• COPYRIGHT
– Assigns copyright text
• DCL
– Defines a variable
• DCLF
– Defines a file used by program
/* ------------------------------------- Declarations Section */
DCLPRCOPT TEXT('Send a message to a user')
COPYRIGHT 'Copyright Acme Corp. 20xx. All rights reserved.'
DCL &reply *CHAR
1
DCL &sender *CHAR 10
DCL &text
*CHAR 255
DCL &user
*CHAR 10
DCLF Myfile
Procedure Section
• Specifies procedures and processes the program will
perform
• Four segments
– Global message monitor
• Provides program-wide error handling instructions
• Optional
– Main procedure segment
• All normal processing performed by program
– Error handling segment
• Major error-handling procedures
• Optional
– Subroutine segment
• Blocks of code suited to a particular task
• Optional
• Ends with ENDPGM command
Procedure Section
/* ---------------------------------------- Procedure Section */
/* --------------------------------- Global Message Monitor */
MONMSG (CPF0000) EXEC(GOTO ERROR)
/* --------------------------------- Main Procedure Segment */
IF (&reply = 'Y') DO
RTVUSRPRF MSGQ(&sender)
SNDMSG MSG(&text) TOUSR(&user) MSGTYPE(*INQ) +
RPYMSGQ(&sender)
ENDDO
ELSE DO
SNDMSG MSG(&text) TOUSR(&user) MSGTYPE(*INFO)
ENDDO
RETURN
/* Normal end of program */
/* ----------------------------------- Error Handling Segment */
ERROR:
DMPCLPGM
/* Dump the CL program */
MONMSG CPF0000
/* Just in case
*/
SNDPGMMSG MSG('Error occurred in program.')
ENDPGM
INCLUDE Statement
• Embeds another source file when compiling
– Works with OPM or ILE CL compiler
– Cannot nest INCLUDE statements
– Cannot include PGM or ENDPGM
• Compiler can name default INCFILE parameter
INCLUDE MYMBR MYLIB/MYSRC
INCLUDE MYMBR
Use CRTCLMOD … INCFILE(MYLIB/MYSRC)
Complete CL Program
/* ------------------------------ Program Information Section */
/*
*/
/* Program Name ...... APPGM1
*/
/*
*/
/* Author ............ Joe Programmer
*/
/* Date Completed .... December 31, 20xx
*/
/*
*/
/* Program Purpose ... This program sends a message to a
*/
/*
user, optionally requesting a reply
*/
/* ---------------------------------------- Procedure Section */
/* Parameters ........ (1) User id
*/ /* ---------------------------------------- Procedure Section */
/*
(2) Reply requested? (Y/N)
*/ /* --------------------------------- Global Message Monitor */
/*
(3) Message text
*/
/* --------------------------------Global Message Monitor */
MONMSG
(CPF0000) EXEC(GOTO ERROR)
/*
*/
MONMSG
(CPF0000)
EXEC(GOTO
ERROR)
/* ---------------------------------------------------------- */ /* --------------------------------- Main Procedure Segment */
/* --------------------------------- Main Procedure Segment */
/* ---------------------------------- Program Linkage Section */ /* Send message, requesting reply if required */
PGM PARM(&user &reply &text)
Send message,
requesting reply if required */
IF/*(&reply
= 'Y') DO
IF
(&reply
=
'Y')
DO
RTVUSRPRF MSGQ(&sender)
/* ------------------------------------- Declarations Section */
RTVUSRPRF
MSGQ(&sender)
SNDMSG MSG(&text) TOUSR(&user) MSGTYPE(*INQ) +
DCLPRCOPT TEXT('Send a message to a user')
SNDMSGRPYMSGQ(&sender)
MSG(&text) TOUSR(&user) MSGTYPE(*INQ) +
COPYRIGHT 'Copyright Acme Corp. 20xx. All rights reserved.'
RPYMSGQ(&sender)
ENDDO
ENDDO
ELSE DO
DCL &reply
*CHAR
1
ELSE
DO MSG(&text) TOUSR(&user) MSGTYPE(*INFO)
SNDMSG
DCL &sender *CHAR 10
SNDMSG
MSG(&text) TOUSR(&user) MSGTYPE(*INFO)
ENDDO
DCL &text
*CHAR 255
ENDDO
DCL &user
*CHAR 10
SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) +
SNDPGMMSGMSGDTA('The
MSGID(CPF9898)
MSGF(QCPFMSG)
+
message
has been sent')
+
MSGDTA('The
message
has
been
sent') +
MSGTYPE(*COMP)
MSGTYPE(*COMP)
RETURN /* Normal end of program */
RETURN /* Normal end of program */
/* ----------------------------------- Error Handling Segment */
/* ----------------------------------- Error Handling Segment */
ERROR:
ERROR:
DMPCLPGM
/* Dump the CL program */
DMPCLPGM
Dumpinthe
CL program */*/
MONMSG CPF0000
/*/*Just
case
MONMSG
CPF0000
/*
Just
in
case
*/
SNDPGMMSG MSG('Error occurred in program.')
SNDPGMMSG
MSG('Error
occurred
in
program.')
ENDPGM
ENDPGM