cl lecture

Upload: atom-k-padhy

Post on 05-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 CL Lecture

    1/25

    Student Notebook

    Unit 8. Control language programmingWhat this unit is about

    Control Language Programming (CLP) is a powerful Job ControlLanguage that provides far more functionality than simply controllingwork flow.This unit describes the basic functions and coding of CLPs.

    What you should be able to doAfter completing this unit, you should be able to: Code, compile, and execute a simple CL program Explain the structure of a CL program Describe OPM and ILE CALLs Use the System i LPEX Editor Use the System i Source Entry Utility Editor

    How you will check your progressAccountability: Lab exercises

    Copyright IBM Corp. 1997,2008 Unit 8. Control language programmingCourse materials may not be reproduced in whole or in part

    without the prior written permission of IBM.

    8-1

  • 8/2/2019 CL Lecture

    2/25

    Student Notebook

    8.1. Structure and rules:)

    Copyright IBM Corp. 1997,2008 Unit 8. Control language programming 8-3

    ;

    Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    3/25

    Student Notebook

    8-5

    Why use control language programs

    UsersOperatorsProgrammers

    Copy ri gh t I BM Co rp o ra ti on 2 0 08OL491 0.0igure8-3. Why use control languageprograms

    Notes:On some computer systems, programmers write high-level programs to do part of theapplication and surround it with instructions to the supervisor language to execute theprogram. Operator functions often require instructions to the supervisor program in adifferent format.On the System i, CL programs span both of these areas. A function that previously requireda user to type a series of instructions one at a time, from a run book, can now be put into aCL program and be run in one step. A CL program can solve some of the applicationrequirements that are normally written in RPG, COBOL, or another high-level language.All system functions are run via CL. CL may be entered interactively at a workstation.Several statements may be typed together in a diskette or in a source file source member,then submitted to a job queue as a job stream. They may be entered into the system as thesource for a program. That program can be compiled and run like any other high-levellanguage program. CL can be run in an interactive or batch environment.

    . CL can be compiled. The advantages of compiling CL are that it runs faster and can include) logic. Copyright IBM Corp. 1997,2008 Unit 8. Control language programming

    Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    4/25

    \Student Notebook,

    Control language program A set of CL commands arranged in a logical sequence toperform one or more tasks:- Like a procedure- Manage flow of work for an application- Can set up user environment- Made up of variables and arithmetic I logical operations- Can access screen (display) files and DB files Edited using System i Source Entry Utility editor or LPEX PCEditor

    Compiled on System i server; can be created using LPEX Can CALL other HLL programs- Can receive and pass parameters (variables) Can create in legacy environment (OPM) or IntegratedLanguage Environment (ILE)

    Copyright IBM Corporation 2008Figure 8-4. Control language program OL 49 1 0 .0

    Notes:The control of work flow is a primary function of CLP.A CL program is similar to a job control language or procedure language on anothersystem.As you can see in the visual, CL programs are based on System i commands, ControlLanguage. While there are many things that you can do with CL programs, we will coverthe basics in this unit.For further knowledge of CLP, you should plan on attending the OL20/S6020 ControlLanguage Programming Workshop.

    Copyright IBM Corp. 1997, 2008 Unit 8. Control language programmingCourse materials may not be reproduced in whole or in part

    without the prior written permission of IBM.8-7

  • 8/2/2019 CL Lecture

    5/25

    Student Notebook

    Command format

    PARAMETERALABEL: CRTLIB rLlB(A) ~I I /\OMMAND COMMAND KEYWORD VALUE

    LABEL NAME

    KEYWORD I POSITIONAL

    Copyright IBM Corporation 2008Figure8-6. Command format OL49 1 0 .0

    Notes:CL programs use the same command format as system commands with the addition of theability to code a label (used to set a branching point within the CL program).

    Copyright IBM Corp. 1997, 2008 Unit 8. Control language programmingCourse materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

    89

  • 8/2/2019 CL Lecture

    6/25

    Student Notebook

    CL command syntax (2 of 4) Keyword format- DSPLIB L1B(PAYLlB) QUTPUT(*PRINT)- DSPLIB QUTPUT(*PRINT) LlB(PAYLlB) Positional format- DSPLIB PAYLIB *PRINT Combination format- DSPLIB PAYLIB QUTPUT(*PRINT)

    Copyright IBM Corporation 2008Figure 8-8. CL command syntax (2 of 4) OL491 0.0

    Notes:When F4 is used to prompt for a command, it is entered in keyword format.This figure shows both the keyword form and the positional form for entering CLcommands.The keywords may be omitted if the parameter values are entered in the sequence in whichthey are listed in the reference material in the Information Center. All three commands inthis figure perform exactly the same function.

    Copyright IBMCorp. 1997,2008 Unit 8.Control language programming 8~11Course materials may not be reproduced in whole or in part

    without the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    7/25

    Student Notebook

    CL command syntax (4 of 4)/* This is a full-line comment */SNDRCVF DEV(*FILE) RCDFMT(MENUFMT)SNDRCVF RCDFMT(MENUFMT)SNDRCVF *N MENUFMT

    +ake default for positional parameter.generic name

    ~DSPOBJD OBJ(PAYLIB/PAY*)t OBJTYPE(*PGM *FILE)tConnects parts

    of a qualifiedname

    Blank to separatelist elements

    Copyr igh t I BM Co rpo ra t ion 2008Figure 8-10. CL command syntax (4 of 4) OL491 0.0

    Notes:

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-13Course materials may not be reproduced in whole or in part

    without the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    8/25

    Student Notebook

    Coding rules (2 of 2) /* and */- Use to frame a comment Predefined values- Start with * such as *ALL Generic names- Names end with * such as PAY* Plus(+) or Minus (-)- At the end of a line that continues on a next line. *N- Indicates the absence of a positional parameter

    ) Copyright IBM Corporation 2008

    Figure8-12. Coding rules (2of 2) O L 49 1 0 .0

    Notes:Using comments is encouraged. Comments are valuable documentation. The + and - signsare used for continuation. If you use prompting in the editor, statements will be createdcorrectly for you.

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-15Course materials may not be reproduced in whole or in part

    without the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    9/25

    Student Notebook

    8.2. Basic CL programming

    )

    Copyright IBM Corp. 1997,2008 Unit 8. Control language programming 8-17Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    10/25

    Student Notebook

    Control work flowPGM / * THIS IS CL PROG - MSTRCL */CALL PGMA /*COBOLPROG-STEP1 */CALL PGMB */* CL PROG - STEP 2CALL PGMC / * RPG PROG - STEP 3 */

    ENDPGM

    Copyright IBM Corporation 2008Figure8-15. Controlwork flow OL491 0.0

    Notes:This is an example of a very simple CL program.

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-19Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    11/25

    Student Notebook

    Declare Cl variable (DCl)DCl VAR(&NAME) TYPE( lEN( ) VAlUE()

    Examples:(1)DCl &A*lGl VALUE(' 1')(2)DCl &B *CHAR5 ABCD(3)DCl &C *DEC (6 2) 543.21

    Copyright IBM Corporation 2008Figure8-17. Declare Cl variable (DCl) Ol491 0.0

    Notes:Use the DCl command to declare any variables needed in your Cl program that are notalready implicitly declared. Declaring a file implicitly declares all the fields in the file as Clvariables. logical variables may be either a character '1' (true) or '0' (false). If a field isdeclared, but is not referenced by any other command in the program, the variable is notincluded in the compiled program.The figure above shows the default lengths and initial values for the three different types ofvariables. The examples below the table show how to declare a logical, character, anddecimal variable:1. In example 1, field &A is declared as a logical variable with initial value of character '1'

    (true).2. In the second example, field &B is declared as a character variable, five positions long,

    with initial value' ABCD '.3. In the third example, variable &C is declared as a decimal variable, six positions long.

    For &C, the last two positions are to the right of the decimal point, and the initial value is0543.21.

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-21Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    12/25

    Student Notebook

    Relational and logical expressionsExpression: (Operand Operator Operand)Result of a relational or logical expression: '1' True or '0' FalseOperands: Constant, Variable, Another Expression

    ~)

    Operators Description< LT Less Than= *EQ Equal To> *GT Greater Than *NG Not Greater Than

    >= *GE Greater Than or Equal To. . = *NE Not Equal To

    & *AND True if both operands are '1'*OR True if both or either operand is '1'

    . . *NOT Copyright IBM Corporation 2008

    Figure 8-19. Relational and logical expressions OL4910.0

    Notes:Logical and relational expressions are either true or false. The system represents a truecondition with a '1', and represents a false condition with a '0'. These are character values.This figure shows two ways of representing operators. You may use either themathematical symbols, or the equivalent English abbreviations to show a relationship.

    Copyright IBMCorp. 1997,2008 Unit 8.Control language programming 8-23Course materials may not be reproduced in whole or in part

    without the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    13/25

    Student Notebook

    IF COND(&A *GT 100) THEN(DO)CALL PGM1CALL PGM2ENDDO

    ELSE CMD(DO)CALL PGM3CALL PGM4ENDDO

    CALL PGM5

    J Copyright IBM Corporation 2008

    Figure 8-21. DOgroup OL 49 1 0 .0

    Notes:The DO command is valid in the THEN parameter of an IF statement or in the CMDparameter of an ELSE statement. It groups all commands between it and an ENDDOcommand.In this example, either PGM1 and PGM2 are executed or PGM3 and PGM4 are executed,but not both groups. PGM5 will be executed in either case.CL program syntax allows indentation for readability.

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-25Course materials may not be reproduced in whole or in part

    without the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    14/25

    Student Notebook

    PGMB and/or PGMC and/or PGMD

    IFCOND(&A=&B) THEN(CALL PGM(PGMB))IFCOND(&A=&C) THEN(CALL PGM(PGMC))IF COND(&A=&D) THEN(CALL PGM(PGMD))

    Copyright IBM Corporation 2008Figure8-23. PGMB and/or PGMCand/or PGMD OL491 0.0

    Notes:This figure shows a structured flow chart and the corresponding C L statements. Thesestatements are a series of IF statements. The condition is tested in each case. If &A = &8= &C = &0, all three programs, PGMB, PGMC, and PGMD, are run.

    Copyright IBM Corp. 1997,2008 Unit 8. Control language programming 8-27Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    15/25

    Student Notebook

    Branching within a program: GOTO

    J Statement labels must be unique. Copyright IBM Corporation 2008

    Figure8-25. Branchingwithin a program: GOTO OL491 0.0

    Notes:Any command in a CL program may have a statement label. Labels have one to 10characters and are followed by a colon. A label is used as the target of the GOTOcommand. In this program, when the GOTO START command is executed, control ispassed to the statement with label START at the beginning of the program. If &OPTION =12, the GOTO command sends control to the ENDPGM statement.

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-29Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    16/25

    Student Notebook

    The Do While (DOWHILE) command lets you process a group of commands zero or moretimes while the value of a logical expression is true. The DOWHILE command is used tostate a condition that, if true, specifies a command or group of commands in the program torun. The group of commands is defined as those commands between the DOWHILE andthe matching ENDDO command. After the group of commands is processed, the statedprocessing resumes with the next command following the associated ENDDO. If thecondition is true, the group continues processing with the first command in the group.When the ENDDO command is reached, control returns to the DOWHILE command toagain evaluate the condition. The logical expression on the COND parameter may be asingle logical variable or constant, or it must describe a relationship between two or moreoperands; the expression is then evaluated as true or false.When the DOWHILE group is processed, the stated condition is evaluated. If the conditionis true, the group of commands in the DOWHILE group is processed. If the condition isfalse, processing continues with the command following the associated ENDDO command.If the value of &LGL is true, the commands in the DOWHILE group are run until &INTisequal to 2 causing the &LGL variable to be set to false.

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-31Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    17/25

    )_ _ J '

    Student Notebook

    that specifies the action to take if the condition is true. The logical expression on the CONDparameter may be a single logical variable or constant or it must describe a relationshipbetween two or more operands; the expression is then evaluated as true or false.If the condition described by the logical expression is evaluated as true, the programprocesses the CL command on the THEN parameter. This may be a single command or agroup of commands specified by the DO, DOWHILE, or DOUNTIL commands. If thecondition is not true, the condition specified on the next WHEN command in the SELECTgroup is evaluated. If there is no WHEN command following this one, the commandidentified by the OTHERWISE command, if any, is processed. If there is no next WHENand no OTHERWISE command, processing continues with the command following theassociated ENDSELECT command.In the example, if the initial value of &LGL is true ('1'), processing continues with thecommand following the ENDSELECT, because there is no THEN parameter. If the initialvalue of &LGL is false ('0'), the COND of the second WHEN is evaluated. If &INTis lessthan zero, the CHGVAR is processed, setting the value of &INTto zero. Processing thencontinues with the command following the ENDSELECT. If the first two conditions are notmet, the value of &INT is checked to determine if it is greater than zero. If the value isgreater than zero, the DOUNTIL group is entered and the value of &INTis decrementeduntil it reaches zero. When &INTreaches zero, the DOUNTIL group is exited andprocessing continues with the next command following the ENDSELECT. If none,':oftheconditions on any of the WHEN commands is evaluated as true, the CHGVAR specified onthe CMD parameter of the OTHERWISE command is processed. The value of &INTremains unchanged while &LGL is set to be true. Processing then continues with the nextcommand following the ENDSELECT.

    Copyright IBMCorp. 1997,2008 Unit 8.Control language programming 8-33Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    18/25

    Student Notebook

    Communicating data to a program with CALL

    Copyright IBM Corporation 2008Figure 8-29. Communicating data to a program with CALL OL 49 1 0 .0

    Notes:This figure illustrates the rules of passing data to a Cl program: logical variables are either character '1' or '0'. Decimal constants are always passed with a length of (15 5), the default length in aDCl statement.

    Character constants pass with a 32-position length if the character string is 32 positionsor fewer. If the character string is greater than 32 positions, the exact length of thecharacter string is passed to the Cl, program.

    Variables must be in the same sequence in the PARM parameter of the PGM commandof the called program as they are in the PARM parameter of the CALL.When a variable is changed in the called program, it is automatically changed in thecalling program. The variables must be defined with the same characteristics in thecalled program as in the calling program, but do not have to have the same names.

    When parameters are passed from one Cl program to another, no new storage area isallocated in the called program for the variables received as parameters.

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-35Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    19/25

    Student Notebook

    HLL and CL programs may call each otherCL

    Figure 8-30. HLL and CL programs may call each other Copyright IBM Corporation 2008

    OL491 0.0

    Notes:From the standpoint of the calling CL program, there is no difference in calling an RPG,COBOL, CL, PLl1 or any other type of program.

    ) Copyright IBMCorp. 1997,2008 Unit 8.Control language programming 8-37

    Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    20/25

    Student Notebook

    CHGVAR: Conversion between *CHAR and *OECdata types

    CHGVAR VAR(&A) VALUE(&8)

    CHGVAR VAR(&8) VALUE(&A)

    JFigure8-32. CHGVAR:Conversion between *CHAR and *DEC data types

    Notes:

    O L 49 1 0 .0

    Data type conversion is often used in CLP to convert decimal data into character data inorder to use it in character string operations.In the above example, the decimal fields are shown with signs and decimal points forclarity. Decimal points are actually implied and the code for a plus or minus numberoccupies half of the low-order byte.

    ) Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 8-39

    Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM....-------~~~ ...~--~

  • 8/2/2019 CL Lecture

    21/25

    Student Notebook

    Data type conversion and concatenation example&NAME &CUSNUMA

    Resulting message: ~ ~Customer XYZ CORPORATION, account number 54321,is overdue by 030 days.

    t&DAYSA

    VARIABLE TYPE LENGTH VALUE&NAME *CHAR 25 XYZ CORPORATION&CUSNUM *DEC (5 0) 54321&CUSNUMA *CHAR 5&DAYS *DEC (3 0) 30&DAYSA *CHAR 3

    Figure8-34. Data type conversion and concatenation example OL 49 1 0 .0

    Notes:Decimal variables must be converted to character variables before they can be used inmessage text.

    Copyright IBM Corp. 1997,2008 Unit 8. Control language programming 8-41Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    22/25

    Student Notebook

    Write a Basic CL ProgramMachine exercise

    Figure8-36. Machine exercise O L 49 1 0 .0 Cop yr ig ht I BM Cor po ra tio n 2 00 8

    Copyright IBM Corp. 1997, 2008 Unit 8. Control language programming 8-43

    Notes:

    Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    23/25

    Student Notebook

    8.3. ILE versus OPM

    Copyright IBM Corp. 1997,2008 Unit 8. Control language programming 8-45Course materials may not be reproduced in whole or in partwithout the prior written permission of IBM.

  • 8/2/2019 CL Lecture

    24/25

    Student Notebook

    OPM versus ILEOPM ILE

    Copyright IBM Corporation 2008Figure 8-38. OPM versus ILE OL491 0.0

    Notes:When programming on the System i, you will hear the terms OPM and ILE. Theprogramming concepts and compilation commands that have been covered are consideredpart of the Original Programming Model (OPM). The System i now also supports a newermodel called Integrated Language Environment (lLE).The OPM model focuses on a single programmable language and offers error handlingspecific to that language. For example, interactions between RPG and COBOL programsare difficult to predict.The ILE model allows for programs written in different languages to run as a single unit.The output of an ILE compiler is a non-executable module. One or more modules are thenbound together to form an executable program.ILE also allows for: Modularity Better code optimization More extensive control over resources

    Copyright IBMCorp. 1997,2008 Unit 8. Control language programming 847Course materials may not be reproduced in whole or in part

    without the prior written permission of IBM... .... _---_ __ ._--__-----_ .._--._---------

  • 8/2/2019 CL Lecture

    25/25

    Student Notebook

    Dynamic program call*PGM MAIN *PGM B1

    *PGM 82 *PGM DATE

    *PGM B2A

    _.? Copyright IBM Corporation 2008

    Figure 8-39. Dynamic program call OL 49 1 0 .0

    Notes:Each *PGM object has been compiled from source member. The CALL operation codeused to call another program is resolved at execution time.We know how to compile a single executable program object from a source member. Adynamic program call from program MAIN to either 81, 82, or 82A, is resolved at the timewe actually run the programs. That is, program objects 81, 82, and 82A need not exist atthe time that program MAIN is compiled. Indeed, program UPDTOT, called from 82A doesnot exist, and any dynamic program call to a missing program object will not fail until theprogram is executed. Not until each program is actually called during execution of programMAIN, will the individual user's authority, and the program object's virtual address beresolved. This address resolution might only be performed on the first call to each of theprograms 81, 82, and 82A from within MAIN, or, depending on HLL and the specific codingtechnique used, might be performed on each call.We are encouraged to write small, modular routines, that can be easily reused, or calledfrom other programs, but the dynamic program call can have significant impact on systemperformance.