You are on page 1of 154

INDUS PL/I CLASS MATERIAL

Page:-1 /154

PL/I Hand Book

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-2 /154

SECTION NO.
SECTION 1 SECTION 2 SECTION 3 SECTION 4 SECTION 5 SECTION 6 SECTION 7 SECTION 8 SECTION 9 SECTION 10 SECTION 11 SECTION 11-A SECTION 11-B SECTION 11-C SECTION 11-D SECTION 12 SECTION 13

CONTENTS TOPIC
BASIC STRUCTURE OF PL/I PROGRAMS COMPILING UNDER MVS DATA TYPES AND DATA MANIPULATION PROCEDURES, FUNCTIONS SUBROUTINES AND FUNCTIONS CONTROL STATEMENTS CONDITIONS AND ON UNITS ARRAYS STRUCTURES AND PICTURES STORAGE CONTROL FILE PROCESSING STREAM ORIENTED DATA TRANSMISSION RECORD I/O DEFINING AND USING VSAM DATA SETS DEFINING AND USING REGIONAL DATA SETS CICS/DB2 EXERCISES

PAGE
3 8 13 22 36 50 54 69 72 81 101 105 117 123 134 145 149

References:1) IBM PL/I for MVS and VM Programming Guide 2) IBM PL/I for MVS and VM Language Reference 3) PL/I Structured Programming by Joan.K.Hughes.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-3 /154

SECTION 1
Basic Program Shell

BASIC STRUCTURE OF PL/I PROGRAMS

MYPROG:PROCEDURE OPTIONS(MAIN); . . /* this is a comment */ . END MYPROG; Note:1. MYPROG: is a label 2. Comments are encased in /* */ 3. OPTIONS(MAIN) indicates entry point of program and must be indicated only for one procedure in the program. 4. Columns start at 2 and end at 72. 5. Free form of writing 6. Column 2 to 72 user source code 7. Column 73 to 80 may contain program ID or sequence code. It is not checked by the compiler. A first simple program MYPROG:PROCEDURE OPTIONS(MAIN); GET LIST(A,B,C,D,E); SUM = A + B + C + D + E; AVERAGE_GRADE = SUM / 5; PUT LIST('AVERAGE GRADE IS =',AVERAGE_GRADE); END MYPROG; A = 8.50000E+01; /* Input thru GET LIST */ B = 7.60000E+01; /* Input thru GET LIST */ C = 9.50000E+01; /* Input thru GET LIST */ D = 8.80000E+01; /* Input thru GET LIST */ E = 9.80000E+01; /* Input thru GET LIST */ SUM = 4.42000E+02; AVERAGE_GRADE = 8.83999E+01; AVERAGE_GRADE IS=8.83999E+01 /* printed thru put list */ Character Sets $@# ABCDEFGHIJKLMNOPQRDSTUVWXYZ 0123456789 blank = Equal / assignment symbol + Plus sign Minus sign

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL * / ( ) , . ' % ; : & | > < _ ? Asterisk / multiply symbol Slash / divide symbol Parenthesis Comma Point or period Single quotation mark or apostrophe Percent symbol Semicolon Colon NOT symbol AND symbol OR symbol Greater than symbol Less than symbol Break (underscore) Question mark

Page:-4 /154

A combination of symbols may be used , e.g. A**3 A power 3 A >= A greater than or equal to 3 'A' || 'B' A concatenated with B IDENTIFIERS Variable names, Procedure names, Statement Labels, keywords (e.g. PUT, GET), File names etc. Consists of 1 to 31 alphabetic characters made up of (A-Z,!,#,$,0-9,_). First character must be a Alphabet. Example MY_LABEL_1 CLIENT_NAME A procedure with OPTIONS(MAIN) is an external procedure . External procedure names can be maximum of eight characters only. This is also true of a file name. STATEMENT FORMAT LABEL: KEYWORD STATEMENT OPTIONS; Example READ_STMT:GET LIST(A,B,C); Free form statement which can contain as many blanks as felt necessary by programmer. Position 1 reserved for use by O/S. Position 2 to 72 may contain one or more PL/I statement each separated by ' ; ' GET LIST (A,B); GET LIST (C,D); Alternately one statement may be continued across several lines e.g.; GET LIST (A,B); Position 73 to 80 may contain sequence number maintained by editor.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-5 /154

PL/I CONSTANTS Decimal Fixed Point Decimal Floating Point Character String Bit String Constant Binary Fixed Point Binary Floating Point

12.30 , 180, +10.23 , -1.12, 0.03 0.1234E+2 X.XXXXXE+XX, X.XXXXXE-XX Exponent range 10**-78 to 10**+75 'ABCDE' 'ABCD''EF', (2)'ABCD' '11011'B, (16)'0'B 1011B 0.11011011E+48B

LIST DIRECTED I/O INPUT GET LIST (A,B,C,D,E); GET FILE(SYSIN) LIST (A,B,C,D,E); 100 90 80 70 90 OR 70 80 90 100 OR 100,90,80,70,90

/*Reads from SYSIN /*explicit form

*/ */

90

In List Directed I/O no record boundaries are considered. Items are either separated by a blank or a comma GET LIST (A,B) COPY; /* additionally copies the data to SYSPRINT */ Example of Input 12.90,.04E+3,'ABCD','1010'B Example DECLARE DATA1 CHAR(12); GET LIST (DATA1); OUTPUT PUT LIST (50,'ABC',123,127); PUT LIST (23,86,87); 1 50 86 25 ABC 87 49 123 73 127 97 23 121

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-6 /154

PUT LIST (A,5,C*D); PUT PAGE LIST ('ABC'); PUT SKIP LIST(123); PUT SKIP(0) LIST (123); PUT SKIP(2) LIST (123); PUT PAGE LINE (10) LIST (123); PUT PAGE LIST ((100)'-');

/* note use of an expression C*D */ /* ABC prints in next page */ /* Skip one line before print */ /* Print without spacing */ /* Skip two lines before print */ /* Skip to line 10 of new page and print */ /* new page and 100 dashes */

PROGRAM STRUCTURE MNPROC: PROCEDURE OPTIONS(MAIN); . . CALL SUBPROC_1; CALL SUBPROC_2; . RETURN; SUBPROC_1: PROCEDURE; . . END SUBPROC_1; SUBPROC_2: PROCEDURE; . . END SUBPROC_2; END MNPROC; MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD1 FIXED DECIMAL(7,2); DCL FIELD2 FIXED DECIMAL(7,2); GET DATA (FIELD1,FIELD2); PUT SKIP LIST(SUM(FIELD1,FIELD2)); SUM:PROCEDURE (A,B) RETURNS(FIXED DECIMAL(7,2)); DCL A FIXED DECIMAL(7,2); DCL B FIXED DECIMAL(7,2); RETURN (A+B); END SUM; END MYPROG; //SYSIN DD * FIELD2=1.23,FIELD1=3.45; /* // Example to demonstrate scope of variables MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD1 FIXED DECIMAL(7,2);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL DCL FIELD2 FIXED DECIMAL(7,2); DCL SUM FIXED DECIMAL(7,2); GET DATA (FIELD1,FIELD2); CALL ADD(FIELD1,FIELD2); PUT SKIP LIST(SUM); PUT SKIP DATA; /* Procedure ADD */ ADD:PROCEDURE (A,B); DCL A FIXED DECIMAL(7,2); DCL B FIXED DECIMAL(7,2); SUM=A+B; END ADD; /* End of Procedure ADD */ END MYPROG; /* //GO.SYSIN DD * FIELD2=1.23,FIELD1=3.45; /* //

Page:-7 /154

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-8 /154

SECTION 2

COMPILING UNDER MVS

Cataloged Procedure IEL1CLG for compile link and go //IEL1CLG PROC LNGPRFX='IEL.V1R1M1',LIBPRFX='CEE.V1R4M0', // SYSBLK=3200,GOPGM=GO //PLI EXEC PGM=IEL1AA,PARM='OBJECT,NODECK',REGION=512K //STEPLIB DD DSN=&LNGPRFX..SIELCOMP,DISP=SHR // DD DSN=&LIBPRFX..SCEERUN,DISP=SHR //SYSPRINT DD SYSOUT=* //SYSLIN DD DSN=&&LOADSET,DISP=(MOD,PASS),UNIT=SYSDA, // SPACE=(80,(250,50)),DCB=(BLKSIZE=&SYSBLK) //SYSUT1 DD DSN=&&SYSUT1,UNIT=SYSDA, // SPACE=(1024,(200,50),,CONTIG,ROUND),DCB=BLKSIZE=1024 //LKED EXEC PGM=IEWL,PARM='XREF,LIST',COND=(9,LT,PLI), // REGION=512K //SYSLIB DD DSN=&LIBPRFX..SCEELKED,DISP=SHR //SYSPRINT DD SYSOUT=* //SYSLIN DD DSN=&&LOADSET,DISP=(OLD,DELETE) // DD DDNAME=SYSIN //SYSLMOD DD DSN=&&GOSET(&GOPGM),DISP=(MOD,PASS), // UNIT=SYSDA,SPACE=(1024,(50,20,1)) //SYSUT1 DD DSN=&&SYSUT1,UNIT=SYSDA,SPACE=(1024,(200,20)), // DCB=BLKSIZE=1024 //SYSIN DD DUMMY //GO EXEC PGM=*.LKED.SYSLMOD,COND=((9,LT,PLI),(9,LT,LKED)), // REGION=2048K //STEPLIB DD DSN=&LIBPRFX..SCEERUN,DISP=SHR //SYSPRINT DD SYSOUT=* //CEEDUMP DD SYSOUT=* //SYSUDUMP DD SYSOUT=* 1. 2. 3. 4. IEL1C IEL1CL IEL1CLG IEL1CG Compile only Compile and Link Edit Compile, Link-edit and run Compile load and run SOURCE COPY BOOKS LOADLIB WHERE COMPILER RESIDES COMPILATION LISTING COMPILED OBJECT CODE COMPILER WORK FILE LOADLIB WHERE LINKAGE EDITOR RESIDES PRIMARY INPUT FOR LINKAGE EDITOR OUTPUT OF LINKAGE EDITOR, THE LOAD MODULE SECONDARY INPUT FOR LINKAGE EDITOR LINK LIBRARY OF PRECOMPILED OBJECT CODE LINKAGE EDITOR WORK FILE

DD names PLI.SYSIN PLI.SYSLIB PLI.STEPLIB PLI.SYSPRINT PLI.SYSLIN PLI.SYSUT1 LKED.STEPLIB LKED.SYSLIN LKED.SYSLMOD LKED.SYSIN LKED.SYSLIB LKED.SYSUT1

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL LKED.SYSPRINT LINKAGE EDITOR LISTING

Page:-9 /154

SOME IMPORTANT COMPILER OPTIONS ESD | NOESD Listing of external symbol dictionary FLAG(I) List all messages FLAG(W) List all except information messages FLAG(E) list all except warning and information messages FLAG(S) list only severe error messages DECK | NODECK Deck specifies that the object module is written in card image form onto SYSPCH [NO]AGGREGATE controls listing of lengths of arrays and structures in program listing [NO]ATTRIBUTES (FULL | SHORT) controls listing of all source program identifiers with their attributes in the program listing. In SHORT un-referenced identifiers are omitted. NOT('not character') [NO]GONUMBER Defines new NOT character controls whether the compiler produces code which will give line number from the source program to be included in run time messages controls whether the compiler produces a object module on SYSLIN controls whether the compiler prints the options in effect in the program listing controls whether the compiler lists the source program in the program listing.

[NO]OBJECT [NO]OPTIONS [NO]SOURCE

SYSTEM (MVS|CMS|TSO|CICS..) Specifies the format in which parameters are passed to the main PLI procedure (this is system dependent) [NO]XREF(FULL|SHORT) Controls inclusion of cross reference table in the listing These options can be set in the source by the %PROCESS option,option,..; or *PROCESS option,option,.;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-10 /154

SECTION 3
PL/I Data Type FIXED DECIMAL FIXED BINARY FLOAT DECIMAL FLOAT BINARY PICTURE CHARACTER BIT

DATA TYPES AND DATA MANIPULATION


IBM Data Format Packed Decimal Fixed Point Floating Point Zoned Decimal Character Bit Storage 4 bits / decimal digit, sign Halfword or Fullword 32 | 64 | 128 Bits 8 bits per digit 8 bits per Char One byte min Basic Usage Arithmetic Operations Arithmetic Operations Arithmetic Operations Numeric Character Alphabetic Character Logical Data

DECLARE PRICE DECIMAL FIXED(5,2); Precision of five digits of which two are decimal fraction Scale Attribute Base attribute Identifier (variable) PL/I Keyword DECLARE PRICE FLOAT DECIMAL(6); PRICE = 3.12345 DECLARE PRICE DECIMAL FIXED(6,2); PRICE = 1234.56; DECLARE PRICE FIXED BINARY(15); /* 15 bits + sign bit */ PRICE = 123456; DECLARE PRICE FIXED BINARY(31); /* 31 bits + sign bit */ PRICE = 123456; DECLARE PRICE FIXED BINARY(15,2);/* 15 bits + sign. implied 2 bits fraction 1111111111111.11 fraction max .75 decimal*/ MODE ATTRIBUTE DCL COMPLEX_VAR FLOAT DECIMAL (6) COMPLEX; Both Real and imaginary component have FLOAT DECIMAL (6) base Scale and Precision. Partial Declarations possible DCL A DECIMAL; DCL A BINARY /* only base defined /* scale and precision default to FLOAT (6) /* only base defined /*scale and precision default to FLOAT(21) */ */ */ */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL DCL A FIXED; DCL A FLOAT

Page:-11 /154 /* only scale defined */ /* base and precision default to DECIMAL (5,0)*/ /* only scale defined */ /* base and precision default to DECIMAL (6) */

Other attributes default to the table below Declared Attributes Default Attributes DECIMAL FIXED (5,0) DECIMAL FLOAT (6) BINARY FIXED (15,0) BINARY FLOAT (21) DECIMAL FLOAT (6) BINARY FLOAT (21) FIXED DECIMAL (5,0) FLOAT DECIMAL (6) None, Variable begins with I-N BINARY FIXED (15) None, Variable begins with A-H,O-Z,@,#,$ DECIMAL FLOAT (6) Bit Data DCL END_OF_FILE BIT(1); DCL NO BIT(1) INIT('0'B); DCL YES BIT(1) INIT('1'B); . . END_OF_FILE = NO; ON ENDFILE(SYSIN) END_OF_FILE = YES; DO WHILE( END_OF_FILE); . . END; Character Data DCL CHAR_DATA Varying attribute DCL CHAR_DATA CHAR(20) INIT('ABCDEFGH'); /* Left Justified and padded on right with blanks CHAR(20) VARYING; /* This is a two byte length field followed */ /* by a 20 byte field for data */

*/

CHAR_DATA = 'MY NAME'; CHAR_DATA =''; /* null string */ DCL BIT_DATA BIT(8) VARYING; Defined attribute DCL A CHAR(20); DCL B CHAR(5) DEF A; DCL C CHAR(10) DEF A; DCL D CHAR(10) DEF C;

/* overlay on A */ /* overlay on A */ /* ERROR !, not allowed

*/

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL Permitted Redifines Base Identifier Coded Arithmetic variable Character String Bit String Position Attribute DCL CHAR_LIST DCL A DCL B

Page:-12 /154

Defined Item coded arithmetic variable with same BASE, SCALE, PRECISION Character string or Bit String Character string or Bit string CHAR(20); CHAR(10) DEFINED CHAR_LIST; /* overlays first ten positions */ CHAR(10) DEFINED CHAR_LIST POSITION(10); /* overlays next 10 positions */

Initial Attribute Use this to initialise variables DCL A FIXED DECIMAL (7,2) INITIAL(24.50); DCL B CHAR(10) INIT((10)'A'); DCL C BIT(2) INIT('00'B); DCL D FIXED BINARY(15) INIT (0); same effect can also be achieved by an assignment statement at lower code efficiency. PL/I Data Attributes FIXED DECIMAL ( Also represented as FIXED, DECIMAL FIXED) Data Format Packed Decimal Type of Data Coded arithmetic Default Precision 5 Decimal Digits S99,999 Maximum Precision 15 Decimal digits S999,999,999,999,999 Used for monetary calculations Example: DCL A FIXED DECIMAL (5,2) INIT (123.45); FIXED BINARY Data Format Fixed point signed binary (Halfword or Fullword) Type of Data Coded arithmetic Default Precision 15 Bits plus sign bit (Decimal 32,767) Maximum Precision 31 Bits plus sign bit (Decimal 2,147,483,647) Used for fast computations, usually for integers Variables beginning I-N default to fixed Binary unless explicitly defined otherwise Can be used for representing fractions, but fractions do not have exact representation in Binary . Example: DCL A FIXED BINARY (31) INIT(2147483647); FLOAT DECIMAL (Also represented as DECIMAL) Data Format Floating Point, 32bit, 64 bit or 128 bit representation Type of data Coded Arithmetic Default precision 6 Decimal digits Maximum precision 16 Decimal digits Range of Exponent 10**-78 to 10**+75 Examples: DCL A FLOAT DEC(6); DCL A DEC (6); /* Defaults to FLOAT */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-13 /154

DCL A FLOAT DEC(6) INIT(6.0E+12); Note that in storage there is no difference between FLOAT BINARY and FLOAT DECIMAL data representation. They are both represented as E, D and L format floating point data, depending on the precision. BIT Data Format Type of Data Default length Maximum Length bit (8 bits per byte) Logical None 8000 bits for constants 32767 bits for variables

Examples DCL FLAGS BIT(8) INIT('11100001'B); DCL FLAGS BIT(32) INIT((32)'0'B); Note that bit strings are (like character strings ) assigned left to right. If a smaller bit string is assigned to a larger string, padding with binary zeroes takes place on the vacant right positions. If the assigned bit string is larger than the receiving field then truncation takes place on the right. CHARACTER Data Format Type of data Default length Maximum Length Character Alphanumeric (anything goes) None 1000 characters for constants 32767 characters for variables On assignment data is left aligned and vacant positions on right are padded with spaces. If the receiving field is smaller, then truncation takes place on the right. Example: DCL A CHAR(20) INIT('ABCDEF'); DCL A CHAR(10) INIT((10)' '); DATA DECLARATION Explicit Declaration The scope of an explicit declaration of a name is that block to which the declaration is internal, including all contained blocks, except those blocks (and any blocks contained within them) to which another explicit declaration of the same name is internal. P: PROC; DCL A, B; Q: PROC; DCL B, C; R: PROC DCL C, D /* b of proc p is now hidden by this b */ /* a of proc p is still visible */ /* c of proc q is visible /* /* /* /* c of proc q is now hidden by this c */ a of proc p is still visible */ b of proc q is still visible */ d of proc r is visible */ */

*/

END R; /* now c of proc q is visible

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL /* b of proc q is visible /* a of proc p is visible END Q; /* Now B of PROC P is visible /* A of PROC P is visible END P;

Page:-14 /154 */ */ */ */

DECLARE Statement The DECLARE statement specifies attributes of a name and its position determines the scope of the declaration of the name. DECLARE [level] name [attribute] , [level] name [attribute] Factoring of Attributes Attributes common to several name scan be factored to eliminate repeated specification of the same attribute. DECLARE (A,B,C,D) BINARY FIXED (31); DECLARE (E DECIMAL(6,5), F CHARACTER(10)) STATIC; DECLARE ((A,B) FIXED(10),C FLOAT(5)) EXTERNAL; Implicit Declaration of Names If a name appears in a program and is not explicitly declared, it is implicitly declared. The scope of an implicit declaration is determined as if the name were declared in a DECLARE statement immediately following the PROCEDURE statement of the external procedure in which the name is used. Implicit declaration has the same effect as if the name were declared in the external procedure, even when all the occurrences of the name are internal to a block (called B, for example) that is contained in the external procedure. Consequently, the name is known throughout the entire external procedure, except for any blocks in which the name is explicitly declared. It is as if block B has inherited the declaration from the containing external procedure. Some attributes for a name declared implicitly can be determined from the context in which the name appears. These cases, called contextual declarations, are: A name that appears in a CALL statement, in a CALL option, or is followed by an argument list is given the BUILTIN and INTERNAL attributes. A name that appears in a FILE or COPY option, or a name that appears in an ON, SIGNAL statement for a condition that requires a file name is given a FILE attribute. A name that appears in an ON CONDITION or SIGNAL CONDITION statement is given the CONDITION attribute. A name that appears in the BASED attribute, in a SET option, or on the left-hand side of a locator qualification symbol is given the POINTER attribute. A name that appears in an IN option, or in the OFFSET attribute, is given the AREA attribute.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-15 /154

Examples of contextual declaration are: READ FILE (PREQ) INTO (Q); ALLOCATE X IN (S); In these statements, PREQ is given the FILE attribute, and S is given the AREA attribute. Scopes of Declarations P A: PROCEDURE; DECLARE P, Q; B: PROCEDURE; DECLARE Q; R = Q; C: BEGIN; DECLARE R; DO I = 1 TO 10; . END; END C; END B; D: PROCEDURE; DECLARE S; END D; END A; P is declared in the block A and known throughout A since it is not redeclared. Q is declared in block A, and re-declared in block B. The scope of the first declaration of Q is all of A except B; the scope of the second declaration of Q is block B only. R is declared in block C, but a reference to R is also made in block B. The reference to R in block B results in an implicit declaration of R in A, the external procedure. Therefore, two separate names (R and R`) with different scopes exist. The scope of the explicitly declared R is block C; the scope of the implicitly declared R is all of A except block C. I is referred to in block C. This results in an implicit declaration in the external procedure A. As a result, this declaration applies to all of A, including the contained procedures B, C, and D. S is explicitly declared in procedure D an is known only within D. INTERNAL and EXTERNAL Attributes Q Q R R S I

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-16 /154

INTERNAL specifies that the name can be known only in the declaring block. Any other explicit declaration of that name refers to a new object with a different, non overlapping scope. A name with the EXTERNAL attribute can be declared more than once, either in different external procedures or within blocks contained in external procedures. All declarations of the same name with the EXTERNAL attribute refer to the same data. EXTERNAL is the default for controlled variables, file constants, entry constants, Because external declarations for the same name all refer to the same data, they must all result in the same set of attributes. Language Specified Defaults When a problem-data name has not been declared with a data type or when the RETURNS option is omitted from a function procedure, the default is coded arithmetic problem data. If mode, scale, and base are not specified by a DECLARE or DEFAULT statement, or by a RETURNS option, variables with names beginning with any of the letters I through N are given the attributes REAL FIXED BINARY (15,0), and those with names beginning with any other alphabetic character are given the attributes REAL FLOAT DECIMAL (6). A scaling factor in the precision attribute constitutes an explicit declaration of FIXED. If mode, scale, or base is specified by a DECLARE or DEFAULT statement, or by a RETURNS option, the remaining attributes are completed from the following list of defaults: The default base is DECIMAL. The default scale is FLOAT. The default mode is REAL. Default precision is then completed from the following list: (5,0) for DECIMAL FIXED (15,0) for BINARY FIXED (6) for DECIMAL FLOAT (21) for BINARY FLOAT For example the statement: DCL X BINARY (15) /* no scale specified .gets the attributes REAL and FLOAT DCL X BINARY (15,0) /* scale specified gets the attributes REAL and FIXED. */ */

If no parameter descriptor list is given, the default is that the argument attributes match the parameter attributes.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-17 /154

DEFAULT Statement The DEFAULT statement specifies data-attribute defaults (when attribute sets are not complete). Any attributes not applied by the DEFAULT statement for any partially complete explicit or contextual declarations, and for implicit declarations, are supplied by language-specified defaults. Abbreviation: DFT RANGE(letter) Specifies that the defaults apply to names that begin with the name(s) specified. Letter can be any letter in the English alphabet. Example: RANGE (ABC) applies to these names: ABC ABCD ABCDE but not to: ABD ACB AB A Hence a single letter in the range-specification applies to all names that start with that letter. RANGE (letter : letter) Specifies that the defaults apply to names with initial letters that either correspond to the two letters specified, or to any letters between the two in alphabetic sequence. RANGE(*) Specifies all names in the scope of the DEFAULT statement. Example: DEFAULT RANGE (A:C) VALUE (FIXED DEC(10),FLOAT DEC(14),AREA(2000)); DECLARE B FIXED DECIMAL, C FLOAT DECIMAL, A AREA; These statements are equivalent to: DECLARE B FIXED DECIMAL (10), C FLOAT DECIMAL(14), A AREA(2000); Example: DFT RANGE(I:N) FIXED BINARY VALUE(FIXED BINARY(15)); DFT RANGE(A:H,O:Z) FLOAT DECIMAL VALUE(FLOAT DECIMAL(6)); Data Conversions When mixed data types appear in an arithmetic expression, PL/I causes the following conversions to take place to make the expression have all tokens of the same data type.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-18 /154

1. If base of the data item differs, DECIMAL is converted to BINARY 2. If the scale differs FIXED is converted to FLOAT 3. If CHARACTER and BIT are involved, then BIT is converted to CHARACTER a Bit 1 becoming character 1, a zero becoming character 0. Values operated on DCL A FIXED DEC, B FLOAT DEC; Y = A + B; DCL C FIXED DEC, D FIXED BIN; I = C*D; DCL E FIXED DEC, F FLOAT BIN; R = E/F; DCL K CHAR(13), I CHAR(5), J BIT(8); K = I || J; Conversion A is converted to FLOAT C is converted to FIXED BIN E is converted to FLOAT BIN Comments scale is different FIXED -> FLOAT base is different DEC -> BIN base and scale are different FIXED -> FLOAT DECIMAL -> BINARY BIT -> CHARACTER

J is converted to CHAR(8)

Assignment Statements DCL A FIXED DECIMAL(5,2) INIT(1.12); DCL B FIXED DECIMAL(5,2) INIT(2.32); DCL C FIXED DECIMAL(5,2); C = A+B; /* note that assignment is the = sign */ A,B,C = 0; /* all three variables assigned value 0 */ C = A*3 ; /* Right hand side is an expression */ Note that the assigned term can be a variable or an expression ( of constants / constants and variables. Arithmetic Operations ** Exponentiation * Multiplication / Division + Addition Subtraction Rule 1: Order of resolution in an arithmetic expression is Exponentiation first going right to left. Next moving from left to right multiplication or division, whichever appears first. Then addition or subtraction , whichever appears first, moving from left to right. Rule 2: If parenthesis are used in an expression, the innermost expression within the parenthesis is resolved first, in consonance with Rule 1, and the resolution moves outwards. Example A * (B+C) /* resolve B+C first and multiply the result by A */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL Rule 3: Prefix operators are performed before infix operators Example A = B**-A /* Negate A first. Then raise B to the power of A

Page:-19 /154

*/

Rule 4: Any expression or element may be raised to a power which can be a negative value Example A**2, (A+5)**3, A**-B Rule 5: If two or more operators with the highest priority appear in the same expression, the order of priority is from right to left Example -A**2 /* Exponentiation first, Negation next */ A**B**C /* Evaluate B**C first.Then raise A to the resultant power*/ Operator Precedence Level Operator 1 prefix +, prefix -, ** , 2 *, / 3 infix +, infix 4 || 5 >=,>,=,<, =,.. 6 & 7 |

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-20 /154

SECTION 4
Built In Functions Declaration required DCL function-name BUILTIN; Example DCL DATE BUILTIN;

PROCEDURES, FUNCTIONS

Arithmetic Built-in Functions ROUND(arg1,arg2) arg1 is the value to be rounded, can be an element, array element or array name arg2 specifies the digit position where rounding is to occur. If positive, rounding occurs nth digit to right of decimal point. If zero, rounding occurs at the first digit left of the decimal point. If negative rounding occurs at n+1 digit to the left of the decimal point. Example: DCL X FIXED DECIMAL(7,4); DCL Y FIXED DECIMAL(7,4); X = 123.7261 Y = ROUND(X,3); /* Y is 123.7260*/ Y = ROUND(X,2); /* Y is 123.7300*/ Y = ROUND(X,0); /* Y is 124.0000*/ Y = ROUND(X,-1); /* Y is 120.0000*/ MOD(arg1,arg2) The value returned is the remainder of the division of arg1 by arg2. COS(arg1), SIN(arg1), LOG(arg1) etc. are available Date and Time built in functions DCL DATEVAR CHAR(6); DCL TIMEVAR CHAR(9); DCL DATE BUILTIN; DCL TIME BUILTIN; DATEVAR = DATE; TIMEVAR = TIME;

/* YYMMDD /* HHMMSSTTT

*/ */

String handling functions SUBSTR(arg1,I,J) Returns a sub-string from arg1 (character, Bit or Picture attribute)starting at position I and J characters long. Arg1 can be a constant, variable name or an expression. It may also be specified as a LIST item in a GET or PUT statement. Pseudovariables Is a built in function appearing as a receiving field. Example: DCL DATE CHAR(6) INIT('980215'); SUBSTR(DATE,1,2)='99'; /* change YY to 99 GET LIST (SUBSTR(DATE,3,4)); /* get MMDD from SYSIN

*/ */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-21 /154

BIT Converts coded arithmetic data item or Character string to a Bit string DCL A BIT(32); DCL B FIXED BINARY(31); A = BIT(B); /* converts into a 31 + sign bit string */ A = BIT(B,15); /* A contains a bit string of length 15 */ CHAR Converts a given value to Character form DCL A FIXED DEC(5) INIT(175); DCL B CHAR(5); B = CHAR(A); /* B contains '175' */ B = CHAR(A,2); /*B contains '17'

*/

LENGTH Returns length of character or Bit string. Useful in finding length of character strings with the VARYING attribute. DCL A CHAR(30) VARYING; DCL A_LENGTH FIXED BINARY(15); A = 'ABCDEF'; A_LENGTH = LENGTH(A); /* A_LENGTH has a value of 6 */ INDEX Searches a given string for a specified BIT or CHAR sub-string. If found returns the left most occurrence of the sub-string else returns zero. Arguments can be bit string, character string, binary coded arithmetic field, decimal picture or array names. If both arguments are not bit strings, both arguments are converted into character strings, else the operation is performed on bit strings. DCL SENTENCE CHAR(40); SENTENCE = 'THE DOG CHASED THE TAC'; START = INDEX(SENTENCE,'TAC'); /* Locate 'TAC' */ SUBSTR(SENTENCE,START,3) = 'CAT';/* and Change it to 'CAT' */

REPEAT REPEAT(string,n) Repeats string n times. DCL SENTENCE CHAR(40); SENTENCE = (10)'ABCD' /*SENTENCE contains 'ABCDABCD.' */ SENTENCE = REPEAT('WALLA',2); /* SENTENCE is 'WALLAWALLA' */ TRANSLATE TRANSLATE(Source_string, Replacement_string, Position_string) DCL SOURCE CHARACTER(5); DCL TARGET CHARACTER(5); DCL NEW_VALUE CHAR(1); DCL OLD_VALUE CHAR(1); SOURCE = '+1234'; NEW_VALUE = ' '; OLD_VALUE = '+'; TARGET = TRANSLATE(SOURCE,NEW_VALUE,OLD_VALUE); /* TARGET is now ' 1234' */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-22 /154

PROGRAM ORGANISATION Programs A PL/I program consists of one or more external procedures. Each procedure can contain other procedures, begin-blocks, or both. Program Activation A PL/I program becomes active when a calling program invokes the main procedure. CONTRL: PROCEDURE OPTIONS(MAIN); CALL A; CALL B; CALL C; END CONTRL; Blocks A block is a delimited sequence of statements that determines the scope of the declaration of names declared within it, limits the allocation of automatic variables, and determines the scope of DEFAULT statements. Two kinds of blocks: procedure blocks and begin-blocks. Begin-blocks and procedures can contain declarations that are treated as local definitions of names. These declarations are not known outside their own block. Automatic storage is allocated upon entry to the block where the storage is declared. The storage is freed upon exit from the block. Block Activation Except for the main procedure, external and internal procedures contained in a program are activated only when they are invoked by a procedure reference. Begin-blocks are activated through sequential flow or as ON-units. During Block Activation: Expressions for automatic and defined variables are evaluated for dimension bounds, area sizes, string lengths, and initial values (including iteration factors). Storage is allocated for automatic variables and initialisation, if specified. Currently active blocks known to the procedure are identified, so that the correct generations of automatic storage are accessible, and the correct ON-units can be entered. Storage is allocated for any dummy arguments that might be created in this block. Block Termination

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-23 /154

A procedure is terminated when control passes back to the invoking block or to some other active block, by means other than a procedure reference. Similarly, a begin-block is terminated when control passes to another active block, by means other than a procedure reference. During block termination: The ON-unit environment is re-established as it existed before the block was activated. Storage for all automatic variables allocated in the block is released. Internal and External Blocks There can be no overlapping of blocks; A procedure that is contained within another block is called an internal procedure. A procedure that is not contained within another block is called an external procedure. Begin-blocks are always internal; A: PROCEDURE; . . B: BEGIN; . . END B; . . C: PROCEDURE; . . D: BEGIN; . . E: PROCEDURE; . . END E; . . END D; END C; . . END A; Procedures A procedure is a sequence of statements delimited by a PROCEDURE statements and a corresponding END statement. For example: NAME: A: PROCEDURE; . .

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL . END NAME;

Page:-24 /154

The leftmost label of the PROCEDURE statement represents the primary entry point of the procedure. Optionally, additional labels define secondary entry points. The ENTRY statement also defines secondary entry points. B: ENTRY; PROCEDURE and ENTRY Statements A procedure (subroutine or function) can have one or more entry points. The primary entry point to a procedure is established by the leftmost label of the PROCEDURE statement. Secondary entry points to a procedure are established by additional labels of the PROCEDURE statement and by the ENTRY statement. A: I: PROCEDURE (X); is the same as: A: PROCEDURE (X); I : ENTRY (X); When an EXTERNAL ENTRY is declared without a parameter descriptor list, matching between parameters and arguments does not occur. Therefore, no diagnostic message is issued if any arguments are specified in a CALL to such an entry point. example: DECLARE X ENTRY EXTERNAL; CALL X(parameter); Format for PROCEDURE statement entry-constant: PROCEDURE (parameter, parameter,) RETURNS(attribute) OPTIONS (Options-list) Format for ENTRY statement entry-constant: ENTRY (parameter, parameter,) RETURNS(attribute) OPTIONS (Options-list) Important OPTIONS are BYADDR or BYVALUE Specify how all parameters defined for this procedure entry are passed. If you specify BYADDR, parameters are received by address. If you specify BYVALUE, parameters are received by value. Any change to a parameter that is being passed by value is not reflected in the argument passed by the caller. BYADDR is the default BYVALUE can be specified for EXTERNAL PROCEDURE statements, but it cannot be specified for internal procedures or ENTRY statements. Additionally BYVALUE entry points can only have scalar arguments and return values that are either POINTER or REAL FIXED BINARY(31,0) BYADDR is the default.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-25 /154

Example: EXTR: PROCEDURE(A,B) OPTIONS (BYADDR); DCL (A,B) FLOAT; MAIN The PL/I procedure is the initial procedure of a PL/I program. The operating system control program invokes it as the first step in the execution of that program.

COBOL Specifies that the designated entry point is in a COBOL subprogram. ASSEMBLER specifies that the designated entry point is in an assembler subroutine. PL/I passes arguments directly to the subroutine, rather than via PL/I control blocks. Entries with the ASSEMBLER option are subject to the following rules: They cannot be used as a function reference. Any number of arguments can be passed in the CALL statement invoking the entry, from zero up to the number specified by the entry declaration, but intervening arguments cannot be omitted.

Parameter Attributes Because a parameter has no associated storage within the invoked procedure, it cannot be declared to have any of the storage attributes STATIC, AUTOMATIC, BASED, or DEFINED. However, it can be declared to have the CONTROLLED attribute. Controlled Parameter Bounds, Lengths, and Sizes The bounds, length, or size of a controlled parameter can be specified in a DECLARE statement either by asterisks or by element expressions. MAIN: PROCEDURE OPTIONS(MAIN) ; DECLARE (A(20), B(30), C(100), D(100)) CONTROLLED, NAME CHARACTER (20), I FIXED (3,0) ; DECLARE (SUB1,SUB2) ENTRY; ALLOCATE A,B; CALL SUB1(A,B) ; FREE A,B; /* Frees A and B allocated in SUB1 */ FREE A,B; /* Frees A and B allocated in MAIN */ GET LIST (NAME,I) ; /*Get NAME and I from SYSIN */ CALL SUB2 (C,D,NAME,I); FREE C,D; END MAIN; SUB1: PROCEDURE (U,V) ; DCL (U(*), V(*)) CONTROLLED; ALLOCATE U(30), V(40); RETURN; END SUB1;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL SUB2: PROCEDURE (X,Y,NAMEA,N); DECLARE (X(N),Y(N)) CONTROLLED, NAMEA CHARACTER (*), N FIXED(3,0); ALLOCATE X,Y; RETURN; END SUB2;

Page:-26 /154

When SUB1 is invoked, A and B, which have been allocated as declared, are passed. The ALLOCATE statement in SUB1 specifies bounds for the arrays; consequently, the allocated arrays, which are actually a second generation of A and B, have bounds different from the first generation. If no bounds were specified in the ALLOCATE statement, the bounds of the first and the new generation are identical. On return to MAIN, the first FREE statement frees the second generation of A and B (allocated in SUB1), and the second FREE statement frees the first generation (allocated in MAIN). In SUB2, X and Y are declared with bounds that depend upon the value of N. When X and Y are allocated, this value determines the bounds of the allocated array. The asterisk notation for the length of NAMEA indicates that the length is to be picked up from the argument (NAME).This can be done by the built-in function LENGTH. Procedure Activation After the keyword CALL in a CALL statement (as described in CALL Statement) After the keyword CALL in the CALL option of the INITIAL attribute Example: SET_UP is the name of a procedure that can set the initial values of elements in TABLE. X and Y are arguments passed to SET_UP. DECLARE TABLE (20,20) INITIAL CALL SET_UP (X,Y); As a function reference Entry Points A: READIN: PROCEDURE; statement-1 statement-2 ERRT: ENTRY; statement-3 statement-4 statement-5 NEXT: RETR: ENTRY; statement-6 ... END READIN; In the example, A is the primary entry point. A and READIN specify the same entry point, as do NEXT and RETR. However note that depending on the entry point used to invoke the procedure, the code executed can be different. The procedure can be activated by any of the following statements:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-27 /154

CALL CALL CALL CALL CALL

A; ERRT; NEXT; RETR; READIN;

Entry Variables DECLARE ENT1 ENTRY VARIABLE; ENT1 = ERRT; CALL ENT1; /* this call and the next call have the same effect CALL ERRT; /* this call and the previous have the same effect Procedure Termination Normal procedure termination occurs when:

*/ */

Control reaches a RETURN statement within the procedure. The execution of a RETURN statement returns control to the point of invocation in the invoking procedure. If the point of invocation is a CALL statement, execution in the invoking procedure resumes with the statement following the CALL. If the point of invocation is one of the other forms of procedure references (that is, a CALL option of the INIT keyword or a function reference), execution of the statement containing the reference is resumed. Control reaches the END statement of the procedure. Effectively, this is equivalent to the execution of a RETURN statement. Transferring control out of a procedure using a GO TO statement can sometimes result in the termination of several procedures and/or begin-blocks. Specifically, if the transfer point specified by the GO TO statement is contained in a block that did not directly activate the block being terminated, all intervening blocks in the activation sequence are terminated. Example: A: PROCEDURE OPTIONS(MAIN) ; statement-1 statement-2 B: BEGIN; statement-b1 statement-b2 CALL C; statement-b3 END B; statement-3 statement-4 C: PROCEDURE; statement-c1 statement-c2 statement-c3 D: BEGIN;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL statement-d1 statement-d2 GO TO LAB; statement-d3 END D; statement-c4 LAB: END A; END C; statement-5 statement-6 statement-7

Page:-28 /154

A activates B, which activates C, which activates D. In D, the statement GO TO LAB transfers control to statement-6 in A. Since this statement is not contained in D, C, or B, all three blocks are terminated; A remains active. Thus, the transfer of control out of D results in the termination of intervening blocks B and C as well as the termination of block D. Dynamic Loading of an External Procedure Use FETCH and RELEASE statements PROGA: PROC OPTIONS(MAIN); DCL (C,D) FIXED BIN(31); DCL PROGB ENTRY(FIXED BIN(31),FIXED BIN(31)); FETCH PROGB; CALL PROGB(C,D); RELEASE PROGB; END PROGA; Example // JOB //DYNSTP EXEC PROC=IEL1CL,REGION.PLI=1M //PLI.SYSIN DD * PROGB : PROC(A,B); DCL (A,B) FIXED BIN(31); PUT LIST('A AND B:' ,A,B); END PROGB; /* //PLI.SYSLIN DD DSN=&&LOADSET,DISP=(NEW,KEEP),.......same parms as existing SYSLIN statement in IEL1CL proc //LKED.OBJMOD DD DSN=&&LOADSET,DISP=(OLD,DELETE) //LKED.SYSLMOD DD DSN=MYLIB.PLI.LOADLIB,DISP=OLD //LKED.SYSLIN DD * ENTRY PROGB INCLUDE OBJMOD NAME PROGB( R ) /* JCL for compile and link edit main proc. //.. JOB //COMPLEK EXEC IEL1CL //PLI.SYSIN DD *

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-29 /154

PROGA: PROC OPTIONS(MAIN); DCL (C,D) FIXED BIN(31); DCL PROGB ENTRY(FIXED BIN(31),FIXED BIN(31)); C=1;D=2; FETCH PROGB; CALL PROGB(C,D); RELEASE PROGB; END PROGA; /* //LKED.SYSLMOD DD DSN=MYLIB.PLI.LOADLIB(PROGA),DISP=SHR Executing main proc //STEP EXEC PGM=PROGA //STEPLIB DD DSN=MYLIB.PLI.LOADLIB,DISP=SHR Subroutines A subroutine is a procedure that is invoked by a CALL statement or CALL option of an INITIAL attribute. It can be either an external or an internal procedure. Example of Invocation of subroutines that are internal to and external to the invoking block. Example PRMAIN: PROCEDURE; DECLARE NAME CHARACTER (20), ITEM BIT (5), OUTSUB ENTRY; CALL OUTSUB (NAME, ITEM); END PRMAIN; OUTSUB: PROCEDURE (A,B); DECLARE A CHARACTER (20), B BIT (5); PUT LIST (A,B); END OUTSUB; Example A: PROCEDURE; DECLARE RATE FLOAT (10), TIME FLOAT (5), DISTANCE FLOAT (15), MASTER FILE; CALL READCM (RATE, TIME,DISTANCE, MASTER); READCM: PROCEDURE (W,X,Y,Z) ; DECLARE W FLOAT (10), X FLOAT (5), Y FLOAT (15), Z FILE; GET FILE (Z) LIST (W,X,Y); Y = W*X; IF Y > 0 THEN RETURN; ELSE PUT LIST (ERROR READCM); END READCM;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL END A;

Page:-30 /154

BUILTIN Subroutines These have entry name that are defined at compile-time and are invoked by a CALL statement. Functions A function is a procedure that is invoked by a function reference in an expression. A function reference is an entry reference that represents an entry name (a particular entry point of a procedure) invoked as a function. A function returns a value, and control, to replace the function reference in the evaluation of the expression in which the function reference appears. This single value can be of any data type except entry. Examples MAINP: PROCEDURE; GET LIST (A, B, C, Y); X = Y**3+SPROD(A,B,C);

. END MAINP; SPROD: PROCEDURE (U,V,W) RETURNS (BIN FLOAT (21)); DCL (U,V,W) BIN FLOAT (53); IF U > V + W THEN RETURN (0); ELSE RETURN ( U*V*W); END SPROD; Association of Arguments and Parameters When a function or subroutine is invoked, parameters in the parameter list are associated , from left to right, with the arguments in the argument list. The number of parameters and arguments must be the same. Dummy Arguments A reference to an argument, not its value, is generally passed to a subroutine or function. This is known as passing arguments by reference. However, this is not always possible or desirable. Constants, for example, should not be altered by an invoked procedure. Therefore, the compiler allocates storage (in storage belonging to the invoking procedure) for some arguments using attributes that agree with the parameter, converts, and assigns the argument to the allocated storage, and then passes a reference to the allocated storage. These storage locations are called dummy arguments. Any change to a parameter for which a dummy argument has been created is reflected only in the value of the dummy argument and not in the value of the original argument from which it was constructed. A dummy argument is created when the original argument is any of the following: A constant.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-31 /154

An expression with operators, parentheses, or function references. A variable whose data attributes or alignment attributes are different from the attributes declared for the parameter. This does not apply to simple parameters when only bounds, lengths, or size differ and, for the parameter, these are declared with asterisks. In the case of arguments and parameters with the PICTURE attribute, a dummy argument is created unless the picture specifications match exactly, after any repetition factors are applied. A controlled string or area (because an ALLOCATE statement could change the length or extent). Passing an Argument to the MAIN Procedure TOM: PROC (PARAM) OPTIONS (MAIN) ; DCL PARAM CHAR(100) VARYING; When NOEXECOPS is specified, the MAIN procedure can have a single parameter that is a VARYING CHARACTER string. The parameter is passed as is, and a descriptor is set up. (/, if contained in the string, is treated as part of the string). Example: MAIN: PROC(PARM) OPTIONS(MAIN NOEXECOPS); DCL PARM CHAR(n) VARYING; Note that the OPTION NOEXECOPS implies that the PARM string is not evaluated by the run environment of CMS or MVS but passed unchanged to the user program. Shows a MAIN procedure that can be invoked as follows: // EXEC PGM=MAIN, PARM=REPORT,LIST

The PARM contains REPORT,LIST and has a length of 11. When PL/I programs are called from Assembler routines, more than one parameter can be passed as below: Example MAIN: PROC(FUNC, P) OPTIONS(MAIN NOEXECOPS); DCL FUNC FIXED BIN(31); DCL P POINTER; Begin-Blocks A begin-block is a sequence of statements delimited by a BEGIN statement and a corresponding END statement. Example: B: BEGIN; statement-1 statement-2 . . statement-n END B;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL Unlike a procedure, a label is optional for a begin-block.

Page:-32 /154

Begin-block Activation Begin-blocks are activated through sequential flow or as a unit in an IF, ON, WHEN, or OTHERWISE statement. Control can be transferred to a labelled BEGIN statement by execution of a GO TO statement. Begin-Block Termination A begin-block is terminated when control passes to another active block by some means other than a procedure reference; that is, when: Control reaches the END statement for the block. When this occurs, control moves to the statement physically following the END, except when the block is an ON-unit. The execution of a GO TO statement within the begin-block (or any block activated from within that begin-block) transfers control to a point not contained within the block. A STOP or EXIT statement is executed (thereby terminating execution of the current task and all its sub-tasks). Control reaches a RETURN statement that transfers control out of the begin-block (and out of its containing procedure as well). A procedure within which the begin-block is contained has been attached as a task, and the attaching block terminates. A GO TO statement can also terminate other blocks if the transfer point is contained in a block that did not directly activate the block being terminated. In this case, all intervening blocks in the activation sequence are terminated. Entry Data Entry data can be an entry constant or the value of an entry variable. An entry constant is a name written as a label prefix to a PROCEDURE or ENTRY statement, or a name declared with the ENTRY attribute and not the VARIABLE attribute. An entry constant can be assigned to an entry variable. Example: P: PROCEDURE; DECLARE EV ENTRY VARIABLE, (E1,E2) ENTRY; EV = E1; CALL EV; EV = E2; CALL EV;

P, E1, and E2 are entry constants. EV is an entry variable. The following example contains a sub-scripted entry reference: DECLARE (A,B,C,D,E) ENTRY, DECLARE F(5) ENTRY VARIABLE

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-33 /154

INITIAL (A,B,C,D,E); DO I = 1 TO 5; CALL F(I) (X,Y,Z); END; /* the five entries A,B,C,D,E, are each invoked with parameters X,Y and Z

*/

Declaring Entry Data Internal entry constants are explicitly declared by the appearance of a label prefix to a PROCEDURE or ENTRY statement. A parameter-descriptor list (the number of parameters and their attributes) is obtained from the parameter declarations, if any, and by defaults. you must explicitly declare external entry constants. This declaration: Defines an entry point to an external procedure optionally specifies a parameter-descriptor list, if any, for the entry point Optionally specifies the attributes of the value that is returned by the procedure if the entry is invoked as a function. Examples of parameter descriptor list ENTRY (CHARACTER(10),,,FIXED DEC); /* 4 parameters */ ENTRY (*); /* indicates one parameter */ ENTRY(FLOAT BINARY, ); /*indicates 2 parameters */ ENTRY ( ); /* specifies entry has nil parms*/ ENTRY; /*Suppresses argument checks*/ Example TEST: PROCEDURE (A,B,C,D,E,F); DECLARE A FIXED DECIMAL (5), B FLOAT BINARY (15), C POINTER, 1 D, 2 P, 2 Q, 3 R FIXED DECIMAL, 1 E, 2 X, 2 Y, 3 Z, F(4) CHARACTER (10); END TEST; ENTRY for this could be declared as follows: DECLARE TEST ENTRY (DECIMAL FIXED (5), BINARY FLOAT (15), POINTER,

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL 1, 2, 2, 3 DECIMAL FIXED, 1, 2, 2, 3, (4) CHARACTER (10));

Page:-34 /154

BUILTIN Attribute Specifies that the name is a built-in function name, PSEUDOVARIABLE name, or built-in subroutine name A: PROCEDURE; DECLARE SQRT FLOAT BINARY; X = SQRT; /* programmer declared SQRT is assigned */ B: BEGIN; DECLARE SQRT BUILTIN; Z = SQRT(P); /* internal SQRT function is called*/ END B; END A;

GENERIC Attribute and References A generic name is declared with the GENERIC attribute, and specifies a set of entry references. Example DECLARE CALC GENERIC ( FXDCAL WHEN (FIXED,FIXED), FLOCAL WHEN (FLOAT,FLOAT), MIXED WHEN (FLOAT,FIXED)); Z = X+CALC (X,Y); Depending on the argument attributes, the matching entry point is used. In a similar manner, an entry point to a procedure can be selected on the basis of the dimension. Example: DCL D GENERIC ( D1 WHEN ((*)), D2 WHEN ((*,*))), A(2), B(3,5); CALL D(A); /* entry D1 */ CALL D(B); /* entry D2 */

RETURN Statement The RETURN statement terminates execution of the procedure that contains the RETURN statement. RETURN (expression);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-35 /154

The RETURN statement with expression is used to terminate a procedure invoked by a function reference.

SECTION 5

SUBROUTINES AND FUNCTIONS

Subroutines and Procedures Main Procedures (entry point into program) Subroutine Procedures Function Procedures Procedures can be nested (unlike C and like Pascal) Arguments can be passed to procedures Function procedures can return value (like C functions) Subroutine procedures return value(s) through modification of argument(s) passed Invoke a procedure via a CALL statement Example: CALL SUBRT(A,B,C); Subroutines compiled separately are called EXTERNAL procedures and their name is limited to seven characters. (Although MVS supports eight, the last position is used by the PL/I compiler for a suffix to segregate code from one external procedure into more than one CSECT). A STOP or EXIT routine in a procedure stops or exits the whole run unit. Example: MAINPR: PROCEDURE OPTIONS(MAIN); DCL X FIXED DEC(7,2); DCL Y FIXED DEC(7,2); DCL Z FIXED DEC(8,2); DCL SUBRT ENTRY; /* declares SUBRT as a procedure*/ GET LIST (X,Y); CALL SUBRT(X,Y,Z); PUT LIST('THE SUM IS =',Z); END MAINPR; SUBRT: PROCEDURE(A,B,C); DCL A FIXED DEC(7,2); DCL B FIXED DEC(7,2); DCL C FIXED DEC(8,2); C = A + B; END SUBRT; An argument can be one of the following Variable Constant Expression Array name Array expression Major structure name Minor structure name Structure expression Built in function name Entry name (can be an Entry Variable or a Label variable) File name Example:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-36 /154

CALL SUBRT('ABCD',VAR_1); CALL SUBRT(VAR_2,SQRT(X)); /* since built in function has arguments /* it is invoked before SUBRT is called */ CALL SUBRT(A,DATE,C); /* DATE is NOT called before SUBRT */ CALL SUBRT(A,(DATE),C); /* DATE is called before SUBRT */ DAY = SUBSTR(DATE,5,2); /* since DATE is argument to call to another built in function it IS invoked before SUBSTR is called */

When a constant is passed as an argument, internally a dummy argument is generated to handle this argument Example: CALL SUBRT(5.7,X); Note that the dummy argument will have attribute FIXED(2,1) whereas in SUBRT the first argument was FIXED(7,2). This can cause error. To avoid this pitfall code as below Example: DCL ARG_1 FIXED DECIMAL(7,2); ARG_1 = 5.7; CALL SUBRT(ARG_1,X); Alternately declare the subroutine procedure as below Example: DCL SUBRT ENTRY(FIXED FIXED FIXED Alternately DCL SUBRT ENTRY(FIXED DECIMAL(7,2), DECIMAL(7,2), DECIMAL(8,2)); DECIMAL(7,2), FIXED DECIMAL(7,2) , );

/* note the absence of third parameter by the comma. This is required as the third parameter has no value initially */ A dummy argument is generated under following conditions If an argument is a constant If an argument is an expression involving operators If an argument is an expression in parenthesis If an argument is a variable whose attributes are different from those the declared for the parameter in the ENTRY name attribute specification appearing in the invoking block If an argument is itself a function reference containing arguments Note that any modification made to a parameter which is represented by a dummy argument is not reflected in the original parameter. Function Procedures Return statement is used to terminate a function RETURN(element-expression); Example : W = CALC(A,B);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-37 /154

MAINPR: PROCEDURE OPTIONS(MAIN); DCL CALC ENTRY RETURNS(FIXED DEC(7)); DCL SUM FIXED DECIMAL(7); DCL A FIXED DECIMAL(7); DCL B FIXED DECIMAL(7); DCL C FIXED DECIMAL(7); GET LIST(A,B,C); SUM = CALC(A,B,C); PUT LIST('SUM IS',SUM); END MAINPR; CALC: PROCEDURE (X,Y,Z) RETURNS(FIXED DECIMAL(7)); DCL X FIXED DECIMAL(7); DCL Y FIXED DECIMAL(7); DCL Z FIXED DECIMAL(7); RETURN(X+Y+Z); END CALC; Scope of Identifiers P1:PROCEDURE; DCL X CHAR(2); DCL Y CHAR(2); DCL Z CHAR(2); DCL SUM FIXED DEC(7); . . CALL P2; . . P2:PROCEDURE; DCL A CHAR(2); DCL B CHAR(2); DCL C CHAR(2); DCL SUM CHAR(7); /* this hides the SUM in P1 */ X = 'AB'; /* Parameters in outer block can be accessed */ . . END P2; END P1; Note: X,Y and Z are known to P1.However A,B and C are not known to P1 A,B and C come into existence only when P2 is invoked X,Y,Z,A,B and C are all known to P2. Inner blocks can see out. Outer blocks cannot see inwards. Any data item with the EXTERNAL attribute tells the compiler that this variable is known outside this module and is to be allocated once in the Global data area. However all other modules which refer to this variable must declare this with same name and attributes and EXTERNAL attribute. Classification of Built In Functions

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-38 /154

To aid in their description, built-in functions are listed in classes below. The first four classes are computational built-in functions. String-handling Arithmetic Mathematical Array-handling Condition-handling Storage control Event Input / Output Miscellaneous String-Handling Built In Functions The string-handling built-in functions simplify the processing of bit and character strings. They are: BIT BOOL CHAR GRAPHIC HIGH INDEX LENGTH LOW MPSTR REPEAT STRING SUBSTR TRANSLATE UNSPEC VERIFY

Arithmetic Built In Functions The arithmetic built-in functions allow you to: 1. Control conversion of base, scale, mode, and precision both directly and during basic arithmetic operations. 2. Determine properties of arithmetic values. For example, the SIGN function indicates the sign of an arithmetic value. They are: ABS ADD BINARY CEIL COMPLEX CONJG DECIMAL DIVIDE FIXED FLOAT FLOOR IMAG MAX MIN MOD MULTIPLY PRECISION REAL ROUND SIGN TRUNC

Mathematical Built In Functions The mathematical built-in functions provide mathematical operations. They are: ACOS COSD LOG SINH ASIN COSH LOG2 SQRT ATAN ERF LOG10 TAN ATAND ERFC SIN TAND ATANH EXP SIND TANH All of these functions operate on floating-point values to produce a floating-point result. Array-Handling Built In Functions The array-handling built-in functions operate on array arguments and return an element value . They are: ALL LBOUND ANY POLY

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL DIM HBOUND PROD SUM

Page:-39 /154

Condition-Handling Built In Functions The condition-handling build-in functions allow you to investigate the cause of enabled conditions. They are: DATAFIELD ONCHAR ONCODE ONCOUNT ONFILE ONKEY ONLOC ONSOURCE

Use of these functions is in context when within the scope of an ON-unit entered for the condition specific to the built-in function, or within an ON-unit for the ERROR or FINISH condition when raised as an implicit action. All other uses are out of context. Storage Control Built In Functions The storage-control built in functions allow you to determine the storage requirements and location of variables, to assign special values to area and locator variables, to perform conversion between offset and pointer values, and to obtain the number of generations of a controlled variable. They are: ADDR ALLOCATION BINARYVALUE CURRENTSTORAGE EMPTY ENTRYADDR NULL OFFSET POINTER POINTERADD POINTERVALUE STORAGE SYSNULL

Input / Output Built In Functions The Input / Output built-in functions allow you to determine the current state of a file. They are: COUNT LINENO SAMEKEY Miscellaneous Built In Functions The built-in functions that do not fit into any of the foregoing classes are: DATE DATETIME PLIRETV TIME PLIRETC Built IN Subroutines The PL/I built-in subroutines are the following; PLICANC PLISRTC PLICKPT PLISRTA PLIDUMP PLISRTB

PLISRTD PLITEST

Pseudo-variables Pseudo-variables represent receiving fields. Except when noted in the description, the Pseudo-variables : Can appear on the left of the assignment symbol in an assignment.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-40 /154

Can appear in a data list of a GET statement or in the STRING option of a PUT statement. Can appear as the string name in a KEYTO option. The Pseudo-variables are: COMPLETION COMPLEX ENTRYADDR IMAG ONCHAR ONSOURCE PRIORITY REAL STATUS STRING SUBSTR UNSPEC

ADDR (Storage control) ADDR returns the pointer value that identifies the generation of x. The syntax for ADDR is: ADDR (X) Reference to variable of any data type, ALL (Array-Handling) ALL returns a bit string in which each bit is 1 if the corresponding bit in each element of X exists and is 1. The length of the result is equal to that of the longest element. The syntax for ALL is: ALL (X) /* Where X is an Array expression */ If X is not a bit-string array, it is converted to bit string. ALLOCATION (Storage Control) ALLOCATION returns a FIXED BINARY (31,0) value specifying the number of generations that can be accessed in the current task for X. The syntax for ALLOCATION is: ALLOCATION (X) ANY (Array-handling) ANY returns a bit string in which each bit is 1 if the corresponding bit in any element of X exists and is 1. The length of the result is equal to that of the longest element . The syntax for ANY is: ANY (X) X is an Array expression. BINARYVALUE (Storage Control) BINARYVALUE returns a REAL FIXED BIN (31,0) value that is the converted value of its pointer expression, X. The syntax for BINARYVALUE is: BINARYVALUE (X) BIT (String) BIT returns the value of X with length specified by Y where Y is FIXED BINARY(15) The syntax is BIT (X, Y)

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-41 /154

CHAR (String-Handling) CHAR returns the character value of X, with a length specified by Y which is FIXED BINARY(15). The syntax for CHAR is: CHAR (X, Y) COUNT (Input / Output) COUNT returns a FIXED BINARY (15,0) value specifying the number of data items transmitted during the last GET or PUT operation on X. The syntax for COUNT is: COUNT (X) X is a File-reference. The file must be open and have the STREAM attribute. CURRENTSTORAGE (Storage Control) CURRENTSTORAGE returns a FIXED BINARY (31,0) value giving the implementationdefined storage in bytes, required by X . The syntax for CURRENTSTORAGE is: CURRENTSTORAGE (X) DATAFIELD (Condition-Handling) DATAFIELD is in context in a NAME condition ON-unit (or any of its dynamic descendants), and returns a character string whose value is the contents of the field that raised the condition. DATAFIELD ( ) DATE (Miscellaneous) DATE returns a character string, length 6, in the format YYMMDD. The syntax for DATE is: DATE ( ) The returned character string represents: YY last two digits of the current year MM Current month DD Current day DATETIME (Miscellaneous) DATATIME returns a character string, length 17, in the format of YYYYMMDDHHMMSSTTT. The syntax for DATATIME is: DATETIME ( ) The returned character string represents: YYYY MM DD HH MM SS TTT Current year Current month Current day Current hour Current minute Current second Current millisecond

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-42 /154

DECIMAL (Arithmetic) DECIMAL returns the decimal value of X, with a precision specified by P and Q . The result has the mode and scale of X. The syntax for DECIMAL is: DECIMAL (X, P, Q) DIM (Array-Handling) DIM returns a FIXED BINARY (31,0) value specifying the current extent of the dimension Y of X . The syntax for DIM is: DIM (X, Y) X Array expression. X must not have less than Y dimensions, and X must not be an array of structures. Y Expression specifying a particular dimension of X. If necessary, Y is converted to a FIXED BINARY (31,0) value. Y must be greater than or equal to 1 If the extent of an array dimension exceeds the allowable number for the implementation, the DIM function returns an undefined value. EMPTY (Storage Control) EMPTY returns an area of zero extent. It can be used to free all allocations in an area. The syntax for EMPTY is: EMPTY ( ) Example DECLARE A AREA, I BASED (P); J BASED (Q); ALLOCATE I IN (A), J IN (A); A = EMPTY( ); /*equivalent to: FREE I IN (A), J IN (A);*/ ENTRYADDR (Storage Control) ENTRYADDR returns a pointer value that is the address of the first executed instruction if the entry X is invoked . The entry X must be an external entry. The syntax for ENTRYADDR is: ENTRYADDR (X) X Entry reference. ENTRYADDR (Pseudo-variable) The ENTRYADDR Pseudo-variable initialises an entry variable X, with the address of the entry to be invoked. The syntax for ENTRYADDR PSEUDOVARIABLE is: ENTRYADDR (X) X Entry reference HBOUND (Array-Handling)

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-43 /154

HBOUND returns a FIXED BINARY (31,0) value specifying the current upper bound of dimension Y of X. The syntax for HBOUND is: HBOUND (X, Y) X Array expression. X must not have less than Y dimensions, and X must not be an array of structures. Y Expression specifying a particular dimension of X. If necessary Y is converted to a FIXED BINARY (15,0) value. Y must be greater than or equal to 1 INDEX (String-Handling) INDEX returns a FIXED BINARY (15,0) value indicating the starting position within X of a sub-string identical to Y. The syntax for INDEX is: INDEX (X, Y) X String-expression to be searched. Y String-expression to be searched for. if Y does not occur in X, or if either X or Y have zero length, the value zero is returned If occurs more than once in X, the starting position of the leftmost occurrence is returned. LBOUND (Array-Handling ) LBOUND returns a FIXED BINARY (31,0) value specifying the current lower bound of dimension Y of X. The syntax for LBOUND is: LBOUND (X, Y) X Array expression, X must not have less than Y dimensions, and X must not be an array of structures. Y Expression specifying the particular dimension of X. If necessary Y is converted to a FIXED BINARY (15,0) value . Y must be greater than or equal to 1 LENGTH (String-Handling) LENGTH returns a FIXED BINARY (15,0) value specifying the current length of X. The syntax for LENGTH is: LENGTH (X) X String -expression. If X is binary it is converted to bit string; otherwise any other conversion required is to character string. LINENO ( Input / Output) LINENO returns a FIXED BINARY (15,0) value specifying the current line number of X. The syntax for LINENO is: LINENO (X) X File-reference. The file must be open and have the PRINT attribute. NULL (Storage Control) NULL returns the null pointer value . The null pointer value does not identify any generation of a variable The null pointer value can be converted to OFFSET by assignment of the built-in function value to an offset variable. The syntax for NULL is: NULL ( )

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-44 /154

OFFSET (Storage Control) OFFSET returns an offset value derived from a pointer reference X and relative to an area Y. If X is the null pointer value, the null offset value is returned. The syntax for OFFSET is: OFFSET (X, Y) X Pointer reference, which must identify a generation of a based variable within the area Y, or be the null pointer value. Y Area reference. ONCHAR (Condition-Handling) ONCHAR returns a character string of length 1, containing the character that caused the CONVERSION condition to be raised. It is in context in an ON-unit (or any of its dynamic descendants) for the CONVERSION condition or for the ERROR or FINISH condition raised as the implicit action for the CONVERSION condition. The syntax for ONCHAR is: ONCHAR ( ) ONCHAR ( Pseudo-variable ) The Pseudo-variable sets the current value of the ONCHAR built-in function. The element value assigned to the pseudo-variable is converted to a character value of length 1. The new character is used when the conversion is re-attempted. ONCHAR ( ) ONCODE (Condition-Handling) ONCODE returns FIXED BINARY (15,0) value that is the condition code. It is in context in any ON-unit, or any dynamic descendant of an ON-unit. ONCODE ( ) ONFILE (Condition-Handling) ONFILE returns a character string whose value is the name of the file for which an input/output or CONVERSION condition is raised. ONFILE ( ) ONLOC (Condition-Handling) ONLOC returns a character string whose value is the name of the entry-point used for the current invocation of the procedure in which a condition was raised. It is in context in any ON-unit, or in any of its dynamic descendants. ONLOC ( ) ONSOURCE (Condition - Handling) ONSOURCE returns a character string whose value is the contents of the field that was being processed when the CONVERSION condition was raised. It is in context in an ONunit, or any of its dynamic descendants, for the CONVERSION condition or for the ERROR or FINISH condition raised as the implicit action for the CONVERSION CONDITION. The syntax for ONSOURCE is:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL ONSOURCE ( )

Page:-45 /154

ONSOURCE (Pseudo-variable) The pseudo-variable sets the current value of the ONSOURCE built-in function. The element value assigned to the pseudo-variable is converted to a character string and, if necessary, is padded on the right with blanks or truncated to match the length of the field that raised the CONVERSION condition. The new string is used when the conversion is re-attempted. ONSOURCE ( ) PLIDUMP (Built-in Subroutine) This built-in subroutine allows you to obtain a formatted dump of selected parts of storage used by your program. For the syntax of the PLIDUMP and other details lookup Language Environment for MVS and VM Debugging Guide and Run Time Messages. PLIRETC (Built-in Subroutine) This built-in subroutine allows you to set a return code that can be examined by the program or (sub) system that invoked this PL/I program or by another PL/I procedure via the PLIRETV built-in function. The syntax for PLIRETC is: PLIRETC (return-code) Example: CALL PLIRETC(16); /* sets return code of 16 */ PLIRETV (Miscellaneous) PLIRETV returns a FIXED BINARY (31,0) value that is the PL/I return code. The syntax for PLIRETV is: PLIRETV ( ) The value of the PL/I return code is the most recent value specified by a CALL PLIRETC statement in any task or the value returned by a COBOL or assembler routine (via Register 15) whose entry point is declared with the option OPTIONS(RETCODE), or zero. POINTER (Storage Control) POINTER returns a pointer value that identifies the generation specified by an offset reference X, in an area specified by Y. If X is the null offset value, the null pointer value is returned. The syntax for POINTER is: POINTER (X, Y) X Y Offset reference, which can be the null offset value; if it is not, it must identify a generation of a based variable. Area reference.

REPEAT (String-Handling) REPEAT returns a bit or character string consisting of X concatenated to itself the number of times specified by Y; that is, there are (Y+1) occurrences of X. The syntax for REPEAT is:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-46 /154

X Y

REPEAT (X, Y) Bit or character expression to be repeated. if X is arithmetic, it is converted to bit string if it is binary, character string if it is decimal. Expression. If necessary, y is converted to a FIXED BINARY ( 15,0) if Y is zero or negative, the string X is returned.

STORAGE (Storage Control) STORAGE returns a FIXED BINARY (31,0) value giving the implementation - defined storage, in bytes, allocated to a variable X. The syntax for STORAGE is: X STORAGE ( X ) A variable of any data type, data organisation, alignment

STRING (String-Handling) STRING returns an element bit or character string that is the concatenation of all the elements of X. The syntax for STRING is: STRING ( X ) X Aggregate or element reference. Each base element of X must be either all bit-string, or all character string and/or numeric character, in any combination. If X is a structure that has padding caused by ALIGNED elements, the padding is not included in the result. If any of the strings in the aggregate X are of varying length, only the current length, not including the 2-byte length prefix, is concatenated. if X is an element variable, the rules for aggregates apply except that there is no concatenation.

SUBSTR (String-Handling) SUBSTR returns a sub-string, specified by Y and Z, of X. The syntax for SUBSTR is: SUBSTR ( X, Y, Z) X String-expression from which the sub-string is to be extracted. If X is not a string, it is converted to a bit string if binary, or a character string if decimal. Expression that can be converted to a FIXED BINARY (15,0) value specifying the starting position of the sub-string in X. Expression that can be converted to a value specifying the length of the sub-string in X. If Z is zero, a null string is returned. if Z is omitted, the sub-string returned is position Y in X to the end of x.

Y Z

The STRINGRANGE condition is raised if Z is negative or if the values of Y and Z are such that the sub-string does not lie entirely within the current length of X; it is not raised when Y = LENGTH(X)+1 and Z=0 or Z is omitted. SUBSTR (Pseudo-variable) The pseudo-variable assigns a string value to a sub-string, specified by Y and Z, of X. The remainder of X is unchanged. (Assignments to a varying string do not change the length of the string.) The syntax for SUBSTR pseudo-variable is:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-47 /154

SUBSTR (X,Y,Z) = .. X Y Z String-reference. X must not be a numeric character. Expression that can be converted to a FIXED BINARY (15,0) value specifying the starting position of the sub-string in X. Expression that can be converted to a FIXED BINARY (15,0) value specifying the length of the sub-string in X. If Z is zero, a null string is returned. If Z is omitted, the sub-string returned is position Y in X to the end of X. Y and Z can be arrays only if X is an array.

SUM (Array-Handling) SUM returns the sum of all the elements in X. The base, mode, and scale of the result match those of X. The syntax for SUM is: X SUM ( X ) Array expression. If the elements of x are strings, they are converted to fixed-point integer values.

SYSNULL (Storage Control) SYSNULL returns the system null pointer value. It can be used to initialise static pointer and offset variables. It also can be assigned or converted to offset variables (like NULL). The Syntax for SYSNULL is : SYSNULL ( ) TIME(Miscellaneous) TIME returns a character string, length 9, in the format of HHMMSSTTT. The syntax for TIME is: TIME ( ) The returned character string represents: HH Current hour MM Current minute SS Current second TTT Current millisecond TRASLATE (String-Handling) TRANSLATE returns a character string of the same length as X. The Syntax for TRANSLATE is: TRANSLATE (X, Y, Z) X Y Z Character expression to be searched characters. for possible translation of its

Character expression containing the translation values of characters. Character expression containing the characters that are to be translated. If Z is omitted, a string of 256 characters is assumed; it contains one

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-48 /154

instance of each EBCDIC code arranged in ascending collating sequence (hexadecimal 00 through FF) TRANSLATE operates on each character of X as follows. If a character in X is found in Z, the character in Y that corresponds to that in Z is copied to the result; otherwise, the character in X is copied directly to the result. if Z contains duplicates, the leftmost occurrence is used. Y is padded with blanks, or truncated, on the right to match the length of Z. Any arithmetic or bit arguments are converted to character. Example: DECLARE (W, X) CHAR (3); X = ABC; W = TRANSLATE (X, TAR, DAB); /* W = ARC */ VERIFY (String- Handling) VERIFY returns a FIXED BINARY (15,0) value indicating the leftmost character or bit position in X that is not in Y. If all the characters or bits in X do appear in Y, a value of zero is returned. If X is the null string, a value of zero is returned. If X is not the null string and Y is the null string, a value of one is returned. The syntax for VERIFY is: VERIFY (X, Y,) X String Expression Y String expression . If either argument is character or decimal, conversions are performed to produce character string. Otherwise, if the arguments are bit and binary or both binary, and conversions are performed to produce bit strings. In the following example, the VERIFY built-in function is used to test whether or not a string is all-numeric: VERIFY (X, 0123456789).

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-49 /154

SECTION 6
Logical Testing Symbols GE or >= GT or > NE or = LT or < LE or <= NL or NG or Operations Greater than or equal to Greater than Not equal to Equal to Less than Less than or equal to Not less than Not greater than

CONTROL STATEMENTS

IF logical expression THEN statement ; ELSE statement; IF logical expression THEN DO; . . END; ELSE DO; . . END; Nested IF statement IF A=B THEN IF A=C THEN X=1; ELSE X=2; ELSE X=3; Null IF IF A=B THEN IF A=C THEN X=1; ELSE; ELSE X=3; Select Statement SELECT (expression which is optional); WHEN (expression) action 1; WHEN (expression) action 2; WHEN (expression) action 3; IF A=B THEN IF A=C THEN X=1; ELSE X=3;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL OTHERWISE action 3; END; Example: SELECT (CODE) WHEN (1) DO; . . END; WHEN (2) CALL SUBRTN; OTHERWISE CALL INVCODE; END; SELECT; WHEN(CODE<10) DO; . . END; WHEN (CODE>=10) CALL SUBRTN; OTHERWISE CALL ERRORTN; END; Logical Operators NOT & | AND OR

Page:-50 /154

Note that the | or can be changed by the %PROCESS statement like below %PROCESS NOT('~'); /* ~ becomes the not operator */ IF A=B & C=D THEN CALL SUBRTN; Logical Operators in the assignment statement A=B=C; /*If B=C then a value of 1 is assigned to A else 0 is assigned */ A=B>C; /* If B>C then a value of 1 is assigned to A else 0 is assigned*/ Example of capturing PARM, SELECT statement and IF statements //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M,PARM.GO='E' //PLI.SYSIN DD * %PROCESS ATTRIBUTES(FULL),XREF(FULL); MYPROG: PROCEDURE(A) OPTIONS(MAIN); DCL A CHAR(20) VARYING; PUT SKIP EDIT('PARM PASSED IS=',A)(A); SELECT(A); WHEN('A') PUT SKIP LIST('PARM PASSED IA A'); WHEN('B') PUT SKIP LIST('PARM PASSED IA B'); WHEN('C') PUT SKIP LIST('PARM PASSED IA C'); WHEN('D') PUT SKIP LIST('PARM PASSED IA D'); OTHERWISE PUT SKIP LIST('UNKNOWN CHAR PASSED'); END;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL IF A = 'A' THEN PUT SKIP LIST('CHAR IS A'); ELSE IF A='B' THEN PUT SKIP LIST('CHAR IS B'); ELSE IF A='C' THEN PUT SKIP LIST('CHAR IS C'); ELSE IF A='D' THEN PUT SKIP LIST('CHAR IS D'); ELSE PUT SKIP LIST('UNKNOWN CHAR'); END MYPROG; /* //

Page:-51 /154

DO LOOPS DO UNTIL(expression) /* test is after the loop body */ /* loop terminates when the condition becomes true */ DO WHILE(expression) /* test is before the loop body */ /* loop terminates when the condition becomes false*/ DO UNTIL (expression); . . END; Expression can be a logical expression or a flag variable (BIN (1)). A flag variable with a value of binary one is true. A binary value of zero is false. Iterative DO loop DO control-variable = initial value TO limit value BY modification value [WHILE(expression) | UNTIL(expression)]; Example: J=10; K=5; L=2; DO I=J TO K BY L; . . END; Example(s): DO I=1 TO 100 BY 1; DO I=100 TO 1 BY -1; DO I=1 BY 1; /*termination condition must be inside the loop*/ /* DO I=1 BY 1 TO 100 is also OK */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL DO I=0.1 BY 0.1 TO 1.0; DO I=1 TO 5,10 TO 20, 25 TO 50 BY 2; DO I=1,4,7,22;

Page:-52 /154

DO I=1 TO 100 WHILE(X<100); /* loop terminates when I reaches 100 or >=100*/ Commas separate specifications as below DO I=1 TO 10, 11 BY 0 WHILE(A=B); /* after I = 1 to 10, continuos looping at I = 11 takes place while A=B. At this time loop terminates only when A is not = B */ DO I='ABC','DEF','GHI' Leave statement The leave statement transfers control from within a DO loop to the statement following the end statement that delimits the group and terminates the DO group. Example A: DO; . . LEAVE; /* transfers control to the next statement after this block*/ . END; next statement; Example A: DO I =1 to 10; DO J = 1 to 10; IF X(I,J)=0 THEN LEAVE A; /* to statement outside group A ELSE.. . END; END; statement after group A.

*/

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-53 /154

SECTION 7
Condition and ON-UNITS condition occurs

CONDITIONS AND ON UNITS

action (null operation)

WHAT system ACTION ? action

no

Program specified action In absence of program specified action default system action takes place. ERROR condition is raised and the program is terminated. Two built-in functions exist, ONCODE (which returns the error code, binary value, for diagnostic purposes) and ONLOC which returns the procedure/function name, as a string, where the error occurred. Note that these built-in functions do not have any arguments and should therefore be declared before use as below DCL ONCODE BUILTIN;

Standard error handling routines can be kept in source form in a source statement library and included in your program by the statement %INCLUDE book_name

ON units can be declared to handle specific conditions. Within an ON unit facilities are provided to get more information on the condition that caused the ON unit to be activated. Some of these are ONCODE( ), ONSOURCE( ), ONCHAR( ). A normal return from an ON unit is said to take place when execution returns through END statement. An abnormal termination is said to occur when we branch out of the ON unit through a GOTO statement. ON condition BEGIN; . . END; Example: ON ENDFILE(SYSIN) BEGIN . . END; Example:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-54 /154

ON ERROR SYSTEM; /* restores back system action for ERROR condition*/ ON ERROR BEGIN; ON ERROR SYSTEM; PUT DATA; /* output all variables and values */ END; Condition Handling Condition Prefixes You can specify whether or not some conditions are enabled or disabled. If a condition is enabled, the raising of the condition executes an action. If a condition is disabled, the raising of the conditions does not execute an action. Enabling and disabling can be specified for the eligible conditions by a condition prefix. Example: (SIZE): L1 X=(I**N); The conditions that are always enabled unless they are explicitly disabled by condition prefixes are: CONVERSION FIXEDOVERFLOW OVERFLOW UNDERFLOW ZERODIVIDE Each of the preceding conditions can be disabled by condition prefix specifying the condition name preceded by NO with intervening blanks. Thus, one of the following in a condition prefix disables the respective condition: NOCOVERSION NOFIXEDOVERFLOW NOOVERFLOW NOUNDERFLOW NOZERODIVIDE The conditions that are always disabled unless they are enabled by a condition prefix are: SIZE SUBSCRIPTRANGE STRINGRANGE STRINGSIZE All other conditions are always enabled and cannot be disabled . These conditions are: AREA ATTENTION CONDITION ENDFILE ENDPAGE ERROR FINISH KEY NAME RECORD TRANSMIT UNDEFINEDFILE PENDING

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-55 /154

Scope of the Condition Prefix A condition prefix attached to a PROCEDURE or BEGIN statement applies to all the statements up to and including the corresponding END statement. This includes other PROCEDURE or BEGIN statements nested with that block. The scope of a condition prefix applied to a DO or SELECT statement is limited to execution of the statement itself; it does not apply to execution of the entire group. ON Statement The ON statement establishes the action to be executed for any subsequent raising of an enabled condition in the scope of the established action. ON condition [SNAP] [SYSTEM] | on-unit SNAP Specifies that when the enabled condition is raised, a list is printed of all the blocks and ON-units active in the current task at the time the condition is raised. The action of the SNAP option precedes the action of the ON-unit. SYSTEM Specifies that the implicit action is taken. The implicit action is not the same for every condition, although for most conditions a message is printed and the ERROR condition is raised. on-unit Specifies the action to executed when the condition is raised and is enabled. The action is defined by the statement or statements in the ON-unit itself. The On-unit is not executed at the time the ON statement is executed; it is executed only when the specified enabled condition is raised. null on-Unit The effect of a null statement on-unit is to execute normal return from the condition. Use of the null on-unit is not the same as disabling, for two reasons: A null ON-unit can be specified for any condition, but not all conditions can be disabled. Disabling a condition, if possible, can save time by avoiding any checking for this condition. (If a null ON-unit is specified, the system must still check for the raising of the condition). The execution of an ON statement establishes an action specification for a condition. Once this action is established, it remains established throughout that block and throughout all dynamically descendent blocks until it is overridden by the execution of another ON statement or a REVERT statement or until termination of the block in which the ON statement is executed.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL Example for REVERT:PROC1: PROCEDURE; ON CONVERSION BEGIN; END; B: PROC2; ON CONVERSION BEGIN; END; . . REVERT CONVERSION; END PROC2; END PROC1;

Page:-56 /154

/* Overrides ON unit in PROC1 */

/* Now the ON unit in PROC1 is back in control */

Note: Dynamic descendency refers to the fact that ON-units are inherited from the calling procedure in all circumstances. Dynamic descendancy is often not known until run time, since a procedure can be called from anywhere where it is visible. To prevent an ERROR condition raised in an ERROR ON-unit from executing the same ERROR ON-unit, thus raising the ERROR condition again and causing a loop, the following technique can be used: ON ERROR BEGIN; ON ERROR SYSTEM; . . . END; Example DCL F FILE, G FILE VARIABLE; G =F; L1: ON ENDFILE (G); L2: ON ENDFILE (F); The statements labelled L1 and L2 are equivalent. Example DECLARE FV FILE VARIABLE, FC1 FILE, FC2 FILE; FV = FC1; ON ENDFILE (FV) GO TO FIN; /* ON unit for FC1 . . .

*/

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL FV =FC2; READ FILE (FC1) INTO (X1); READ FILE (FV) INTO (X2);

Page:-57 /154

An ENDFILE condition raised during the first READ statement causes the on-unit to be entered, since the on-unit refers to file FC1. If the condition is raised in the second READ statement, however, the on-unit is not entered, since this READ refers to file FC2. SIGNAL Statement You can raise a condition by means of the SIGNAL statement. This statement can be used in program testing to verify the action of an ON-unit and to determine whether the correct action is associated with the condition. The established action is taken unless the condition is disabled. If the specified condition is disabled, the SIGNAL statement becomes equivalent to a null statement. The syntax for the SIGNAL statement is: SIGNAL Condition; Classification of Conditions The conditions are classified as follows: Computation conditions-those associated with data handling, expression evaluation, and computation. The conditions are: CONVERSION SIZE FIXEDOVERFLOW UNDERFLOW OVERFLOW ZERODIVIDE If a computational conditional (except UNDERFLOW) is raised and the condition is disabled, the program is in error; Input/output conditions-those conditions associated with input and output. They are: ENDFILE ENDPAGE KEY TRANSMIT UNDEFINEDFILE NAME RECORD ENDFILE KEY UNDEFINEDFILE ENDPAGE TRANSMIT NAME RECORD When end of file is reached Incorrect KEY for a keyed data set File could not be opened End of page on PRINT file I/O error On GET DATA when incorrect items in input Typically when buffer is too small for record For read. For write of fixed length records it Can occur if buffer is larger than record size.

Program-checkout conditions-those conditions that facilitate the debugging of a program. They are STRINGSIZE STRINGRANGE

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-58 /154

SUBSCRIPTRANGE If SUBSCRIPTRANGE is raised is disabled, the program is in error. Because this checking involves a substantial overhead in both storage space and run time, it usually is used only in programs testing. It is removed for production programs, because the above are normally disabled conditions. Miscellaneous conditions, which are: AREA ERROR ATTENTION FINISH CONDITION AREA Condition The AREA condition is raised in either of the following circumstances: When an attempt is made to allocate a based variable within an area that contains insufficient free storage for the allocation to be made. When an attempt is made to perform an area assignment, and the target area contains insufficient storage to accommodate the allocations in the source area. The syntax for AREA is: ON AREA BEGIN; . END; Result: In both cases the attempted allocation or assignment has no effect. Implicit Action: A message is printed and the ERROR condition is raised. Status: AREA is always enabled. Normal Return 1) If the condition was raised by an allocation and the on-unit is a NULL on-unit the allocation is not attempted again. 2) If the on-unit is not NULL then the allocation is attempted again provided the pointer qualifying the reference to the AREA has been changed 3) If the condition was raised by an area assignment or by a SIGNAL statement, the execution continues from the next statement.

CONDITION Condition The CONDITION condition is raised by a SIGNAL statement that specifies the appropriate name. The name specified in the SIGNAL statement determines which CONDITION condition is to be raised. The syntax for CONDITION is: ON CONDITION (name) Abbreviation: COND The CONDITION condition allows you to establish an ON-unit that is executed whenever a SIGNAL statement is executed specifying CONDITION and that name. As a debugging aid, this condition can be used to establish an ON-unit whose execution results in printing information that shows the current status of the program. The On-unit

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-59 /154

can be executed from any point in the program through placement of SIGNAL statement. Of course, normal rules of name scope apply; Following is an example of how the CONDITION condition might be included in a program: DCL TEST CONDITION; /* declare the TEST condition */ ON CONDITION (TEST) BEGIN; /*Now set up the ON unit */ . . . END; The begin-block is executed whenever the following statement is executed: SIGNAL CONDITION (TEST); Implicit Action: A message is printed and execution continues with the statement following SIGNAL. Status: CONDITION is always enabled. Normal Return: Execution continues with the statement following the SIGNAL statement. CONVERSION Condition The CONVERSION computational condition is raised whenever an invalid conversion is attempted on character data. A character other than 0 or 1 exists in character data being converted to bit data. A character value being converted to a numeric character field, or to coded arithmetic, contains characters which are not the representation of an optionally signed arithmetic constant, or an expression to represent a complex constant . A value being converted to a character pictured item contains characters not allowed by the picture specification. The syntax for CONVERSION is; ON CONVERSION . Implicit Action: A message is printed and the ERROR condition is raised. Status: CONVERSION is enabled throughout the program, except within the scope of a condition prefix specifying NOCONVERSION. Normal Return: If the ONSOURCE or ONCHAR PSEUDOVARIABLE is used, the program retries the conversion on return from the on-unit. If the error is not corrected, the program loops. If these PSEUDOVARIABLES are not used the ERROR condition is raised. Example DCL X BIT(4);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL X = '10A1'; /* all chars must be 1 or 0 */

Page:-60 /154

ENDFILE Condition The ENDFILE input \ output condition can be raised during a GET or READ operation by an attempt to read past the end of the file specified in the GET or READ statement. It applies only to SEQUENTIAL INPUT, SEQUENTIAL UPDATE, and STREAM INPUT files. The syntax for ENDFILE is: ON ENDFILE (file-reference) .. In record-oriented data transmission, ENDFILE is raised whenever an end of file is encountered during the execution of a READ statement. In stream-oriented data transmission, ENDFILE is raised during the execution of a GET statement if an end of file is encountered either before any items in the GET statement data list have been transmitted or between transmission of two of the data items. If an end of file is encountered while a data item is being processed, or if it is encountered while an X format item is being processed, the ERROR condition is raised. If the file is not closed after ENDFILE is raised, any subsequent GET or READ statement for that file immediately raises the ENDFILE condition again. Implicit Action: A message is printed and the ERROR condition is raised. Status: The ENDFILE condition is always enabled. Normal Return: Execution continues with the statement immediately following the GET or READ statement that raised this condition. If a file is closed in an on-unit for this exception, the results of normal return is undefined. Exit in such situations must be by a GO TO from the on-unit block. ENDPAGE Condition The ENDPAGE input/output condition is raised when a PUT statement results in an attempt to start a new line beyond the limit specified for the current page. This limit can be specified by the PAGESIZE option in an OPEN statement; if PAGESIZE has not been specified, a default limit of 60 is applied. The attempt to exceed the limit can be made during data transmission (including associated format items, if the PUT statement is editdirected), by the LINE option, or by SKIP option. ENDPAGE can also be raised by a LINE option or LINE format item that specified a line number less than the current line number . The syntax for ENDPAGE is ON ENDPAGE (file-reference) ENDPAGE is raised only once per page, except when it is raised by the SIGNAL statement. We can artificially signal this condition with SIGNAL ENDPAGE(filename); The ON-unit can start a new page by execution of a PAGE option or a PAGE format item, which sets the current line to one. If the ON-unit does not start a new page, the current line number can increase indefinitely

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-61 /154

Implicit Action: A new page is started. If the condition is signalled, execution is unaffected and continues with the statement following the SIGNAL statement. Status: ENDPAGE is always enabled. Normal Return: Execution of the PUT statement continues and the data is written on the current line which may(probably would) have been changed by the ON UNIT. ERROR Condition The ERROR condition is raised under the following circumstances: Provides common condition that can be used to check many other conditions As a result of the implicit action for a condition for which that action is to print an error message and raise the ERROR condition. As a result of an error (for which there is no other condition) during program execution. As a result of an ABEND As a result of a SIGNAL ERROR statement. Use ONCODE built-in to distinguish between various circumstances that can raise ERROR condition The syntax for ERROR is: ON ERROR Implicit Action: If the condition is raised in the major task, the FINISH condition is raised and the task terminates. If the condition is raised in any other task, the program is terminated. Status: ERROR is always enabled Normal Return : The implicit action is taken. FINISH Condition The FINISH condition is raised during execution of a statement that would terminate the major task of the PL/I program, that is, by a STOP statement in any task, or an EXIT statement in the major task, or a RETURN or END statement in the MAIN procedure of the program. Note: The STOP statement immediately terminates the program including all concurrent tasks. Before termination the FINISH condition is raised in the task in which the STOP executes. On normal return from the on-unit all tasks in the program terminate. The EXIT statement immediately terminates the program or the task that contains the statement and all tasks attached by this task. If executed in a major task, EXIT raises the FINISH condition in that task. On normal return from the on-unit, the task executing the statement and all of its descendant tasks are terminated. Thus EXIT in a major task is equivalent to a STOP statement. The condition is also raised by SIGNAL FINISH, and as part of the implicit action for the ERROR condition. The condition is raised in the task in which the statement is executed,

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-62 /154

and any ON-unit specified for the condition is executed as part of that task. An abnormal return from the ON-unit avoids program termination and allows the program to continue. The syntax for FINISH is: ON FINISH Implicit Action: No action is taken and processing continues from the point where the condition was raised. Status: FINISH is always enabled. Normal Return: Execution of the statement is resumed. FIXEDOVERFLOW Condition The FIXEDOVERFLOW computational condition is raised when the length of the result of a fixed-point arithmetic operation exceeds the maximum length allowed by the implementation. The FIXEDOVERFLOW condition differs from the SIZE condition in that SIZE is raised when a result exceeds the declared size of a variable, while FIXEDOVERFLOW is raised when a result exceeds the maximum allowed by the computer. The syntax for FIXEDOVERFLOW IS ON FIXEDOVERFLOW .. Result: The result of the invalid fixed-point operation is undefined. Implicit Action: A message is printed and the ERROR condition is raised. Status: FIXEDOVERFLOW is enabled throughout the program, except within the scope of a condition prefix that specifies NOFIXEDOVERFLOW. Example DCL A DCL B DCL C A=40000000; B=80000000; C=A*B; FIXED DEC(15); FIXED DEC(15); FIXED DEC(15); /* fixed point overflow is raised */

OVERFLOW Occurs when the magnitude of the result of a floating point operation exceeds 10**75 A=55E71; B=23E11; C=A*B; /* OVERFLOW condition is raised */ Result: The value of such an invalid floating point number is undefined Implicit Action: A message is printed and ERROR is raised.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-63 /154

Status: Enabled throughout the program except within the scope of NOOVERFLOW Normal Return: Control returns to the point after the instruction which caused this condition. SIZE Condition The SIZE computational condition is raised only when high-order (that is, leftmost) significant binary or decimal digits are lost in an attempted assignment to a variable or an intermediate result or in an input/output operation. This loss can result from a conversion involving different data types, different bases, different scales, or different precision. The size condition is not enabled unless it appears in a condition prefix. The syntax for SIZE is: ON SIZE .. The SIZE condition differs from the FIXEDOVERFLOW condition in that, whereas FIXEDOVERFLOW is raised when the size of a calculated fixed-point value exceeds the maximum allowed by the implementation, SIZE is raised when the size of the value being assigned to a data item exceeds the declared (or default) size of the data item in your program. SIZE can be raised on assignment of a value regardless of whether or not FIXEDOVERFLOW was raised in the calculation of the value. The declared size is not necessarily the actual precision with which the item is held in storage, however, the limit for SIZE is the declared or default size, not the actual size in storage. For example, a fixed binary item of precision (20) occupies a FULLWORD in storage, but SIZE is raised if a value whose size exceeds FIXED BINARY (20) is assigned to it. Because this checking involves a substantial overhead in both storage space and run time, it usually is used only in program testing. You should remove it for production programs. If the SIZE condition is raised and it is disabled, the program is in error. Result: The result of the assignment is undefined. Implicit Action: A message is printed and the ERROR condition is raised. Status: SIZE is disabled within the scope of a NOSIZE condition prefix and elsewhere throughout the program, except within the scope of a condition prefix specifying SIZE. Example DCL X DCL Y X = Y; FIXED DEC(4); FIXED DEC(5) INIT(12345); /* size condition is raised

*/

STRINGRANGE Condition The STRINGRANGE program-checkout condition is raised whenever the values of the arguments to a SUBSTR reference fail to comply with the rules described for the SUBSTR built-in function. It is raised for each such reference. The syntax for STRINGRANGE is:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-64 /154

ON STRINGRANGE. Implicit Action: A message is printed and processing continues as described for normal return. Status: STRINGRANGE is disabled by default and within the scope of a NOSTRINGRANGE condition prefix. It is enabled only within the scope of a STRINGRANGE condition prefix. Normal Return: Depends on various factors. (See PL/I language reference) Example DCL NAME CHAR(20); DCL FIRST CHAR(16); FIRST=SUBSTR(NAME,5,20); /* raises the string range condition

*/

/* STRINGRANGE is disabled by default. To enable it code as below */ (STRINGRANGE): MAINLINE PROCEDURE OPTIONS(MAIN); . . . END MAINLINE; STRINGSIZE Condition The STRINGSIZE program-checkout condition is raised when you attempt to assign a string to a target with a shorter maximum length. The syntax for STRINGSIZE is: ON STRINGSIZE . Result: After the condition action, the truncated string is assigned to its target string. The right-hand characters, bits or graphics of the source string are truncated so that the target string can accommodate the source string. Implicit Action; A message is printed and processing continues. Status: STRINGSIZE is disabled by default and within the scope of a NOSTRINGSIZE condition prefix. It is enabled only within the range of a STRINGSIZE condition prefix. Example DCL NAME CHAR(20); DCL FIRST CHAR(16); FIRST=NAME; /* raises the stringsize condition */ /* To enable stringsize (which is off by default) for an assignment statement code */ /* below */ (STRINGSIZE): RECEIVE = SUBSTR(FIELD_1,5,20); SUBCRIPTRANGE Condition

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-65 /154

The SUBSCRIPTRANGE program-checkout condition is raised whenever a subscript is evaluated and found to lie outside its specified bounds. ON SUBCRIPTRANGE . Result: When SUBCRIPTRANGE has been raised, the value of the invalid subscript is undefined, and, hence, the reference is also undefined. Implicit Action: A message is printed and the ERROR condition is raised. Status: SUBSCRIPTRANGE is disabled by default and within the scope of a NOSUBSCRIPTRANGE condition prefix. It is enabled only within the scope of a SUBCRIPTRANGE condition prefix. Normal Return: Normal return from a SUBSCRIPTRANGE ON-unit raises the ERROR condition . UNDEFINEDFILE Condition The UNDEFINEDFILE input/output condition is raised whenever a nonzero return code is received from the OPEN SVC. If the attempt is made by means of an OPEN statement that specifies more than one file, the condition is raised after attempts to open all files specified. The syntax for UNDEFINEDFILE is: ON UNDEFINEDFILE (file-reference) .. Implicit Action: A message is printed and the ERROR condition is raised. Status: UNDEFINDFILE is always enabled. Normal Return: Upon the normal completion of the final ON-unit, control is given to the statement immediately following the statement that raised the condition. UNDERFLOW Occurs when the magnitude of the result of a floating point operation is less than 10**-78 A=23E-71; B=3E-9; C=A*B; /* underflow condition */ Result: The value of the invalid floating point value is set to 0. Implicit Action: A message is printed and execution continues from the point at which the condition was raised Status: Enabled throughout the program except within the scope of NOUNDERFLOW. Normal Return: Control returns to the point immediately following the point at which the condition was raised. ZERODIVIDE Condition

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-66 /154

The ZERODIVIDE computational condition is raised when an attempt is made to divide by zero. This condition is raised for fixed-point and floating-point division. The compiler can also raise this condition, instead of fixed overflow, when: The results of a conversion from decimal to binary exceeds the maximum length allowed by the implementation. A fixed, floating-point, or decimal divide exception is detected by the hardware, as, for example, when using the DIVIDE built-in function and the quotient exceeds the size specified for the result. The syntax for ZERODIVIDE is: ON ZERODIVIDE If the ZERODIVIDE condition is raised and it is disabled, the program is in error. Result: The result of a division by zero is undefined. Implicit Action: A message is printed and the ERROR condition is raised. Status: ZERODIVIDE is enabled throughout the program, except within the scope of a condition prefix specifying NOZERODIVIDE. Normal Return: Control returns to the point immediately following the point at which the condition was raised. Example A=15; B=0; C=A/B; /* ZERODIVIDE condition */

Example to demonstrate SIZE condition //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * (SIZE): MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD1 FIXED DECIMAL(7,2); DCL FIELD2 FIXED DECIMAL(7,2); DCL FIELD3 FIXED DECIMAL(10,2); DCL SUM FIXED DECIMAL(7,2); ON SIZE BEGIN; PUT SKIP LIST('SIZE ERROR'); END; FIELD3=19999999.99; FIELD1=FIELD3; GET DATA (FIELD1,FIELD2); SUM=FIELD1+FIELD2; PUT SKIP LIST(SUM); GET DATA (FIELD1,FIELD2); SUM=FIELD1+FIELD2; PUT SKIP LIST(SUM);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL END MYPROG; /* //GO.SYSIN DD * FIELD2=1.23,FIELD1=3.45; FIELD1=99999.99,FIELD2=88888.88; /* // Example to demonstrate various CONDITIONS //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD FIXED DEC(5,2); DCL BUFF CHAR(10); DCL BUFF1 CHAR(20) INIT('ABCDEFGHIJKLMNOP'); DCL (J,K) FIXED BINARY(15); DCL A FIXED DECIMAL(5,2); DCL MYCONDITION CONDITION;

Page:-67 /154

ON CONVERSION BEGIN; PUT SKIP LIST('CONVERSION CONDITION RAISED'); PUT SKIP EDIT('CHAR IN ERROR IS=',ONCHAR( ))(A); PUT SKIP EDIT('FIELD IN ERROR IS=',ONSOURCE( ))(A); PUT SKIP LIST('ATTEMPTING REPAIR...'); ONCHAR='1'; END; ON STRINGSIZE PUT SKIP LIST('STRINGSIZE CONDITION...'); ON STRINGRANGE PUT SKIP LIST('STRINGRANGE CONDITION...'); ON ERROR PUT SKIP LIST('ERROR CONDITION RAISED, PROGRAM ABENDING..'); ON FINISH PUT SKIP LIST('FINISH CONDITION RAISED.DO CLEANUP'); ON FIXEDOVERFLOW PUT SKIP LIST('FIXED OVERFLOW CONDITION RAISED'); ON SIZE PUT SKIP LIST('SIZE CONDITION RAISED'); ON NAME(SYSIN) PUT SKIP LIST('NAME CONDITION RAISED ON SYSIN'); ON CONDITION(MYCONDITION) PUT SKIP LIST('MYCONDITION RAISED'); J=1;K=15; GET EDIT (FIELD)(F(5,2)); /* raises conversion condition */ PUT EDIT(FIELD)(F(6,2)); (STRINGSIZE):BUFF=SUBSTR(BUFF1,J,K); /* raises stringsize cond */ J=10;K=20; (STRINGRANGE):BUFF=SUBSTR(BUFF1,J,K);/*raises stringrange cond */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-68 /154

J=32700;K=32700;J=J*K*J; /* raises fixed overflow condition */ J,K=32700; (SIZE):J=J+K; /* raises size condition */ GET SKIP DATA(A); /* name condition raised */ GET SKIP DATA(A); /* this time data is fine*/ (STRINGSIZE):SIGNAL STRINGSIZE; /* just for kicks */ SIGNAL STRINGSIZE; /*sorry wont work here */ SIGNAL CONDITION(MYCONDITION); END MYPROG; /* //GO.SYSIN DD * A1200 B=12.34; A=12.34; /* //

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-69 /154

SECTION 8
Arrays DCL STUD_AVG (365) FIXED DEC(4,2); DCL TABLE(0:11) FIXED DEC(5); DCL GRAPH(-3:3) FIXED DEC(5,2); DCL LIST(-2:6) FIXED BIN(15,0) INIT(1,2,4,21,2,3,4,80,90); DCL TABLE(3,2) FIXED DEC(5); ROW 1 ROW 2 ROW 3 COLUMN 1 (1,1) (2,1) (3,1) COLUMN 2 (1,2) (2,2) (3,2)

ARRAYS

DCL AXIS(-3:3,-4:4) FLOAT DEC(6) INIT((63)0); DCL B(10) FIXED DEC(3) INIT((7)0,1,2,3); DCL TABLE(10) CHAR(2) INIT((10)(2)'A'); DCL TABLE(10) CHAR(5) INIT((10)(1)'EMPTY'); /* note the (1) !. It is required ! */ Access an element as below AXIS(-3,-4) Subscripts can be nested as below /* first element)

DCL X(5) FIXED BIN(15,0) INIT(10,20,30,40,50); DCL Y(3) FIXED BIN(15,0) INIT(3,2,1); I=3; Z=X(Y(I)); /* Z is 10 */ Variable array bounds PROC1: PROCEDURE; DCL A(500) FIXED BIN(31) INIT((500)7); DCL B FIXED BIN(31); CALL MYSUM(A,B); . . . END PROC1; MYSUM:PROCEDURE(ARRAY,SUM); DCL ARRAY(*) FIXED BIN(31); DCL SUM FIXED BIN(31); DCL LOW FIXED BIN(31); DCL HIGH FIXED BIN(31); DCL INDEX FIXED BIN(31); DCL (HBOUND,LBOUND) BUILTIN; LOW = LBOUND(ARRAY,1); /* second parm is the dimension we are looking at*/ HIGH = HBOUND(ARRAY,1); /* LOW and HIGH are now the bounds of ARRAY*/ SUM=0;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-70 /154

DO INDEX=LOW TO HIGH BY 1; SUM=SUM + ARRAY[INDEX]; END; END MYSUM; I/O operations and Arrays DCL AMOUNT(20) FIXED DECIMAL(3); GET LIST AMOUNT; /* AMOUNT(1) to AMOUNT(20) are input */ GET LIST((TEMP(I) DO I = 1 TO LIMIT)); GET LIST(((A(I,J)DO I=21 to 5)DO J=3 TO 7)); /* note that I is inner DO (varies fast). J is outer DO(varies slowly)*/ Array Assignment DCL MONTHS(12) FIXED DEC(4,1); MONTHS = 0; /* inits all elements of MONTHS to 0 DCL DCL A=B; DCL A=-A; A=A*2; DCL A=A+B; B A(10) B(10) A FIXED DEC(6); FIXED DEC(6); FIXED DEC(6) INIT(1,2,3,4,5,6); /* A is now -1,-2,-3,-4,-5,-6 /* A is now -2,-4,-6,-8,-10,-12 FIXED DEC(6) INIT(1,2,3,4,5,6); /* A is now -1,-2,-3,-4,-5,-6 */ */ */

*/

Cross Sections of Arrays DCL TABLE(3,4) FIXED BINARY(15); TABLE(*,1) refers to first column of TABLE TABLE(1,*) refers to first row of TABLE TABLE(*,3) refers to third column of TABLE SUBSCRIPTRANGE condition Default state is disabled. To enable do one of the following (SUBSCRIPTRANGE): MATRIX: PROC OPTIONS(MAIN); . END MATRIX; ON SUBSCRIPTRANGE BEGIN; . . END; Example of ARRAY usage //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-71 /154

//PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL ARRAY(2,10) FIXED DECIMAL(2) INIT((10)1,(10)2); PUT DATA (ARRAY); PUT SKIP LIST('ARRAY AFTER ARRAY+10..'); ARRAY=ARRAY+10; PUT SKIP; PUT DATA (ARRAY); PUT SKIP LIST('NOW RESTORING ARRAY...'); ARRAY(1,*)=1; ARRAY(2,*)=2; PUT SKIP; PUT DATA(ARRAY); END MYPROG; /* // Example to demonstrate ARRAYS //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD_ARRAY(10) FIXED DECIMAL(7,2) INIT(1,2,3,(7)9); DCL SUM FIXED DECIMAL(7,2); CALL ADD(FIELD_ARRAY); PUT SKIP LIST(SUM); ADD:PROCEDURE (A); DCL A(*) FIXED DECIMAL(7,2); DCL (HBOUND,LBOUND) BUILTIN; DCL INDEX FIXED BINARY(15); DCL UPPER FIXED BINARY(15); DCL LOWER FIXED BINARY(15); SUM=0; LOWER=LBOUND(A,1); UPPER=HBOUND(A,1); DO INDEX=LOWER TO UPPER BY 1; SUM=SUM+A(INDEX); END; END ADD; END MYPROG; /* //

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-72 /154

SECTION 9

PICTURES AND STRUCTURES

Pictures To treat character strings as Arithmetic data To treat arithmetic quantities as character strings To edit data ( Zero suppression, Dollar float, +,-,DB,CR and comma decimal insertion in numeric data) for output to printer PICTURE 'picture specification characters' Picture specification characters are 9,V,$,Z,.,,,DB,CR DCL DCL PIC 99999 99999V 999V99 999V99 V99999 99999 999V99 9V9 999V99 S999V99 -999V99 -999V99 S999V99 999V99S +999V99 +999V99 Note: 1:Truncation of most significant digits occurred 2:Truncation of significant digits occurred 3:Truncation on both sides of decimal points occurred 4:Sign is lost as picture clause did not have provision for sign S Arithmetic Operations on Decimal Picture Data DCL SUM PIC'9999'; DCL A PIC'999'; DCL B PIC'999'; SUM = A + B; The compiler generates code to A B Bytes 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 PIC '999V99' /* 9 represents a numeric digit */ */ Note

PIC '(3)9V(2)9' /* V is the implied decimal point Equivalent Value assigned Int. value DECIMAL FIXED(5) 12345 12345^ DECIMAL FIXED(5) 12345 12345^ DECIMAL FIXED(5,2) 123.45 123^45 DECIMAL FIXED(5,2) 12345 345^00 DECIMAL FIXED(5,5) 12345 ^00000 DECIMAL FIXED(5) 123 00123^ DECIMAL FIXED(5,2) 123 123^00 DECIMAL FIXED(2,1) 123.45 3^4 DECIMAL FIXED(5,2) -123.45 123^45 DECIMAL FIXED(5,2) -123.45 -123^45 DECIMAL FIXED(5,2) -123.45 -123^45 DECIMAL FIXED(5,2) +123.45 123^45 DECIMAL FIXED(5,2) +123.45 +123^45 DECIMAL FIXED(5,2) -123.45 123^45DECIMAL FIXED(5,2) +123.45 +123^45 DECIMAL FIXED(5,2) -123.45 123^45

1 2 3 4

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-73 /154

a)Convert A to Fixed Decimal b)Convert B to Fixed Decimal format c) Add A and B d)Convert the result to character form (PIC of SUM) e)Place the converted result in SUM. Note that arithmetic operations can be performed on PIC fields with editing characters. However it results in inefficient code. Z picture character Is used for suppression of leading Zeroes. PIC ZZZZ9 ZZZZ9 ZZZZZ ZZZV99 ZZZVZZ ZZZVZZ ZZZV99 Z9999 ZZZVZ9 ZZ9ZZ Value assigned Internal Representation 100 bb100 0 bbbb0 0 bbbbb 123 12300 1234 23400 .01 bbbb1 0 bbb00 0 b0000 /* invalid. If one Z appears to right of decimal, then all edit chars must b Z */ /* Invalid. All Z'S must be to the left of the 9 */

Decimal Point This is an insertion character DCL A PIC'999V.99' INIT(12.34); PUT LIST(A); /* outputs 012.34

*/

DCL A PIC'999V99' INIT(12.34); PUT LIST(A); /* outputs 01234 */ The alignment is caused by the V edit character. Decimal point is only output as an insertion character. See example below to illustrate this: DCL A PUT LIST(A); PIC'999.99V' INIT(12.34); /* outputs 000.12 */

Comma This is an insertion character DCL A PIC'9,999V.99' INIT(3512.34); PUT LIST(A); /* outputs 3,512.34 DCL A PUT LIST(A); DCL A PUT LIST(A); Blank

*/

PIC'Z,ZZZV.99' INIT(3512.34); /* outputs 3,512.34 */ PIC'Z,ZZZV.99' INIT(512.34); /* outputs 512.34 */

Is another insertion character. Use this to generate blanks on the right hand side of the picture string. If you need blanks on the left, use the Z edit character DCL A PIC'999V99BBB'; /* three blanks on the right*/

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL DCL DCL Slash Is another insertion character DCL RUN_DATE CHAR(6); DCL EDITED_RUN_DATE PIC'99/99/99'; EDITED_RUN_DATE=RUN_DATE; Dollar Sign

Page:-74 /154

B PIC'Z,ZZZV.99(7)B'; /* seven blanks on the right*/ EDITED_DATE PIC'99B99B99'; /*insert blanks between YY,MM, and DD*/

In the Floating form (where there is more than one $ sign) leading zeroes are suppressed and last leading Zero is replaced with the $ sign. In static form (only one $ sign) it appears wherever defined in the picture string. DCL B PUT LIST(B); DCL B PUT LIST(B); PIC'$999V.99' INIT(12.34); /* outputs $012.34 PIC'$$$$V.99' INIT(12.34); /* outputs b$12.34 */ */

Sign characters (S,-,+) The above characters may be also drifting. DCL A PIC'S999' INIT(12); PUT LIST(A); /* outputs +012 */ DCL A PIC'SSS9' INIT(12); PUT LIST(A); /* outputs +12 */ DCL A PIC'9999S' INIT(1234); PUT LIST(A); /* outputs 1234+ */ DCL A PIC'+99' INIT(144); PUT LIST(A); /* outputs +44 */ DCL A PIC'999V.99S' INIT(-123.45); PUT LIST(A); /* outputs 123.45- */ Asterisk

/*error!*/

Usually used as a floating character for protection against forgery DCL A PIC'*****9V.99' INIT(104.75); PUT LIST(A); /* outputs ***104.75 */ DCL A PIC'*****V.**' INIT(104.75); PUT LIST(A); /* outputs **104.75 */ DCL A PIC'*****V.**' INIT(.75); PUT LIST(A); /* outputs *****.75 */ CR and DR DB or CR can be used to indicate negative values. CR or DB to the right of all digit positions in the PIC clause. DCL A PIC'99V.99CR' INIT(-12.75); PUT LIST(A); /* outputs 12.75CR DCL A PIC'99V.99DB' INIT(-12.75); PUT LIST(A); /* outputs 12.75DB DCL A PIC'99V.99CR' INIT(12.75); PUT LIST(A); /* outputs 12.75bb can only appear */ */ */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-75 /154

Character String Pictures These cannot be used for data items which are to participate in computations . They are made up of A, X, and 9 Built-in functions of SUBSTR, LENGTH, INDEX and VERIFY can be used on these data items. A specifies characters A to Z or blank 9 specifies a numeric character X can contain any character The B and other insertion characters may not be specified in these PIC clauses This type of PIC attribute is used primarily for data validation Example: Assume we need to ensure that any data item is of the form 1237AB The first 4 positions are numeric and the last two are alphabetic. Code the data item which is to receive the above data as PIC'9999AA' DCL A PIC'9999AA'; DCL B CHAR(6); A = B; /* if the format of the assigned data does not conform to the PIC of the receiving field, the CONVERSION condition is raised */ The P specification is allowed in input and output GET and PUT statements GET FILE(SYSIN) EDIT(A,B,C,D) (COL(1),P'ZZZ9',P'99V99',P'AA999',P'(5)9'); Input value bb15 1234 AB123 AB123 Format P'ZZZ9' P'99V99' P'AA999' P'(5)9' Resulting internal value 0015 12^34 AB123 conversion error !!

DCL ASSETS FIXED DECIMAL(11,2); ASSETS = 45326985.76; PUT EDIT(ASSETS) (P'$$$$,$$$,$$$V.99'); /* outputs b$45,326,985.76 */ DCL ASSETS FIXED DECIMAL(11,2); ASSETS = 2500.00; PUT EDIT(ASSETS) (P'$ZZZ,ZZZ,ZZZV.99'); /* outputs $bbbbbb2,500.00 */ Structures DCL EMP_ADDRESS, 2 NAME CHAR(20), 2 STREET CHAR(20), 2 CITY CHAR(20), 2 STATE CHAR(20); READ FILE(INFILE) INTO (EMP_ADDRESS); Notes: Level 1 is the Major structure level which does not have attributes. However other storage qualifiers like BASED(P) are to be specified here. 1

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-76 /154

Any number > 1 can represent lower levels Attributes and INIT are defined only at elementary levels There is no equivalent to the COBOL FILLER STRING(EMP_ADDRESS) concatenates all the elements into one character string. STRING(structure variable) can also be used as a pseudo variable (on the LHS of an assignment statement. 1 SALARY_RECORD, NAME, 3 LAST CHAR(10), 3 FIRST CHAR(10), 3 MIDDLE CHAR(10), 2 EMP_NO FIXED DEC(5), 2 HOURS, 3 REGULAR FIXED DEC(4), 3 OVERTIMEFIXED DEC(4), 2 WAGES, 3 REG_PAY FIXED DEC(4), 3 OVT_PAY FIXED DEC(4); 2

DCL

Factoring Example: DCL 1

SALARY_RECORD, NAME, 3 (LAST,FIRST,MIDDLE) 2 EMP_NO 2 HOURS, 3 (REGULAR,OVERTIME) 2 WAGES, 3 (REG_PAY,OVT_PAY) 2

CHAR(10), FIXED DEC(5), FIXED DEC(4), FIXED DEC(4);

Initial Attribute Elementary data items in the structure can have the INIT attribute Example: DCL 1 SALARY_RECORD, 2 NAME, 3 LAST CHAR(10) INIT('JOHNSON'), 2 EMP_NO FIXED DEC(5) INIT(12345), 2 HOURS, 3 REGULAR FIXED DEC(4) INIT(100), 2 WAGES, 3 REG_PAY FIXED DEC(4) INIT(2000); Names within a structure DCL 1 EMP_REC, 3 REGULAR_PAY PIC'999V99', 3 HOURS, 5 REGULAR PIC'99', 5 OVERTIME PIC'99', 3 WAGES,

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-77 /154

5 REGULAR PIC'99V99', 5 OVERTIME PIC'99V99'; EMP_REC.REGULAR_PAY = EMP_REC.HOURS.REGULAR * EMP_REC.WAGES.REGULAR; REGULAR_PAY = HOURS.REGULAR * WAGES.REGULAR; /* also OK if the second and third qualifier uniquely identify the data item */ Arrays in structures DCL 1 2 2 2

INVENTORY_ITEM, PART_NO CHAR(8), QTY_IN_HAND PIC'9999', SALES_HISTORY(12) PIC'99999';

INVENTORY_ITEM.SALES_HISTORY(1) = 1234; Arrays of structures DCL 1 2 WEATHER(20), TEMP, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1), 2 VELOCITY, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1), 2 RAINFALL, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1); Refer to structure as WEATHER(n) Refer to VELOCITY as VELOCITY(n) Refer to TEMP.HIGH as TEMP.HIGH(n) /* called subscripted qualified name */ METRO_WEATHER(365), TEMP, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1), VELOCITY, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1), RAINFALL, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1); 1 COUNTRY_WEATHER LIKE METRO_WEATHER;

Like Attribute DCL 1 2 2 2

DCL

/* effectively the same as the definition below */ DCL 1 2 COUNTRY_WEATHER, TEMP, 3 HIGH FIXED DEC(4,1),

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-78 /154

3 LOW FIXED DEC(4,1), VELOCITY, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1), 2 RAINFALL, 3 HIGH FIXED DEC(4,1), 3 LOW FIXED DEC(4,1); Note that the Dimension Attribute is not copied. To effect the dimension also code as DCL 1 COUNTRY_WEATHER(365) LIKE METRO_WEATHER; 2 We can use LIKE attribute for a minor structure name also. Assignments We can assign one structure to another so long as they have the same minor structuring and same number of elementary items. If arrays are contained within, the bounds must be the same. We can assign Major to minor structures or vice versa provided the relative structuring, as defined above, is the same. Note that even if the attributes of the data items are different, conversion takes place as per rules. DCL 1 A 2 2 2 2 2 B FIXED DEC(5), C FIXED BIN(31), D CHAR(20), E FIXED BIN(15), F FLOAT DEC(5);

DCL 1 AA 2 BB FIXED BIN(15), 2 CC FIXED DEC(5), 2 DD CHAR(10), 2 EE FIXED DEC(4), 2 FF FIXED DEC(6); A = AA; /* valid assignment. Conversions occur /* equivalent to */ /* B = BB */ /* C = CC */ /* D = DD */ /* E = EE */ /* F = FF */ */

Assignment by name DCL 1 INPUT, 2 EMP_NAME 2 EMP_NUMBER 2 HOURS, 3 REGULAR 3 OVERTIME

CHAR(20), CHAR(6), PIC'99', PIC'99',

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL 2 GROSS_PAY DCL 1 OUTPUT, 2 CARRIAGE_CNTL 2 EMP_NUMBER 2 EMP_NAME 2 GROSS_PAY OUTPUT = INPUT, BY NAME;

Page:-79 /154 PIC'999V99'; CHAR(1), CHAR(6), CHAR(20), PIC'999V99';

Elements of INPUT whose names are identical to those of OUTPUT are moved. If Major structure contains a minor structure, the minor structure names must match to move elementary items within the minor structure. DCL 1 FIRST, 2 MINOR_1, 3 A, 3 B, 2 C, 2 D; DCL 1 SECOND, 2 MINOR_2, 3 A, 3 B, 2 C, 2 D; FIRST = SECOND, BY NAME; /* C,D will be moved. However A and B will not as their Minor structure names are different */ Scalar to Structure assignment DCL 1 EMP_REC, 2 EMP_NAME CHAR(20), 2 EMP_ADDRESS, 3 STREET CHAR(20), 3 CITY CHAR(20); EMP_REC = ' '; /* valid /* EMP_NAME = ' '; /* STREET = ' '; /* CITY = ' '; */ */ */ */

Defining Overlay of a Scalar Variable on a Structure Example DCL 1 EMP_REC, 2 EMP_NAME CHAR(20), 2 EMP_ADDRESS, 3 STREET CHAR(20), 3 CITY CHAR(20); DCL EMP_DETAILS CHAR(60) DEFINED EMP_REC;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL EMP_DETAILS=''; Defining Overlay of a structure on a structure Example DCL 1 ISSUES, 2 CODE CHAR(1), 2 QTY PIC '9999', 2 JOB_NO CHAR(4), 2 PART_NO CHAR(7), 2 DEPT CHAR(3), 2 FILLER1 CHAR(3); DCL 1 RECEIPTS DEFINED ISSUES, 2 CODE CHAR(1), 2 QTY PIC '9999', 2 PART_NO CHAR(7), 2 SUPPLIER CHAR(6); READ FILE(INPUT) INTO ISSUES; SELECT (ISSUES.CODE); WHEN ('1') CALL PROCESS_ISSUES; WHEN ('2') CALL PROCESS_RECEIPTS; OTHERWISE CALL CODE_IN_ERROR; END;

Page:-80 /154

Structures in Stream I/O Structures may also be specified in EDIT or LIST directed stream I/O. The names may be major or minor structure names. Example DCL 1 INVENTORY, 2 PART_NUMBER CHAR(6), 2 QTY_ON_HAND FIXED DEC(5), 2 PRICE FIXED DEC(5,2); GET EDIT(INVENTORY) (A(6), F(5), F(5,2)); Example DCL 1 INVENTORY, 2 PART_NUMBER, 3 TYPE CHAR(2), 3 CODE CHAR(3), 2 REORDER_QTY PIC '(4)9'; PUT EDIT(PART_NUMBER,REORDER_QTY) (A(2),A(3),F(4)); 1 A, 2 B(10), 2 C(10); PUT EDIT(A) (F(12));

Example DCL

Outputs

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL A.B(1) A.B(2) A.B(10) A.C(1).A.C(10)

Page:-81 /154

SECTION 10

STORAGE CONTROL

Storage Classes Unless declared otherwise variables will have the storage class AUTOMATIC MAIN: PROCEDURE OPTIONS(MAIN); DCL 1 STRUCTURE, 2 A FIXED DEC (6,2), 2 B CHAR(20); . . P1: PROC; DCL TABLE(100) CHAR(10); . END P1; P2: PROC; DCL LIST(500) FIXED; . END P2; END MAIN; MAIN P1 P2 STRUCTURE TABLE

AUTOMATIC Storage

The above is the layout when P1 is called MAIN P1 P2 STRUCTURE LIST

AUTOMATIC Storage

The above is the layout when P2 is called Note the overlaying of TABLE and LIST which is characteristic of AUTOMATIC variables Automatic variables lifetime is the lifetime of the enclosing block (Either a PROC or BEGIN) Automatic variables make efficient use of storage. The storage for an automatic variable is allocated on entry into the block by a prologue and de-allocated on exit from the block by epilogue. Prologue and Epilogue code is generated by the compiler. STATIC Storage MAIN: PROCEDURE OPTIONS(MAIN); DCL 1 STRUCTURE,

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL 2 A FIXED DEC (6,2), 2 B CHAR(20); . . P1: PROC; DCL TABLE(100) CHAR(10) STATIC; END P1; P2: PROC; DCL LIST(500) FIXED STATIC; . END P2; END MAIN; MAIN P1 P2 LIST TABLE STRUCTURE

Page:-82 /154

static storage static storage automatic storage

The above is the layout when P1 or P2 are called Note that Static storage is allocated before start of program execution and remains allocated throughout program execution. Where you want variables to retain their value from one invocation of the procedure to another declare them with the static attribute. Static variables can be initialised just like automatic variables DCL FLAG BIT(1) INIT('1'B) STATIC; Based Storage DCL P POINTER; DCL A(100) FIXED DEC(5) BASED(P); /* first declaration of P is not mandatory as second declaration implies the nature of P which is a variable of type POINTER Note that storage for A is NOT allocated by the above declaration */ To refer variable A one of the following needs to be done Assign to P the value returned by an ADDR( ) function DCL VALUE1 BIT(32) BASED(P); DCL VALUE2 BIT(32); P = ADDR(VALUE2); Assign to P another pointer which has a valid value DCL (P,Q) POINTER; Q = ADDR(AREA); P = Q;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-83 /154

Initialise P by using it with SET option of a READ or LOCATE I/O statement READ FILE(INPUT) SET(P); /* I/O read direct to based variable */ LOCATE FILE(OUTPUT) SET(Q); /*Based variable mapped to output buffer */ Allocate the variable which P is tied to. DCL (P,Q) POINTER; DCL AREA CHAR(100) BASED(P); ALLOCATE(AREA); /* First generation of AREA */ ALLOCATE(AREA) SET(Q); /* Second generation of AREA */ AREA = ABCD; /* First generation used */ Q->AREA = PQRS; /* Second generation used */ Simulating Overlays DCL A(100) FIXED BIN(15); DCL B(50) FIXED BIN(15) DEFINED A; /* traditional definition of overlay */ DCL A(100) FIXED BIN(15); DCL B(50) FIXED BIN(15) BASED(P); P = ADDR(A); /* alternate method */ /* note that storage size of B must not exceed that of A */ It is possible to have two variables based on one POINTER. DCL PTR POINTER; DCL 1 A, 2 B PIC'9999', 2 C FIXED DEC (13,2), 2 D CHAR(21); DCL 1 J 2 2 2 BASED(PTR), X FIXED BIN(15), Y FLOAT DEC(6), Z BIT(7);

DCL 1 W BASED(PTR), 2 K FIXED BIN(15), 2 L FIXED BIN(15); PTR = ADDR(A); When overlay defining a CHAR varying field special considerations exist DCL FIELD CHAR(100) VARYING; DCL 1 STRUCTURE BASED(P), 2 LENGTH FIXED BIN(15,0), 2 DATA CHAR(100); P = ADDR(FIELD); Note that LENGTH overlays the Half word length field preceding the varying character field.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-84 /154

Using POINTERS in I/O (Locate mode I/O) DCL P POINTER; DCL TAPE FILE INPUT RECORD ENVIRONMENT(F BLKSIZE (240) RECSIZE(24)); DCL 1 ISSUES BASED(P), 2 CODE CHAR(1), 2 QTY PIC'(4)9', 2 JOB_# PIC'(4)9', 2 PART_# PIC'(7)9', 2 DEPT PIC'99', 2 UNUSED CHAR(6); DCL 1 RECEIPTS BASED(P), 2 CODE CHAR(1), 2 QTY PIC'(4)9', 2 UNUSED CHAR(6), 2 PART_# PIC'(7)9', 2 SUPPLIER CHAR'(6)9'; READ FILE (TAPE) SET(P); SELECT (ISSUES.CODE); WHEN ('1') CALL PROCESS_ISSUES; WHEN ('2') CALL PROCESS_RECEIPTS; END; To output in LOCATE mode: LOCATE OUT FILE (TAPEOUT) SET(Q); /* OUT is output data area for which pointer is Q */ Note that the SET parameter is optional. WE could have coded as below DCL OUT CHAR(80) BASED(Q); LOCATE OUT FILE (TAPEOUT); /* earlier declaration should have been DCL OUT CHAR(80) BASED(Q);*/ Controlled Storage DCL A(100) FIXED DEC(5) CONTROLLED; /* A does not exist here */ ALLOCATE A; /* Storage for A is allocated here */ GET LIST(A); TOTAL = SUM(A); FREE A; /* Storage for A is de-allocated here */ Note that controlled variables after allocation exist until they are explicitly freed. They are not affected by block boundaries. For arrays the size specification may be deferred until ALLOCATE DCL A(*) FIXED DEC(5) CONTROLLED; ALLOCATE A(100); DCL TABLE(*,*) BIN(1) CONTROLLED; ALLOCATE TABLE(100,100);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-85 /154

Note that a repeat allocation before a free results in a new generation of the variable. The previous generation is pushed into a stack meant for storing multiple generations of controlled variables. A free will then cause the previous generation to be popped out of the stack. Example follows: DCL BUFF CHAR(100) CONTROLLED; ALLOCATE BUFF; BUFF = 'THIS IS FIRST GENERATION OF BUFF'; PUT LIST(BUFF); /* this is first generation of buff */ ALLOCATE BUFF; BUFF = 'THIS IS SECOND GENERATION OF BUFF'; PUT LIST(BUFF); /* this is second generation of buff*/ FREE BUFF; PUT LIST(BUFF); /* this is first generation of buff */ FREE BUFF; /* No BUFF in existence */ The ALLOCATION(X) built in function returns a '1'B if the variable X has been allocated. Else it returns a '0'B. X can be an element variable name, a major structure name or an unsubscripted array name. DCL 1 PRODUCT BASED(Q), 2 DESCRIPTION CHAR(20), 2 CODE FIXED DEC(4); DCL P POINTER; ALLOCATE PRODUCT SET(P); /* note that Q is not changed by this */ P->PRODUCT.DESCRIPTION = 'SCREW'; /* PRODUCT is referenced by a locator qualifier */ FREE P->PRODUCT; DCL NULL BUILTIN; /* FREE the storage for the variable this way*/ /* NULL is a builtin function which returns a NULL pointer */

Storage Control Allocation for a given variable can take place statically, (before the execution of the program) or dynamically (during execution). A variable that is allocated statically remains allocated for the duration of the program. A variable that is allocated dynamically relinquishes its storage either upon the termination of the block containing that variable or at your request, depending upon its storage class. AUTOMATIC specifies that storage is allocated upon each entry to the block that contains the storage declaration. The storage is released when the block is exited. STATIC specifies that storage is allocated when the program is loaded. The storage is not freed until program execution is completed. CONTROLLED specifies that you maintain full control over the allocation and freeing of storage with the ALLOCATE and FREE statements. Multiple allocations of the

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-86 /154

same controlled variable in the same task, without intervening freeing, stack generations of the variable. BASED, like CONTROLLED, specifies that you maintain full control over storage allocation and freeing. Multiple allocations are not stacked but are available at any time. Each allocation can be identified by the value of a locator variable. The default storage class is AUTOMATIC Automatic and based variables can have internal scope only . Static and controlled variables can have internal or external scope. Static Storage and Attribute You use static storage when the variable is local to the procedure and the value it contains must be saved between successive invocations. Variables declared with the STATIC attribute are allocated prior to running a program. They remain allocated until the program terminates Example A: PROC OPTIONS(MAIN); . . . B: PROC; DECLARE X STATIC INTERNAL; . . . END B; END A; Although the variable X is allocated throughout the program, it can be referenced only within procedure B or any block contained in B. Automatic Storage and Attribute Automatic variables are allocated on entry to the block in which they are declared. They can be reallocated many times during the execution of a program. You control their allocation by your design of the block structure. The syntax for the AUTOMATIC attribute is: DCL X FIXED BIN(31) AUTOMATIC; Example A: PROC; . . . CALL B; B:PROC; DECLARE (X,Y) AUTO; .

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL . . END B; . . . CALL B;

Page:-87 /154

Each time procedure B is invoked, the variables X and Y are allocated storage. when B terminates the storage is released, and the values they contain are lost. Array bounds, string lengths, and area sizes for automatic variables can be specified as expressions. A:PROC; DECLARE N FIXED BIN; . . . B:PROC; DECLARE STR CHAR(N); Controlled Storage and Attribute Variables declared as CONTROLLED are allocated only when they are specified in an ALLOCATE statement. You have individual control over each controlled variable. a controlled variable remains allocated until a FREE statement that names the variable is encountered or until the end of the program in which it is allocated. Controlled variables are independent of the program block structure. Example A: PROC; DCL X CONTROLLED; CALL B; . . . B:PROC; ALLOCATE X; . . . END B; END A; The variable X can be referred to within the procedure B and that part of the procedure A that follows execution of the CALL statement. ALLOCATE Statement for Controlled Variables The ALLOCATE statement allocates storage for controlled variables, independent of procedure block boundaries. The bounds of controlled arrays, the lengths of controlled strings, and the size of controlled areas, as well as their initial values, can also be specified at the time the ALLOCATE statement is executed.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-88 /154

If a bound, length, or size is explicitly specified in an ALLOCATE statement, it overrides that given in the DECLARE statement. If a bound, length, or size is specified by an asterisk in an ALLOCATE statement, the bound, length, or size is taken from the current generation. DCL X (20) FIXED BIN CTL; ALLOCATE X; /* the upper bound is taken as 20 from the DCL statement */ DCL X(20) CHAR(5) CONTROLLED; ALLOCATE X(25) CHAR(6); /* DCL upper bound and length are overridden

*/

Initial values are assigned to a variable upon allocation, if it has an INITIAL attribute in either the ALLOCATE statement or DECLARE statement. If an INITIAL attribute appears in both DECLARE and ALLOCATE statements, the INITIAL attribute in the ALLOCATE statement is used. FREE Statement for Controlled Variables The FREE statement frees the storage allocated for controlled variables. The storage can then be used for other allocations. For controlled variables, the next most recent allocation in the task is made available, and subsequent references in the task refer to that allocation. Multiple Generations of Controlled Variables An ALLOCATE statement for a variable for which storage was previously allocated and not freed pushes down or stacks storage for the variable. This stacking creates a new generation of data for the variable. The new generation becomes the current generation; the previous generation cannot be directly accessed until the current generation has been freed. When storage for this variable is freed, using the FREE statement, storage is popped up from the stack. Example DCL X(10,20) CHAR(5) CTL; ALLOCATE X; ALLOCATE X(10,10); ALLOCATE X(*,*); The first generation of X has bounds (10,20); the second and third generations have bounds (10,10). The elements of each generation of X are all character strings of length 5. The asterisk notation can also be used in a DECLARE statement, but has a different meaning. Example DCL Y CHAR(*) CTL, N FIXED BIN; N=20; ALLOCATE Y CHAR(N); ALLOCATE Y;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-89 /154

The length of the character string Y is taken from the previous generation unless it is specified in an ALLOCATE statement, in which case Y is given the specified length. This allows you to defer the specification of the string length until the actual allocation of storage. Controlled Structures When a structure is controlled, any arrays, strings, or areas it contains can be adjustable. For this reason, you are allowed to describe the relative structuring in an ALLOCATE statement. Example: DCL 1 A CTL, 2 B(-10:10), 2 C CHAR(*) VARYING; ALLOCATE 1 A, 2 B(1:10), 2 C CHAR (5); FREE A; Example DCL 1 A CTL, 2 B(N,M), 2 C CHAR(L) VARYING; N = -10; M = 10; L = 5; ALLOC A; FREE A; Built-in Functions for Controlled Variables The ALLOCATION built-in function returns a binary value of precision (31,0) indicating the number of generations that you can access in the current task for a given controlled variable. Array-handling functions DIM, which determines the extent of a specified dimension of an array, and LBOUND and HBOUND, which determine the lower and upper bound, respectively, of a specified dimension of a given array. The CURRENTSTORAGE built-in function return the amount of storage required by a particular variable. STORAGE returns allocated storage for a variable. For strings, the built-in function LENGTH returns the current length of the string. Based Storage and Attribute A declaration of a based variable is the amount of storage required and its attributes. A locator value identifies the location of the generation. A based variable can be used to describe existing data, to obtain storage by means of the ALLOCATE statement, or to access data in a buffer by means of the LOCATE statement or READ (with SET option) statement. DCL X FIXED BIN BASED(P);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-90 /154

This declares that references to X, except when the reference is explicitly qualified, use the variable P to locate the storage for X. In the following example, the arrays A and C refer to the same storage. The elements B and C (2,1) also refer to the same storage. DCL A (3,2) CHAR(5) BASED(P), B CHAR(5) BASED(Q), C (3,2) CHAR(5); P = ADDR(C); Q = ADDR (A(2,1)); Note: When a based variable is overlaid in this way, no new storage is allocated. The based variable uses the same storage as the variable on which it is overlaid (A(2,1) in the example). Locator Data There are two types of locator data: pointer and offset. The value of a pointer variable is effectively an address of a location in storage relative to the start of the virtual address space ( X00000000). The value of an offset variable specifies a location relative to the start of an AREA variable and remains valid when the area is assigned to a different part of storage. When an offset variable is used in a reference, its value is implicitly converted to a pointer value. Explicit conversion of an offset to a pointer value is accomplished using the POINTER built-in function. DCL P POINTER, O OFFSET(B), B AREA; P = POINTER(O,B); The OFFSET built-in function complements the POINTER built-in function and returns an offset value derived from a given pointer and area. Locator Qualification Locator qualification is the association of one or more locator references with a based reference to identify a particular generation of a based variable. This is called a locatorqualified reference. The composite symbol -> represents qualified by or points to. Example: P -> X X is a based variable and P is a locator variable. Reference to a based variable can also be implicitly qualified. The locator reference used to determine the generation of a based variable that is implicitly qualified is the one declared with the based variable. Example DCL X FIXED BIN BASED(P); ALLOCATE X; X= X + 1; References to X can also be explicitly locator-qualified as follows:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-91 /154

P->X = P->X + 1; Q = P; Q->X = Q->X + 1; Because the locator declared with a based variable can also be based, a chain of locator qualifiers can be implied. Example DECLARE (P(10),Q) POINTER, R POINTER BASED (Q), V BASED (P(3)), W BASED (R); ALLOCATE R,V,W; /* all references below are valid /* /* /* /* /* P(3) -> V V Q -> R -> W R -> W W */ */ */ */ */ */

Levels of Locator Qualification DECLARE X BASED (P), P POINTER BASED (Q), Q OFFSET (A); ALLOCATE P; /* have to do this first to make instance of P */ ALLOCATE X; /* Now we can make an instance of X */ The references: X, P->X, and Q->P->X all represent three levels of locator qualification and are equivalent ways of referencing X. POINTER Variable and Attribute A pointer variable is declared contextually if it appears in the declaration of a based variable, as a locator qualifier, in a BASED attribute, or in the SET option of an ALLOCATE, LOCATE, or READ statement. Setting Pointer Variables NULL ( ) returns null pointer value SYSNULL ( ) returns null pointer value. Use for static pointer and offset variables POINTERADD(X,Y) . X is a pointer, Y is a FIXED BIN(31) POINTERVALUE(X). X is FIXED BIN(31) POINTER(X,Y). X is an OFFSET , Y is an AREA, returns a POINTER. A READ statement with the SET option. An ALLOCATE statement. By assignment of the value of another locator variable, or a locator value returned by a user-defined function.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-92 /154

Built-In Functions for Based Variables The ADDR built-in function returns a pointer value that identifies the first byte of a variable. The ENTRYADDR built-in function returns a pointer value that is the address of the first executed instruction if the entry were to be invoked. In general, the value of the NULL built-in function is used whenever a pointer (or offset) variable does not identify a location in storage. There are many ways a pointer can acquire the null value: 1. By assignment of the NULL built-in function 2. Assignment of the ENTRYADDR built-in function of a procedure that has not been fetched 3. Assignment of the value returned by the ADDR built-in function for an unallocated controlled variable. 4. It can also acquire the system null value by the assignment of the SYSNULL built-in function. ALLOCATE Statement for Based Variables The ALLOCATE statement allocates storage for based variables and sets a locator variable that can be used to identify the location, independent of procedure block boundaries. ALLOCATE (based-variable [, based-variable,]) [IN ( area-reference)] [SET (locator-reference)] IN :-Specifies the area variable in which the storage is allocated. SET :- Specifies a locator variable that is set to the location of the storage allocated. If the SET option is not specified, the declaration of the based variable must specify a locator variable.

Storage is allocated in an area when the IN option is specified or the SET option specifies an offset variable. FREE Statement for based variables The FREE statement frees the storage allocated for based and controlled variables. FREE locator-qualifier->based-variable [IN (area-reference)] REFER Option (Self-Defining Data) A self-defining structure contains information about its own fields, such as the length of a string. A based structure can be declared to manipulate this data. String lengths, array bounds, and area sizes can all be defined by variables declared within the structure. The variable, known as the object of the REFER option, must be a member of the declared structure. It must be REAL FIXED BINARY(p,0). Example DECLARE 1 STR BASED (P)

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL 2 X FIXED BINARY, 2 Y (L REFER (X)), L FIXED BINARY INIT(1000);

Page:-93 /154

This declaration specifies that the based structure STR consists of an array Y and an element X. When STR is allocated, the upper bound is set to the current value of L which is assigned to X . For any other reference to Y, such as a READ statement that sets P, the bound value is taken from X. Example DECLARE 1 STR BASED, 2 (M,N), 2 ARR( I REFER (M) , J REFER(N) ), 2 X; When this structure is allocated, the values assigned to I and J set the bounds of the two dimensional array ARR Example DCL 1 REC BASED (P), 2 N, 2 A(M REFER(N)), M INITIAL (100); ALLOCATE REC; N = 86; WRITE FILE (X) FROM (REC);

86 elements of REC are written. It would be an error to attempt to free REC at this point, since N must be restored to the value it had when allocated (that is, 100). If N is assigned a value greater than 100, an error occurs when the WRITE statement is encountered. Area Data and Attribute Area Variables describe areas of storage that are reserved for the allocation of based variables. This reserved storage can be allocated to, and freed from, based variables by the ALLOCATE and FREE statements. Area variables can have any storage class and must be aligned. When a based variable is allocated and an area is not specified, the storage is obtained from wherever it is available. You might want to identify the locations of based variables within an area variable relative to the start of the area variable. Offset variables are provided for this purpose. A variable is given the AREA attribute contextually by its appearance in the OFFSET attribute or an IN option, or by explicit declaration. The syntax for the AREA attribute it: AREA [ ( * | expression [ REFER (variable)] ) ] If expression, or *, is not specified, the default is 1000

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-94 /154

Use * for AREA with controlled attribute, or when an AREA is passed as an argument to a called procedure where the AREA parameter can be declared with a * so that the size can be picked up from the argument Examples of AREA declarations are: DECLARE AREA1 AREA (2000), AREA2 AREA; Offset Data and Attribute Offset data is used exclusively with area variables. The value of an offset variable indicates the location of a based variable within an area variable relative to the start of the area DCL variable-name OFFSET (area-variable) Setting Offset Variables The value of an offset variable can be set in any one of the following ways: By an ALLOCATE statement By assignment of the value of another locator variable, or a locator value returned by a user-defined function. By assignment of the NULL built-in function value By using the return value from the OFFSET built-in function. Example: DCL X Y A O BASED(O), BASED(P), AREA, OFFSET(A);

ALLOCATE X; ALLOCATE Y IN(A); The storage class of area A and offset O is AUTOMATIC by default. The first ALLOCATE statement is equivalent to: ALLOCATE X IN(A) SET(O); The second ALLOCATE statement is equivalent to; ALLOCATE Y IN(A) SET(P); The following example shows how a list can be built in an area variable using offset variables: DCL A AREA, (T,H) OFFSET(A), 1 STR BASED(H), 2 P OFFSET(A), 2 DATA;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-95 /154

ALLOCATE STR ; T=H; NEXT: ALLOCATE STR SET (T->P); T=T->P; . . GO TO NEXT; T->P = NULL; READ: T=H; DO WHILE ( T ~= NULL); PUT LIST (DATA); T=T->P; END; Area Assignment The value of an area reference can be assigned to one or more area variables by an assignment statement. Area-to-area assignment has the effect of freeing all allocations in the target area and then assigning the extent of the source area to the target area, so that all offsets for the source area are valid for the target area. Example DCL X BASED (O(1)), O(2) OFFSET (A), (A,B) AREA; ALLOC X IN (A); X = 1; ALLOC X IN (A) SET (O(2)); O(2)->X = 2; B = A; Using the POINTER built-in function, the references POINTER (O(2),B)->X and O(2)->X represent the same value allocated in areas B and A respectively. Input / Output of Areas The area facility allows input and output of complete lists of based variables as one unit, to and from RECORD files. On output, the area extent, together with the 16 bytes of control information, is transmitted. Because the extents of areas can vary, V format or U format records should be used. the maximum records length required is governed by the area length (area size + 16). List Processing In list processing, a number of based variables with many generations can be included in a list. Members of the list are linked together by one or more pointers in one member identifying the location of other members or lists. DCL 1 STR BASED(H), 2 P POINTER, 2 DATA, T POINTER;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-96 /154

ALLOCATE STR; T=H; NEXT: ALLOCATE STR SET (T->P); T = T->P; . . . . GO TO NEXT; T->P = NULL; For the above example, the following statements can be used to access each generation in turn: DO T = H WHILE (T =NULL); .... . . . T -> DATA . . . ; . . . T = T->P; END; DEFINED Attribute The DEFINED attribute specifies that the declared variable is associated with some or all of the storage associated with the designated base variable. DECLARE variable-name DEFINED reference Base variable can be EXTERNAL or INTERNAL The defined variable must be INTERNAL and a level-1 identifier. It cannot be INITIAL, AUTOMATIC,BASED, CONTROLLED, STATIC. There are three types of defining: simple, iSUB, and string overlay. Simple Defining Simple defining allows you to refer to an element, array, or structure variable by another name. The defined and base variables can comprise any data type, but they must match, Examples: DCL A(10,10,10), X1(2,2,2) DEF A, X2(10,10) DEF A (*,*,5), X3 DEF A (L,M,N); X1 is a three-dimensional array that consists of the first two elements of each row, column and plane of A. X2 is a two-dimensional array that consists of the fifth plane of A. X3 is an element that consists of the element identified by the subscript expressions L,M, and N. Example DCL B CHAR(10), Y CHAR(5) DEF B; Y is a character string that consists of the first 5 characters of B.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL Example DCL C AREA (500), Z AREA (500) DEF C; Z is an area defined on C. Example DCL 1 D UNALIGNED, 2 E, 2 F, 3 G CHAR(10) VAR, 3 H, 1 S UNALIGNED DEF D, 2 T, 2 U, 3 V CHAR(10) VAR, 3 W;

Page:-97 /154

S is a structure defined on D. For simple defining, the organisation of the two structures must be identical. A reference to T is a reference to E, V to G, and so on. iSUB Defining By defining an iSUB, you can create a defined array that consists of designated elements from a base array. An iSUB variable is a reference, in the subscript list for the base array, to the ith dimension of the defined array. The value of I ranges from 1 to n, where n is the number of dimensions in the defined array. Examples: DCL A (10,10), X(10) DEFINED (A(1SUB,1SUB)); X is a one-dimensional array that consists of a diagonal of A. String Overlay Defining Examples: DCL A CHAR (100), V(10,10) CHAR(1) DEF A; V is a two-dimensional array that consists of all the elements in the character string A. Example DCL B(10) CHAR(1), W CHAR (10) DEF B; W is a character string that consists of all the elements in the array B. POSITION Attribute The POSITION attribute can be used only with string-overlay defining and specifies the bit or character within the base variable at which the defined variable is to begin.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-98 /154

Examples: DCL C (10,10) BIT (1), X BIT(40) DEF C POS(20); X is a bit string that consists of 40 elements of C, starting at the 20th element. INITIAL Attribute The INITIAL attribute specifies an initial value or values assigned to a variable at the time storage is allocated for it. Only one initial value can be specified for an element variable; more than one can be specified for an array variable. A structure variable can be initialised only by separate initialisation of its elementary names, whether they are element or array variables. The INITIAL attribute has two forms. the first specifies an initial constant, expression, or function reference, whose value is assigned to a variable when storage is allocated to it. The second form specifies that, through the CALL option, a procedure is invoked to perform initialisation at allocation. The variable is initialised by assignment during the execution of the called routine (rather than by this routine being invoked as a function that returns a value to the point of invocation). The syntax for the INITIAL attribute is: DCL variable-name attribute INITIAL ( item, item,) Where item is initial-constant | reference | expression | (iteration factor) iteration-item Where Initial constant is one of the following + or - Arithmetic constant bit constant Character constant entry constant file constant label constant For a variable that is allocated when the program is loaded, that is, a static variable, which remains allocated throughout execution of the program, any value specified in an INITIAL attribute is assigned only once. (Static storage for fetched procedures is allocated and initialised each time the procedure is loaded). For automatic variables, which are allocated at each activation of the declaring block, any specified initial value is assigned with each allocation. For based and controlled variables, which are allocated at the execution of ALLOCATE statements (also LOCATE statements for based variables), any specified initial value is assigned with each allocation. However, this initialisation of controlled variables can be overridden in the ALLOCATE statement. Initial values specified for an array are assigned to successive elements of the array in row-major order (final subscript varying most rapidly). If too many initial values are specified, the excess values are ignored; if not enough are specified, the remainder of the array is not initialised.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-99 /154

Only constant values with no operations, for example, 3 or ABC, can be specified in the INITIAL attribute for static variables, except that the NULL built-in function can be used to initialise a static pointer variable. Example DCL C CHAR(3) STATIC INIT (('A'| | 'BC')) Example ((2) 'A') ((2) ('A')) ((2) (1)'A') is equivalent to ('AA') is equivalent to ('A','A') is equivalent to ('A','A')

Note that first index is string repetition factor ( (3)'B' is 'BBB'). The outer index is the iteration factor ( (2)(3)'B' is 'BBB','BBB'). Example In the following example, when storage is allocated for NAME, the character constant JOHN DOE (padded on the right to 10 characters) is assigned to it: DCL NAME CHAR(10) INIT(JOHN DOE); In the following example, when PI is allocated, it is initialised to the value 3.1416: DCL PI FIXED DEC (5,4) INIT (3.1416); The following example specifies that A is to be initialised with the value of the expression B*C: DECLARE A INIT ((B*C)); The following examples illustrate the use of a function reference to initialise a variable: DECLARE X INIT(SQRT(Z)); DECLARE TABLE(100,10) INITIAL (( 920(0), (20) ((3)5,9)); First 920 elements are set to zero. Thereafter the pattern 5,5,5,9 is repeated 20 times. Example DCL TABLE (20,20) INITIAL CALL SET_UP(X,Y); DCL A(15) CHAR(13) INITIAL ( 'JOHN DOE', *, 'RICHARD ROW', 'MARY SMITH');/*only 1st, 3rd and 4th elements inited*/ DCL B(10,10) DECIMAL FIXED(5) INIT ((25)0,25(1),50(0)); DCL 1 C(8), 2 D INIT(0), /* only first element zeroed */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL 2 E INIT((8)0); /* all 8 elements zeroed

Page:-100 /154 */

Example of demonstration of POINTERS //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * %PROCESS ATTRIBUTES(FULL),NOT('~'); MYPROG: PROCEDURE OPTIONS(MAIN); DCL 1 LINK BASED(P), 2 DATA CHAR(80), 2 NEXT POINTER; DCL (HEAD,THIS) POINTER; DCL COUNT FIXED DEC(3); DCL BUFFER CHAR(80); DCL NULL BUILTIN; ALLOCATE LINK SET(HEAD); COUNT=1; PUT STRING(BUFFER) EDIT(COUNT,'ABCDEFGHIJKLMNOPQRS')(F(3),A); HEAD->DATA=BUFFER; THIS=HEAD; DO I=1 TO 100 BY 1; ALLOCATE LINK SET(THIS->NEXT); COUNT=COUNT+1; PUT STRING(BUFFER) EDIT(COUNT,'ABCDEFGHIJKLMNOPQRS')(F(3),A); THIS=THIS->NEXT; THIS->DATA=BUFFER; END; THIS->NEXT=NULL; THIS=HEAD; DO WHILE(THIS ~= NULL); PUT SKIP EDIT(THIS->DATA)(A); THIS=THIS->NEXT; END; END MYPROG; /* // Example of demonstration of CONTROLLED attribute //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * %PROCESS ATTRIBUTES(FULL),NOT('~'); MYPROG: PROCEDURE OPTIONS(MAIN); DCL 1 LINK CONTROLLED, 2 DATA CHAR(80); DCL COUNT FIXED BINARY(15); DCL BUFFER CHAR(80); DCL NULL BUILTIN; DO I=1 TO 100 BY 1; ALLOCATE LINK; PUT STRING(BUFFER) EDIT(I,'ABCDEFGHIJKLMNOPQRS')(F(3),A);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL LINK.DATA=BUFFER; COUNT=COUNT+1; END; COUNT=ALLOCATION(LINK); DO I=1 TO COUNT BY 1; PUT SKIP EDIT(LINK.DATA)(A); FREE LINK; END; END MYPROG; Example of demonstration of AREA type variables %PROCESS ATTRIBUTES(FULL),NOT('~'); MYPROG: PROCEDURE OPTIONS(MAIN); DCL MYAREA AREA(2000); DCL NEWMYAREA AREA(20000); DCL (HEAD,THIS) OFFSET(MYAREA); DCL 1 LINK BASED(P), 2 DATA CHAR(80), 2 NEXT OFFSET(MYAREA); DCL COUNT FIXED DEC(3); DCL BUFFER CHAR(80); DCL NULL BUILTIN;

Page:-101 /154

ON AREA BEGIN; PUT SKIP LIST('INSUFFICIENT STORAGE IN AREA...'); STOP; END; ALLOCATE LINK SET(HEAD); COUNT=1; PUT STRING(BUFFER) EDIT(COUNT,'ABCDEFGHIJKLMNOPQRS')(F(3),A); HEAD->DATA=BUFFER; THIS=HEAD; DO I=1 TO 100 BY 1; ALLOCATE LINK SET(THIS->NEXT); COUNT=COUNT+1; PUT STRING(BUFFER) EDIT(COUNT,'ABCDEFGHIJKLMNOPQRS')(F(3),A); THIS=THIS->NEXT; THIS->DATA=BUFFER; END; THIS->NEXT=NULL; THIS=HEAD; DO WHILE(THIS ~= NULL); PUT SKIP EDIT(THIS->DATA)(A); THIS=THIS->NEXT; END; PUT SKIP LIST('NOW READING FROM NEW AREA..'); NEWMYAREA=MYAREA;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL THIS=HEAD;

Page:-102 /154

DO WHILE(THIS ~= NULL); PUT SKIP EDIT(POINTER(THIS,NEWMYAREA)->DATA)(A); THIS=POINTER(THIS,NEWMYAREA)->NEXT; END; END MYPROG; /* //

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-103 /154

SECTION 11

FILE PROCESSING

TYPES OF TRANSMISSION Stream Data formatting facilities, less efficient, conversion to character form before output or input. Stream implies that the data of the file is a continuos stream of data items in character form assigned to variables or from expressions to the stream Record No data formatting facilities, more efficient, image of data in program buffer written to media. Record mode implies that the file consists of physically separate records each of which consist of one or more data items in any form Record VS Stream I/O STREAM I/O (LIST / EDIT / DATA) GET characters RECORD READ

Convert to Coded arithmetic form

Process coded arithmetic form

Process Data in Record form Any conversions have to be done by the programmer

Convert coded arithmetic form to character form

Write

Put characters

Comparison between the two forms of I/O In Stream I/O conversions take place in the process of reading the data from Character to coded form or vice versa. No such conversions take place with record I/O In stream I/O less than or more than one physical record may be needed to satisfy a GET or a PUT. In record I/O only one record is output or input at a time for a READ or WRITE statement.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-104 /154

In Stream I/O the programmer knows in advance the format of the data (else it is not possible to specify the data items or format list). In record I/O programmer needs to know only RECSIZE / BLKSIZE. Data format need not be known. In record I/O data in any form (coded or character) may be stored. In stream form the data has to be in character form only. Record mode I/O may be used with any type of data set(QSAM, VSAM KSDS, VSAM RRDS, VSAM ESDS). Stream I/O can be used only on sequential data sets. TYPES OF DATA SETS SUPPORTED BY PL/I Type of data set PL/I organization Sequential CONSECUTIVE Indexed sequential INDEXED Direct REGIONAL KSDS,ESDS,RRDS VSAM FILE INPUT / OUTPUT The Process Define the File Open the File Process the File Close the File PL/I statement DECLARE filename FILE OPEN FILE(filename); READ/WRITE/REWRITE for access by record GET/PUT for stream access CLOSE FILE(filename)

A file used within a PL/I program has a PL/I file name. If you have the following file declaration in your program: DCL STOCK FILE STREAM INPUT; Create a DD statement with a data definition name (DDNAME) //GO.STOCK DD DSN=PARTS.INSTOCK, . . .

Ensure that the DDNAME of the DD statement that defines the data set is the same as either the declared PL/I file name, or the character-string value of the expression specified in the TITLE option of the associated OPEN statement. If the file name is longer than 8 characters, the default DDNAME is composed of the first 8 characters of the file name. OPEN FILE (DETAIL) TITLE(DETAILI); Corresponding JCL is //DETAILI DD DSNAME=DETAILA, . . . File Variables are different from file constants as they can be the target of an assignment statement DCL PRICES FILE VARIABLE, /* PRICES is a file variable */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-105 /154 */

RPRICE FILE; /* RPRICE is a file constant PRICES = RPRICE; OPEN FILE (PRICES);

The DD statement should associate the data set with the file constant RPRICE, which is the value of the file variable PRICES, thus: //RPRICE DD DSNAME= . . .

Use of a file variable also allows you to manipulate a number files at various times by a single statement. For example: DECLARE F FILE VARIABLE, A FILE, B FILE, C FILE, . . . DO F=A, B, C; READ FILE (F). . . ; . . . END; FILE ATTRIBUTE OF A DATA ITEM Main attributes are GROUP ALTERNATIVES Usage STREAM | RECORD Function INPUT | OUTPUT | UPDATE Access SEQUENTIAL | DIRECT Buffering BUFFERED | UNBUFFERED Scope EXTERNAL | INTERNAL Notes on Attributes RECORD STREAM INPUT OUTPUT UPDATE SEQUENTIAL DIRECT

DEFAULT STREAM INPUT DIRECT BUFFERED EXTERNAL

Associated verbs OPEN, CLOSE, READ, WRITE, REWRITE. DELETE Associated verbs OPEN, CLOSE, GET, PUT Input only Output only Valid only for RECORD mode. Transmission in either direction Valid in RECORD MODE only. Records accessed in physical sequence Valid in RECORD MODE only. Records accessed in any order by KEY. Implies KEYED attribute.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL [UN]BUFFERED ENVIRONMENT

Page:-106 /154

Record files only. BUFFERED is default for SEQUENTIAL VSAM | REGIONAL(1|2|3) | INDEXED | CONSECUTIVE F | FB | V | VB, BLKSIZE(N), RECSIZE (N),KEYLENGTH(N) Only RECORD files. It means records can be accessed using one of KEY, KEYTO, KEYFROM of data transmission statements

KEYED

Input / Output Verbs OPEN FILE (file-reference) [STREAM | RECORD] [INPUT | OUTPUT | UPDATE] [DIRECT | SEQUENTIAL] [ BUFFERED | UNBUFFERED] [KEYED] [PRINT] [TITLE (expression)] [LINESIZE(expression)] [PAGESIZ3E(expression)] /*Both forms of I/O*/ GET FILE(file-reference). PUT FILE(file-reference). READ FILE(file-reference).. WRITE FILE(file-reference) REWRITE FILE(file-reference). DELETE FILE(file-reference) CLOSE FILE (file-reference) /*Stream I/O*/ /*Stream I/O*/ /*Record I/O*/ /*Record I/O*/ /*Record I/O*/ /*Record I/O*/ /*Both forms I/O*/

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-107 /154

SECTION 11-A

STREAM ORIENTED DATA TRANSMISSION

Data sets with the STREAM attribute are processed by stream-oriented data transmission, which allows your PL/I program to ignore block and record boundaries and treat a data set as a continuous stream of data values in character form. For output, PL/I converts the data items from program variables into character form if necessary, and builds the stream of characters into records for transmission to the data set. For input, PL/I takes records from the data set and separates them into the data items requested by your program, converting them into the appropriate form for assignment to program variables. Defining Files using Stream I/O DCL filename FILE STREAM INPUT | {OUTPUT [PRINT]} ENVIRONMENT (options); options CONSECUTIVE F | FB | FS | FBS | V |VB | U RECSIZE (record-length) BLKSIZE (block-size) BUFFERS(n) Default Definitions created by the PL/I compiler DCL SYSPRINT FILE STREAM OUTPUT PRINT ENV(V BLKSIZE(129)); (120 characters of data, 1 ASA character , 4 bytes Block size, 4 Bytes Record size) DCL SYSIN FILE STREAM INPUT ENV (F BLKSIZE(80)); In your JCL include the following statements to use SYSPRINT and SYSIN //SYSPRINT DD SYSOUT=A //SYSIN /* DD *

Use the GET and PUT verbs for the I/O within your program Salient points of Stream I/O The I/O is in a continuos stream and record boundaries are not reflected to the user program, although they exist at the Physical level. The I/O is in character mode. For example coded arithmetic data is converted to character (display) mode both ways(From arithmetic mode to character mode on output and from character mode to coded mode on input).

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-108 /154

On input the data items must be in character mode and either BLANK or COMMA must be used as a separator. In PUT LIST for printer output the data is output in predetermined tab positions spaced at 21 positions between tabs. List Directed data Transmission Data Directed Data Transmission Edit Directed Data Transmission Verbs OPEN . /* see previous section */ CLOSE /* see previous section */ GET FILE(file-reference) data-specification [COPY (SYSPRINT | file-ref)] [SKIP (expression)] PUT FILE(file-reference) data-specification [PAGE] [LINE(expression)] [SKIP(expression)] PUT STRING(char-reference) data-specification /* SPRINTF of C */

Data Specifications LIST (data-item, data-item,.) DATA (data-item, data-item,.) EDIT (data-item, data-item,) (format-item, format-item,) Notes COPY Specifies that source data stream is written without alteration on the specified data set GET FILE(SYSIN) DATA(A,B,C) COPY(DPL); Specifies a new current line (or record ) in the data set; PUT LIST (X,Y,Z) SKIP(3); outputs on SYSPRINT after skipping three lines Only for PRINT files. Starts a new page. Only for PRINT files. Changes line counter value.

SKIP

PAGE LINE

Example DCL INVEN FILE INPUT STREAM ENV(options); ENV options are BLKSIZE, RECSIZE etc. Note that this can be omitted if it is specified in the JCL Note that Filename is the DD name in the run JCL for this program.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-109 /154

INPUT or OUTPUT attribute can be specified with OPEN statement also. This can be useful when the same file is opened / closed / opened / closed in one run first with one mode and then with another. Default is INPUT unless the file has the PRINT attribute in which case it defaults to OUTPUT. Example DCL INVEN FILE STREAM INPUT ENV(F BLKSIZE(80)); Print Attribute Use this for printer output. This will enable options on PUT like PAGE, SKIP( ), LINE( ) etc. This attribute can be used only when STREAM and OUTPUT are other attributes. This attribute causes first byte of each record of the data set to be reserved for carriage control character DCL PRINTR FILE OUTPUT STREAM ENV(F BLKSIZE(61)); Note record size is size of print line plus one position DCL PRINTR FILE OUTPUT STREAM PRINT ENV(F BLKSIZE(133)); DCL PRINTBUF CHAR(132); PUT FILE(PRINTR) LIST(PRINTBUF); Without print attribute the following statements are used DCL PRINTR FILE OUTPUT STREAM ENV(F BLKSIZE(132)); DCL AREA CHAR(130); PUT FILE(PRINTR) LIST(AREA); Without PRINT attribute PUT encases character strings in quotes. For the quotes two positions are needed (hence AREA is only 130 chars wide). Also note that the BLKSIZE is only 132 as we do not need a carriage control character. DCL I FIXED BINARY(15); I=LINENO(PRINTR) /*returns value of the LINENO counter maintained by PL/I */ Stream Files are automatically opened with first GET or PUT. PAGESIZE and LINESIZE for PRINT files should be specified only in OPEN statement. OPEN FILE(PRINTR) PAGESIZE(50) LINESIZE(120); Default output is to SYSPRINT with default line size of 120.Override this by OPEN FILE(SYSPRINT) LINESIZE(133); The TITLE attribute in the OPEN statement is used to specify the DD name OPEN FILE(DATAIN) TITLE(FILEA); /* FILEA is the DD name*/ Note that FILE attributes can be specified in the JCL, DCL and OPEN statements. They are merged at OPEN time.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-110 /154

The CLOSE FILE(DATAIN); statement is used to close the file. Program end also automatically closes all files Edit Directed I/O Edit Directed I/O overcomes some of the limitations of LIST directed I/O like need for spaces or commas to separate data (wastage of space) or printing at predetermined tab positions for printer output. Edit I/O like Stream I/O deals only in character data. Consider the following Data which is intended to be read with LIST directed I/O GET LIST(EMPNUM,EMPNAME); Employee Employee Number Name 12345678,SRINIVASKAMATH Note the comma separating the two data items For EDIT directed I/O GET FILE(filename) EDIT(datalist) (formatlist); GET EDIT(EMPNUM,EMPNAME) (COLUMN(1),F(8),A(15)); Employee Employee Number Name 12345678SRINIVAS KAMATH Note the absence of blank or comma to separate the two data items Format identifiers are A(n) n Alphameric characters F(m,n) Fixed point with the precision of m,n X(n) Skip n positions COLUMN(n) Data starts from column n Note that in the event of insufficient format identifiers the set of identifiers provided are used again starting with the first format identifier GET EDIT(A,B,C) (F(4),F(5)); A -> F(4) B -> F(5) C -> F(4) Excess format identifiers are ignored. The I/O continues across record boundaries until all the identifiers have been read or written A B 1 70 71 80 1 30

DCL

CHAR(70);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL DCL B CHAR(40); GET EDIT (A,B) (A(70),A(40)); A 1 70 1 B 40 Record boundary

Page:-111 /154

DCL A CHAR(70); DCL B CHAR(40); Record boundary GET EDIT (A,B) (COLUMN(1),A(70),COLUMN(1),A(40)); Alternately, DCL A CHAR(70); DCL B CHAR(40); GET EDIT (A,B) (A(70),X(10),A(40)); Example: To read first forty positions of each record DCL FIELD_1 CHAR(40); DO I=1 to 10; GET EDIT(FIELD_1) (COLUMN(1),A(40)); END; Data items may be Data aggregates Example: DCL TABLE(100) FLOAT DEC(6); GET EDIT(TABLE) (COLUMN(1),F(6,2)); /* note that 100 records will be read to satisfy this read DCL TABLE(100) FLOAT DEC(6); GET EDIT((TABLE(K) DO K=1 TO 50)) (COLUMN(1),F(6,2)); /* note that 50 records will be read to satisfy this read DCL TABLE(100) FLOAT DEC(6); GET EDIT(TABLE) (50 F(3),50 F(4)); /* different data formats for each of the 50 elements */ DCL A CHAR(70); GET EDIT (SUBSTR(A,50,20)) (A(20)); /* read into last 20 positions of A PUT EDIT ('MY NAME') (A(7)); PUT EDIT(A*3,B+C/D) (F(10), F(8,2)); More than one EDIT list and FORMAT list may be specified GET EDIT(data list) (format list) (data list) (format list) (data list) (format list) Example: PUT EDIT (A,B,C) (F(12),F(15,3),A(5))

*/

*/

*/

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL (D,E,F) (B(10),F(5,2),COLUMN(60),A(7)); I/O can be to a string in memory rather than to a FILE

Page:-112 /154

PUT STRING (FIELD_1) EDIT (A,B,C) (A(20),A(10),F(3)); GET STRING (FIELD_1) EDIT (A,B,C) (A(20),A(10),F(3)); This option can be used to effect CHAR to coded arithmetic or vice versa conversion. Writing Headings PUT EDIT('THIS IS A HEADING STARTING IN COLUMN 32') (PAGE,COLUMN(32),A); /*the compiler calculates width of A*/ PUT EDIT(MM,'/',DD,'/',YY) (A,A,A,A,A); PUT EDIT(MM,'/',DD,'/',YY) (5 A); PUT EDIT('EMPLOYEE NO. NAME') (SKIP(2),A) ('RATE HOURS DEDUCTION NET PAY') (COL(40),A); PUT EDIT (A,B,C,D,E,F) (PAGE,3(F(5),F(3,1))); /* format is equivalent to (F(5),F(3,1), F(5),F(3,1), F(5),F(3,1))*/ Format Identifiers A(w) Character field of width w A B(w) B E(w,d) Character field of width determined by the data item it represents Bit field of width w Bit field of width determined by the data item it represents Floating point notation (e.g. 60E+12). W is the total width of the character representation including decimal point (if any) signs and the designation E. For example 156.35E+05 , E(10,2), w is 10. The number of significant digits is d+1 where d digits are the fractional portion. The above value therefore is output as bb1.56E+07 Field contains w characters containing a fixed point decimal value. 123 F(3) 123 -123 F(3) SIZE error -123 F(4) -123 123 F(5) bb123 Width of character field is w and d positions are to right of decimal point. 123.45 F(4,0) b123 123.45 F(6,2) 123.45 123.45 F(7,3) 123.450 123.45 F(6,1) b123.5 123.45 F(5,2) SIZE error 123.65 F(4,0) b124 Skip w characters in input or output Picture format characters $ + - Z 9 v / , . B.

F(w)

F(w,d)

X(w) P

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-113 /154

Used to edit numeric data in fixed decimal point format. COLUMN(n) LINE(n) PAGE SKIP Start from Column n Skip to line n Skip to new page (output only) On input continue with next record On output SKIP(n) where n=0 suppress line feed, n=1 print on next line, n = expression where n-1 lines are skipped.

Data Directed Input Each item in the input is in the form of an assignment statement that specifies both value and the identity of the variable to which it is to be assigned. Example: The input stream could contain A=12.13,B=570,C='SED',D='1011'B; GET DATA(A,B,C,D); GET DATA(B,D,A,C); /* would work just as well */ */

GET DATA(A,B,C,D,E); /* not an error. E is superfluous and is ignored GET DATA(A,C,D); /*ERROR!. Identifier in input stream but not in ET*/

/* this raises the NAME condition which can be handled */ /* by an ON NAME(SYSIN) */ GET DATA; /* all items in stream up to the semicolon are input. However the data names must have been defined prior to this point */

The COUNT(filename) returns the data items input during last GET DCL INFILE FILE INPUT STREAM; GET FILE(INFILE) DATA; I=COUNT(INFILE); Data Directed Output DCL A FIXED DEC(5) INIT(0); DCL B FIXED DEC(5) INIT(0); DCL C FIXED DEC(5) INIT(175); PUT DATA (A,B,C); Outputs -> A=0 B=0 C=175; Note that A is in TAB position 1 B is in TAB position 25 C is in TAB position 49

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-114 /154

Stream File Conditions ENDFILE End of File has been reached ENDPAGE On printed output end of a page of output has been completed TRANSMIT Input or Output device did not transmit correctly RECORD Size of record in the media does not match that declared in the PL/I program. SIZE Number of significant digits in the coded arithmetic item exceeds the size declared for the identifier. Input -> -123 GET EDIT (VALUE) (F(3)); CONVERSION The data has embedded characters which are illegal for type of data item declared Input 1 5 57 98 GET EDIT(VALUE) (COLUMN(1),F(5)); Example of stream I/O //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD CHAR(20) INIT('HELLO WORLD'); PUT LIST (FIELD); GET LIST (FIELD); PUT SKIP LIST(FIELD); PUT SKIP EDIT(FIELD,FIELD) (A(8),X(1),A(1)); END MYPROG; /* //GO.SYSIN DD * 'SRINIVAS KAMATH' /* // Example of stream I/O //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD1 FIXED DECIMAL(7,2); DCL FIELD2 FIXED DECIMAL(7,2); DCL SUM FIXED DECIMAL(7,2); GET LIST (FIELD1,FIELD2); SUM=FIELD1+FIELD2; PUT SKIP LIST(SUM); END MYPROG; /* //GO.SYSIN DD * 123.4 567.8 /* //

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL Example of stream I/O //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL FIELD1 FIXED DECIMAL(7,2); DCL FIELD2 FIXED DECIMAL(7,2); DCL SUM FIXED DECIMAL(7,2); GET DATA (FIELD1,FIELD2); SUM=FIELD1+FIELD2; PUT SKIP LIST(SUM); END MYPROG; /* //GO.SYSIN DD * FIELD1=1.23 FIELD2=3.45; /* //

Page:-115 /154

More Complex Examples Of creation of a data set with stream oriented Data Transmission. The data read from the input stream by the file SYSIN includes a field VREC that contains five unnamed 7 character sub-fields. The field NUM defines the number of these subfields that contain information. //EX7#2 JOB //STEP1 EXEC IEL1CLG //PLI.SYSIN DD * PEOPLE: PROC OPTIONS (MAIN); DCL WORK FILE STREAM OUTPUT, 1 REC, 2 FREC, 3 NAME CHAR(19), 3 NUM CHAR(1), 3 PAD CHAR(25), 2 VREC CHAR(35), EOF BIT(1) INIT (0B), IN CHAR(80) DEF REC; ON ENDFILE (SYSIN) EOF=1B; OPEN FILE(WORK) LINESIZE (400); GET FILE (SYSIN) EDIT (IN) (A(80)); DO WHILE ( EOF); PUT FILE (WORK) EDIT(IN) (A(45+7*NUM)); GET FILE (SYSIN) EDIT (IN) (A(80)); END; CLOSE FILE(WORK); END PEOPLE; /* //GO.WORK DD DSN=HPU8.PEOPLE, DISP=(NEW, CATLG), UNIT=SYSDA, // SPACE=(TRK, (1,1)) //GO.SYSIN DD *
R.C.ANDERSON B.F.BENNETT R.E.COLE 0 202848 DOCTOR 2 771239 PLUMBER 5 698635 COOK VICTOR ELLEN HAZEL VICTOR JOAN ANN OTTO

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL


J.F.COOPER A.J.CORNELL E.F.FERRIS

Page:-116 /154

5 418915 LAWYER FRANK CAROL DONALD NORMAN BRENDA 3 237837 BARBER ALBERT ERIC JANET 4 158636 CARPENTER GERALD ANNA MARY HAROLD

/* Example of Accessing a Data Set with Stream I/O //EX7#5 JOB //STEP1 EXEC IEL1CLG //PLI.SYSIN DD * PEOPLE: PROC OPTIONS(MAIN); DCL WORK FILE STREAM INPUT, 1 REC, 2 FREC, 3 NAME CHAR(19), 3 NUM CHAR(1), 3 SERNO CHAR(7), 3 PROF CHAR(18), 2 VREC CHAR(35), IN CHAR(80) DEF REC, EOF BIT(1) INIT(0B); ON ENDFILE (WORK) EOF=1B; OPEN FILE (WORK); GET FILE (WORK) EDIT(IN,VREC) (A(45), A(7*NUM)); DO WHILE ( EOF); PUT FILE (SYSPRINT) SKIP EDIT (IN) (A); GET FILE (WORK) EDIT (IN, VREC) (A(45), A(7*NUM)); END; CLOSE FILE (WORK); END PEOPLE; /* //GO.WORK DD DSN=HPU8.PEOPLE, DISP=(OLD,DELETE) // More on PRINT Files and Stream I/O The operating system allows you to use the first byte of each record for a print control character. The control characters, which are not printed, cause the printer to skip to a new line or page. In a PL/I program, with stream I/O, the use of a PRINT attribute for a file provides a convenient means of controlling the layout of printed output from streamoriented data transmission. The compiler automatically inserts print control characters in response to the PAGE, SKIP, and LINE options and format items. You can apply the PRINT attribute to any STREAM OUTPUT file, even if you do not intend to print the associated data set directly. Character Blank 0 + 1 Action Space 1 line before printing Space 2 lines before printing Space 3 lines before printing No space before printing Start new page

Controlling Printed Line length

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-117 /154

You can limit the length of the printed line produced by a PRINT file either by specifying a record length in your PL/I program (ENVIRONMENT attribute) or in a DD statement, or by giving a line size in an OPEN statement (LINESIZE option). The record length must include the extra byte for the print control character, that is, it must be 1 byte larger than the length of the printed line (9 bytes larger for V-format records).The value you specify in the LINESIZE option refers to the number of characters in the printed line; the compiler adds the print control character. Overriding the Tab Control Table Data-directed and list-directed output to a PRINTFILE are aligned on preset tabulator positions. The definitions of the fields in the table are as follows. OFFSET OF TAB COUNT: Half word binary integer that gives the offset of Tab count, the field that indicates the number of tabs to be used. PAGESIZE: Half word binary integer that defines the default page size. This page size is used for dump output to the PLIDUMP data set as well as for stream output. LINESIZE: Half word binary integer that defines the default line size. PAGELENGTH: Half word binary integer that defines the default page length for printing at a terminal. For TSO and VM, the value 0 indicates unformatted output . FILLERS: Three Half word binary integers; reserved for future use. TAB COUNT: Half word binary integer that defines the number of tab position entries in the table (maximum 255), If TAB COUNT=0, any specified tab positions are ignored. TAB1-TABN: n half-word binary integers that define the tab positions within the print line. The first position is numbered 1, and the highest position is numbered 255 The value of each tab should be greater than that of the tab preceding it in the table; otherwise, it is ignored. The first data field in the printed output begins at the next available tab position. You can override the default PL/I tab settings for your program by causing the linkage editor to resolve an external reference to PLITABS. To cause the reference to be resolved, supply a table with the name PLITABS, in the format described above. Note that TAB1 identifies the position of the second item printed on a line; the first item on a line always starts at the left margin. DCL 1 PLITABS STATIC EXT, 2 (OFFSET INIT(14), PAGESIZE INIT (60), LINESIZE INIT (120), PAGELENGTH INIT (0), FILL1 INIT (0),

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL FILL2 INIT (0), FILL3 INIT (0), NO_OF_TABS INIT (3), TAB1 INIT (30), TAB2 INIT(60), TAB3 INIT(90)) FIXED BIN (15, 0); Example of changing TAB settings, conversions MYPROG: PROCEDURE OPTIONS(MAIN); DCL A CHAR(6) INIT('123.45'); DCL B FIXED DECIMAL(5,2); DCL C FIXED BINARY(15); DCL (D,E) DECIMAL FLOAT(6); DCL F CHAR(6) INIT('10'); DCL 1 PLITABS STATIC EXTERNAL, 2 (OFFSET INIT(14), PAGESIZE INIT(60), LINESIZE INIT(120), PAGELENGTH INIT(0), FILL1 INIT(0), FILL2 INIT(0), FILL3 INIT(0), N0_OF_TABS INIT(3), TAB1 INIT(15), TAB2 INIT(30), TAB6 INIT(45)) FIXED BIN(15,0); B=A;C=A; PUT SKIP LIST(A,B,C); A='-234.56'; B=A;C=A; PUT SKIP LIST(A,B,C); D=1.23456E+3;E=1.44E-3; PUT SKIP LIST(D,E,(D+E)); B=A/F; PUT SKIP LIST(A,F,B); END MYPROG;

Page:-118 /154

Example of creating a print file Via Stream Data Transmission %PROCESS NOT('~'); CARDPRNT: PROC OPTIONS (MAIN); DCL TABLE FILE STREAM OUTPUT PRINT; DCL PGNO FIXED DEC(2) INIT(1); DCL ONCODE BUILTIN; DCL EOF BIT(1) INIT('0'B); DCL CARD CHAR(80); ON ENDFILE(SYSIN) EOF INIT('1'B); ON ERROR BEGIN; ON ERROR SYSTEM; PUT LIST ('ONCODE = '|| ONCODE); END;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-119 /154

ON ENDPAGE (TABLE) BEGIN; PUT FILE (TABLE) PAGE EDIT (CARD TO PRINT PGNO= ,PGNO) (A,F(2)); PGNO = PGNO +1; END; OPEN FILE (TABLE) PAGESIZE(52) LINESIZE (80); SIGNAL ENDPAGE(TABLE); GET LIST(CARD); DO WHILE ~EOF; PUT FILE (TABLE) EDIT(CARD)(A); GET LIST(CARD); END; END CARDPRNT;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-120 /154

SECTION 11-B
Define Files (DASD, TAPE or CARD) Declare I/O Areas Use READ and WRITE (instead of GET and PUT)

RECORD I/O

DCL file-constant FILE ..attributes Verbs for Record Oriented Data Transmission only READ FILE(file-reference) [INTO(reference) [ KEY(expression) | KEYTO(reference) ]] READ FILE(file-reference) [SET(pointer-reference) [KEY(expression) | KEYTO(reference) ] ] WRITE FILE (file-reference) FROM(reference) [KEYFROM(expression) | KEYTO(reference) ]] DELETE FILE(file-reference) [KEY(expression)] Notes INTO FROM SET KEY Specifies Data area in form of element or aggregate variable into which the logical record is read. Specifies the element or aggregate from which the record is written. Specifies a pointer variable that is set to point to the location in the buffer into which the record has been moved during the READ operation Specifies character key that identifies a record for a INDEXED, REGIONAL, VSAM KSDS data set. Can appear only if the FILE has DIRECT attribute or INDEXED or VSAM and KEYED attribute. Can be used in a READ statement for INPUT or UPDATE file or in a REWRITE for a DIRECT UPDATE file. Example READ FILE (STOCK) INTO (ITEM ) KEY (STKEY ); Specifies a character key that identifies the record in the data set. Can be used in a WRITE statement for SEQUENTIAL output , or DIRECT OUTPUT or DIRECT UPDATE in a REGIONAL FILE . Can also be used in WRITE statement with KEYED SEQUENTIAL UPDATE VSAM data set. For REGIONAL (2) and (3) data sets KEYFROM specifies a recorded key whose length is specified by the KEYLEN sub-parameter or KEYLENGTH option.

KEYFROM

Example WRITE FILE(LOANS) FROM (LOANREC) KEYFROM(LOANNO); KEYTO Specifies the character variable to which the key of a record is assigned. Variables with PIC and STRING cannot be used. Applies only to KEYED files of INDEXED, VSAM or REGIONAL organisation.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-121 /154

For REGIONAL(1) 8 character region number padded or truncated on left (if required) is moved to the KEYTO field For REGIONAL(2) or (3) the recorded key without the region number (padded or truncated on left if required) is moved For VSAM KSDS the recorded key padded or truncated on right (if required) is moved For ESDS a 4 character RBA padded or truncated on right (if required is moved) For VSAM RRDS a 8 character relative record number padded or truncated on the left is moved Example: DCL INFILE FILE INPUT RECORD ENV(F RECSIZE(80)); DCL OUTFILE FILE OUTPUT RECORD ENV(F RECSIZE(80)); DCL DATA_AREA CHAR(80); /* keyword RECORD indicates it is record I/O */ OPEN FILE(INFILE),FILE(OUTFILE); . READ FILE(INFILE) INTO (DATA_AREA); WRITE FILE(OUTFILE) FROM(DATA_AREA); . . CLOSE FILE(INFILE),FILE(OUTFILE); Carriage Control in RECORD I/O Append a carriage control character at start of the Record CC Code Action blank Space one line before printing 0 Space two lines before printing Space three lines before printing + Suppress space before printing 1 Skip to channel 1 before printing 2 Skip to channel 2 before printing . . 9 Skip to channel 9 before printing A Skip to channel 10 before printing . C Skip to channel 12 before printing To indicate to PL/I that ASA (American Standards Association) characters are being used on printer output add the keyword CTLASA / CTL360 to the ENV options of the file declaration. However note that with CTLASA the carriage movement takes place before printing. With CTL360 it occurs after the printing. Example: DCL DATA_AREA CHAR(80); DCL PRINT_AREA CHAR(81); DCL PRINTR FILE OUTPUT RECORD ENV(F RECSIZE(81) CTLASA ); PRINT_AREA = '1' || DATA_AREA; /* ASA character is now '1' */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL WRITE FILE(OUTFILE) FROM(PRINT_AREA); Consecutive Data Sets Used for both STREAM as well as RECORD oriented I/O Records written in order of presentation (sequentially) Records can be fetched sequentially only.

Page:-122 /154

Stream oriented operation on consecutive data sets Use LIST or EDIT or DATA directed I/O for stream operation DCL FILE STREAM INPUT|OUTPUT [PRINT] ENVIRONMENT( options); /* This has been covered extensively in an earlier section */ Environment options are F | FB | V | VB | U RECSIZE(record-length) BLKSIZE(block-size) Type of record organization supported F Fixed-length, unblocked FB Fixed-length, blocked V Variable-length, unblocked VB Variable-length, blocked VS Variable-length, unblocked, spanned VBS Variable-length, blocked, spanned In a sequential (or CONSECUTIVE) data set, records are placed in physical sequence. An indexed sequential (or INDEXED) data set must reside on a direct-access volume. An index or set of indexes maintained by the operating system gives the location of certain principal records. This allows direct retrieval, replacement, addition, and deletion of records, as well as sequential processing. A direct (or REGIONAL) data set must reside on a direct-access volume. The records within the data set can be organised in three ways: REGIONAL(1), REGIONAL(2), and REGIONAL(3); in each case, the data set is divided into regions, each of which contains one or more records. A key that specifies the region number and, for REGIONAL(2) and REGIONAL(3), identify the record, allows direct-access to any record; sequential processing is also possible. An example of a typical FILE declaration for a VSAM KSDS is DCL FILENAME FILE RECORD SEQUENTIAL KEYED INPUT ENV ( VSAM); ENVIRONMENT option F | FB | V | VB . RECSIZE BLKSIZE CTLASA | CTL360 KEYLENGTH DCB sub-parameter RECFM=F | FB |. LRECL BLKSIZE KEYLEN

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-123 /154

V Format C1 C2 RECORD1 C1 C2 RECORD2

VB Format C1 C2 RECORD1 C2 RECORD2

VS Format C1 C2 RECORD1 FULL VBS Format C1 C2 RECORD1 C2 RECORD2 ENTIRE 1ST SEGM C1 C2 C1 C2 RECORD2 C2 RECORD3 LAST SEG C1 C2 RECORD2 FIRST SEGMENT C1 C2 RECORD2 LAST SEGMENT

Block control information Record or segment control information

RECORD-LENGTH is sum of: The length required for data. For variable-length and undefined-length records, this is the maximum length. Any control bytes required. Variable-length records require 4 (for the record-length prefix); fixed-length and undefined-length records do not require any. Additional 4 bytes needed for block control information. you can specify record-length as an integer or as a variable with attributes FIXED BINARY (31,0) STATIC. BLKSIZE Option: The BLKSIZE option specifies the maximum block size on the data set. block-size is the sum of the total length(s) of one of the following: A single record A single record and either one or two record segments Several records Several records and either one or two record segments Two record segments A single record segment. For variable-length records, the length of each record or record segment includes the 4 control bytes for the record or segment length. These quantities are in addition to the 4 control bytes required for each block. Block-size can be specified as an integer, or as a variable with attributes FIXED BINARY(31,0) STATIC.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-124 /154

GENKEY : The GENKEY (generic key) option applies only to INDEXED and VSAM key-sequenced data sets. It enables you to classify keys recorded in a data set and to use a SEQUENTIAL KEYED INPUT or SEQUENTIAL KEYED UPDATE file to access records according to their key classes. A generic key is a character string that identifies a class of keys; all keys that begin with the string are members of that class. For example, the recorded keys ABCD, ABCE, and ABDF are all members of the classes identified by the generic keys A and AB, and the first two are also members of the class ABC; and the three recorded keys can be considered to be unique members of the classes ABCD, ABCE, and ABCF, respectively. DCL IND FILE RECORD SEQUENTIAL KEYED UPDATE ENV (VSAM GENKEY); . . . READ FILE(IND) INTO(INFIELD) KEY (ABC); . . . NEXT: READ FILE (IND) INTO (INFIELD); . . . GO TO NEXT; KEYLENGTH Option: Use the KEYLENGTH option to specify the length of the recorded key for KEYED files where n is the length. You can specify KEYLENGTH for INDEXED or REGIONAL(3) files. If you include the KEYLENGTH option in a VSAM file declaration for checking purposes, and the key length you specify in the option conflicts with the value defined for the data set, the UNDEFINEDFILE condition is raised. DEFINING AND USING CONSECUTIVE DATA SETS In a data set with consecutive organisation, records are organised solely on the basis of their successive physical positions; when the data set is created, records are written consecutively in the order in which they are presented. You can retrieve the records only in the order in which they were written, or, for RECORD I\O only, also in the reverse order when using the BACKWARDS attribute. Defining Files Using Record I/O DCL filename FILE RECORD INPUT | OUTPUT | UPDATE SEQUENTIAL BUFFERED | UNBUFFERED ENVIRONMENT (options); Some important ENVIRONMENT options applicable to consecutive data sets are:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL F | FB | V | VB |.. RECSIZE (record-length) BLKSIZE (block-size) CONSECUTIVE | VSAM | RELATIVE(n) CTLASA | CTL360

Page:-125 /154

EXAMPLE OF PRINTING RECORD ORIENTED DATA TRANSMISSION PRT: PROC OPTIONS(MAIN); DCL TABLE FILE RECORD INPUT SEQUENTIAL; DCL PRINTER FILE RECORD OUTPUT SEQL ENV(V BLKSIZE(89) CTLASA); DCL LINE CHAR(81) VAR; DCL CTLCHAR CHAR(1) DEFINED LINE; DCL DATA CHAR(80) DEFINED LINE POS(2); DCL TABLE_EOF BIT(1) INIT(0B); DCL TRUE BIT(1) INIT(1B); DCL FALSE BIT(1) INIT(0B); ON ENDFILE(TABLE) TABLE_EOF = TRUE; OPEN FILE(TABLE), FILE(PRINTER); READ FILE(TABLE) INTO(DATA); CTLCHAR = 1; /* ASA CHAR */ DO WHILE (TABLE_EOF = FALSE); WRITE FILE(PRINTER) FROM(LINE); READ FILE(TABLE) INTO(DATA); END; CLOSE FILE(TABLE), FILE(PRINTER); END PRT; /* priming read */ /*eof flag for table /* constant true /* constant false */ */ */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-126 /154

SECTION 11-C

DEFINING AND USING VSAM DATA SETS

Before you execute a program that accesses a VSAM data set, you need to know: The name of the VSAM data set The name of the PL/I file Whether you intend to share the data set with other users Then you can write the required DD statement to access the data set: //filename DD DSNAME=dsname,DISP=OLD | SHR

For example, if your file is named PL1FILE, your data set named VSAMDS, and you want exclusive control of the data set, enter: // PL1FILE DD DSNAME=VSAMDS, DISP=OLD

To share your data set, use DISP=SHR. PL/l provides support for three types of VSAM data sets: Key-sequenced data sets (KSDS) Entry-sequenced data sets (ESDS) Relative record data sets (RRDS). These correspond roughly to PL/l indexed, consecutive, and regional data set organisations, respectively. They are all ordered, and they can all have keys associated with their records. Both sequential and keyed access are possible with all three types. Although only key-sequenced data set have keys as part of their logical records, keyed access is also possible for entry-sequenced data sets (using relative-byte addresses) and relative record data sets (using relative record numbers). All VSAM data sets are held on direct-access storage devices, and a virtual storage operating system is required to use them. VSAM does not use the concept of blocking, records need not be of a fixed length. The data items are arranged in control intervals, in control areas. A control interval can contain one or more logical records, VSAM data sets can have two types of indexes-prime and alternate. You can have one or more alternate indexes on a KSDS or an ESDS. Any change in a data set that has alternate indexes must be reflected in all the indexes if they are to remain useful. This activity is known as index upgrade, and is done by VSAM for any index in index upgrade set of the data set. Before using a VSAM data set for the first time, you need to define it to the system with the DEFINE command of Access Method Services, the operation of writing the initial data into a newly create VSAM data set is referred to as loading. Use the three different types of data sets according to the following purposes:

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-127 /154

Use entry-sequenced data sets for data that you primarily access in the order in which it was created (or the reverse order). Use key-sequenced data sets when you normally access records through keys within the records (for example, a stock-control file where the part number is used to access a record). Use relative record data sets for data in which each item has a particular number. you can access records in all types of VSAM data sets either directly by means of a key, or sequentially (backward or forward). You can also use a combination of the two ways: Select a starting point with a key and then read forward or backward from that point. Keys for Indexed VSAM Data Sets Keys for key-sequenced data sets and for entry-sequenced data sets accessed via an alternate index are part of the logical records recorded on the data set. You define the length and location of the keys when you create the data set. You can reference the keys in the KEY, KEYFROM, and KEYTO options Relative Byte Addresses (RBA) Relative byte addresses allow you to use keyed access on an ESDS associated with a KEY SEQUENTIAL file. The RBA, or keys, are character strings of length 4, and their values are defined by VSAM. You cannot construct or manipulate RBA in PL/l; obtain the RBA for a record by using the KEYTO option, on a WRITE or on a READ statement. Subsequently use an RBA in the KEY option of a READ or REWRITE statement. Relative Record Numbers Records in an RRDS are identified by a relative record number that starts at 1 and is incremented by 1 for each succeeding record. Keys used as relative record numbers are character strings of length 8. Source key you use in the KEY or KEYFROM option must represent an unsigned integer. If not 8 characters truncated or padded with blanks (interpreted as Zeros) on the left. You define a direct VSAM data set by using a file declaration with the following attributes: DCL filename FILE RECORD INPUT | OUTPUT | UPDATE DIRECT [KEYED] ENVIRONMENT (options); Some of the ENVIRONMENT options applicable to VSAM data sets are: BUFND (n) BUFNI (n) GENKEY PASSWORD (password - specification) VSAM Defining VSAM data Sets

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-128 /154

Use the DEFINE CLUSTER command of Access Method Services to define and catalogue VSAM data sets. To use the DEFINE command, you need to know: The name and password of the master catalogue if the master catalogue is password protected The name and password of the VSAM private catalogue you are using if you are not using the master catalogue Whether VSAM space for your data set is available The type of VSAM data set you are going to create The volume on which your data set us to be placed The average and maximum record size in your data set The position and length of the key for an indexed data set The space to be allocated for your data set How to code the DEFINE command How to use the Access Method Services program. When you have the information, you are in a position to code the DEFINE command and then define and catalogue the data set using Access Method Services. EXAMPLE OF DEFINING AND LOADING A VSAM ESDS EXAMPLE OF DEFINING A VSAM ESDS CLUSTER //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //STEP1 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //SYSIN DD * DEFINE CLUSTER (NAME(OZA058.VSAM.ESDS.PLI)VOLUMES(TSO001)NONINDEXED RECORDSIZE(80 80)TRACKS(1 1)) /* // EXAMPLE OF LOADING A VSAM ESDS //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL FAMFILE FILE SEQUENTIAL OUTPUT ENV(VSAM); DCL IN FILE RECORD INPUT; DCL STRING CHAR(80); DCL NOTEOF BIT(1) INIT('1'B); ON ENDFILE(IN) NOTEOF='0'B; READ FILE(IN) INTO(STRING); DO I=1 BY 1 WHILE(NOTEOF); PUT FILE(SYSPRINT) SKIP EDIT(STRING)(A); WRITE FILE(FAMFILE) FROM(STRING); READ FILE(IN) INTO(STRING);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-129 /154

END; PUT SKIP EDIT(I-1,'RECORDS PROCESSED')(F(3),A); END MYPROG; /* //GO.FAMFILE DD DSN=OZA058.VSAM.ESDS.PLI,DISP=OLD //GO.IN DD * AAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBB /* //STEP2 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //SYSIN DD * PRINT INDATASET(OZA058.VSAM.ESDS.PLI) CHAR /* // EXAMPLE OF DUMPING A ESDS //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL FAMFILE FILE SEQUENTIAL INPUT ENV(VSAM); DCL BADFILE FILE SEQUENTIAL INPUT ENV(VSAM); DCL STRING CHAR(80); DCL NOTEOF BIT(1) INIT('1'B); ON ENDFILE(FAMFILE) NOTEOF='0'B; ON UNDEFINEDFILE(BADFILE) PUT SKIP LIST(' BADFILE NOT OPENED'); OPEN FILE(BADFILE); READ FILE(FAMFILE) INTO(STRING); DO I=1 BY 1 WHILE(NOTEOF); PUT SKIP EDIT('RECORD NO.',I,STRING)(A,F(3),X(2),A); READ FILE(FAMFILE) INTO(STRING); END; CLOSE FILE(FAMFILE); END MYPROG; /* //GO.FAMFILE DD DSN=OZA058.VSAM.ESDS.PLI,DISP=OLD // Loading a KSDS or Indexed ESDS Open the file for KEYED SEQUENTIAL OUTPUT and present the records in ascending key order, use the KEYFROM option. If the KSDS already contains some records, and you open the associated file with the SEQUENTIAL and OUTPUT attributes, you can only add records at the end of the data set. KEYED SEQUENTIAL OUTPUT file is used with a WRITE FROM KEYFROM statement. The data must be presented in ascending key order. EXAMPLE OF DEFINING A KSDS USING IDCAMS //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //STEP1 EXEC PGM=IDCAMS,REGION=512K

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL //SYSPRINT DD SYSOUT=* //SYSIN DD * DEFINE CLUSTER (NAME(OZA058.VSAM.KSDS.PLI)VOLUMES(TSO001)INDEXED KEYS(20 0) RECORDSIZE(23 80)TRACKS(3 1)) /* //

Page:-130 /154

Example of loading a VSAM KSDS sequentially //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL DIREC FILE RECORD SEQUENTIAL OUTPUT KEYED ENV(VSAM); DCL CARD CHAR(80); DCL NAME CHAR(20) DEF CARD POS(1); DCL NUMBER CHAR(3) DEF CARD POS(21); DCL OUTREC CHAR(23) DEF CARD POS(1); DCL NOTEOF BIT(1) INIT('1'B); ON ENDFILE(SYSIN) NOTEOF='0'B; ON KEY(DIREC) BEGIN; PUT SKIP EDIT('DUPLICATE KEY IN THIS INPUT:',CARD)(A); END; OPEN FILE(DIREC) OUTPUT; GET FILE(SYSIN) EDIT(CARD)(A(80)); DO WHILE(NOTEOF); WRITE FILE(DIREC) FROM(OUTREC) KEYFROM(NAME); GET FILE(SYSIN) EDIT(CARD)(A(80)); END; CLOSE FILE(DIREC); END MYPROG; /* //GO.DIREC DD DSN=OZA058.VSAM.KSDS.PLI,DISP=OLD //GO.SYSIN DD * ACTION,G. 162 BAKER,R. 152 BRAMLEY,O.H. 248 /* //STEP2 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //SYSIN DD * PRINT INDATASET(OZA058.VSAM.KSDS.PLI) CHAR /* // Example of reading a VSAM KSDS in direct mode //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-131 /154

//PLI.SYSIN DD * %PROCESS ATTRIBUTES(FULL); MYPROG: PROCEDURE OPTIONS(MAIN); DCL DIREC FILE RECORD DIRECT INPUT KEYED ENV(VSAM); DCL CARD CHAR(80); DCL NAME CHAR(20) DEF CARD POS(1); DCL NUMBER CHAR(3) DEF CARD POS(21); DCL INREC CHAR(23); DCL NOTEOF BIT(1) INIT('1'B); DCL GOODKEYFLAG BIT(1) INIT('1'B); ON ENDFILE(SYSIN) NOTEOF='0'B; ON KEY(DIREC) BEGIN; PUT SKIP EDIT('INVALID KEY IN INPUT RECORD=',CARD)(A); GOODKEYFLAG='0'B; END; OPEN FILE(DIREC); GET FILE(SYSIN) EDIT(CARD)(A(80)); DO WHILE(NOTEOF); READ FILE(DIREC) INTO(INREC) KEY(NAME); IF GOODKEYFLAG THEN PUT SKIP EDIT('RECORD IS= ',INREC)(A); GOODKEYFLAG='1'B; GET FILE(SYSIN) EDIT(CARD)(A(80)); END; CLOSE FILE(DIREC); END MYPROG; /* //GO.DIREC DD DSN=OZA058.VSAM.KSDS.PLI,DISP=OLD //GO.SYSIN DD * ACTION,G. 162 BAKER,R. 152 KAMATH,M.S 999 BRAMLEY,O.H. 248 /* // Using a Direct File to Access a KSDS or Indexed ESDS You can open a Direct file that is used to access an indexed VSAM data set with the INPUT, or OUTPUT attribute. If you use a DIRECT INPUT or DIRECT UPDATE file, you can read , write, rewrite, or delete records in the same way as for as a KEYED SEQUENTIAL file. EXAMPLE OF UPDATING A VSAM KSDS DYNAMICALLY //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * %PROCESS ATTRIBUTES(FULL),NOT('~'); MYPROG: PROCEDURE OPTIONS(MAIN); DCL DIREC FILE RECORD KEYED ENV(VSAM); DCL ONCODE BUILTIN; DCL OUTREC CHAR(23); DCL NAME CHAR(20) DEF OUTREC POS(1);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-132 /154

DCL NUMBER CHAR(3) DEF OUTREC POS(21); DCL CODE CHAR(1); DCL EOF BIT(1) INIT('0'B); ON ENDFILE(SYSIN) EOF='1'B; ON KEY(DIREC) BEGIN; IF ONCODE=51 THEN PUT FILE(SYSPRINT) SKIP EDIT ('NOT FOUND: ',NAME)(A(15),A); IF ONCODE=52 THEN PUT FILE(SYSPRINT) SKIP EDIT ('DUPLICATE: ',NAME)(A(15),A); END; OPEN FILE(DIREC) DIRECT UPDATE; GET FILE(SYSIN) EDIT(NAME,NUMBER,CODE) (COLUMN(1),A(20),A(3),A(1)); DO WHILE(~EOF); PUT FILE(SYSPRINT) SKIP EDIT(' ',NAME,'#',NUMBER,' ',CODE) (A(1),A(20),A(1),A(3),A(1),A(1)); SELECT (CODE); WHEN ('A') WRITE FILE(DIREC) FROM(OUTREC) KEYFROM(NAME); WHEN ('C') REWRITE FILE(DIREC) FROM(OUTREC) KEY(NAME); WHEN ('D') DELETE FILE(DIREC) KEY(NAME); OTHERWISE PUT FILE(SYSPRINT) SKIP EDIT ('INVALID CODE: ',NAME)(A(15),A); END; GET FILE(SYSIN) EDIT(NAME,NUMBER,CODE) (COLUMN(1),A(20),A(3),A(1)); END; CLOSE FILE(DIREC); PUT FILE(SYSPRINT) PAGE; OPEN FILE(DIREC) SEQUENTIAL INPUT; /* DUMP THE FILE */ EOF='0'B; ON ENDFILE(DIREC) EOF='1'B; READ FILE(DIREC) INTO(OUTREC); DO WHILE(~EOF); PUT FILE(SYSPRINT) SKIP EDIT(OUTREC)(A); READ FILE(DIREC) INTO(OUTREC); END; CLOSE FILE(DIREC); END MYPROG; /* //GO.DIREC DD DSN=OZA058.VSAM.KSDS.PLI,DISP=OLD //GO.SYSIN DD * KAMATH,M.S 501A ACTION,G. 162D /* // Codes used above are A Add a new record C Change the number of an existing name D Delete a record

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-133 /154

VSAM RELATIVE RECORD DATA SETS When an RRDS is being loaded, you must open the associated file for OUTPUT. Use either a DIRECT or a SEQUENTIAL file. For a DIRECT OUTPUT file, each record is placed in the position specified by the relative record number (or key) in the KEYFORM option of the WRITE statement. For a SEQUENTIAL OUTPUT file, use WRITE statements with or without the KEYFROM option. If you specify the KEYFROM option, the record is placed in the specified slot; if you omit it, the record is placed in the slot following the current position. EXAMPLE OF DEFINING A VSAM RRDS USING IDCAMS //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //STEP1 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //SYSIN DD * DEFINE CLUSTER (NAME(OZA058.VSAM.RRDS.PLI)VOLUMES(TSO001)NUMBERED RECORDSIZE(20 20)TRACKS(2 2)) /* // EXAMPLE OF LOADING A VSAM RRDS SEQUENTIALLY //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL NOS FILE RECORD OUTPUT DIRECT KEYED ENV(VSAM); DCL CARD CHAR(80); DCL NAME CHAR(20) DEF CARD POS(1); DCL NUMBER CHAR(2) DEF CARD POS(21); DCL IOFIELD CHAR(20); DCL NOTEOF BIT(1) INIT('1'B); ON ENDFILE(SYSIN) NOTEOF='0'B; OPEN FILE(NOS) OUTPUT; GET FILE(SYSIN) EDIT(CARD)(A(80)); DO WHILE(NOTEOF); PUT FILE(SYSPRINT) SKIP EDIT(CARD)(A); IOFIELD=NAME; WRITE FILE(NOS) FROM(IOFIELD) KEYFROM(NUMBER); GET FILE(SYSIN) EDIT(CARD)(A(80)); END; PUT SKIP LIST('END OF INPUT----'); CLOSE FILE(NOS); END MYPROG; /* //GO.NOS DD DSN=OZA058.VSAM.RRDS.PLI,DISP=OLD //GO.SYSIN DD * ACTION,G. 12

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL BAKER,R. 13 BRAMLEY,O.H. 28 /* //STEP2 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //SYSIN DD * PRINT INDATASET(OZA058.VSAM.RRDS.PLI) CHAR /* //

Page:-134 /154

EXAMPLE OF ACCESSING A VSAM RRDS RANDOMLY //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * %PROCESS NOT('~'); MYPROG: PROCEDURE OPTIONS(MAIN); DCL NOS FILE RECORD INPUT DIRECT KEYED ENV(VSAM); DCL ONCODE BUILTIN; DCL CARD CHAR(80); DCL NAME CHAR(20) DEF CARD POS(1); DCL NUMBER CHAR(2) DEF CARD POS(21); DCL IOFIELD CHAR(20); DCL NOTEOF BIT(1) INIT('1'B); DCL BADKEY BIT(1); ON ENDFILE(SYSIN) NOTEOF='0'B; OPEN FILE(NOS); ON KEY(NOS) BEGIN; PUT SKIP EDIT('INVALID KEY: ',NUMBER)(A,A); PUT EDIT(' ONCODE IS: ',ONCODE)(A,F(4)); BADKEY='1'B; END; GET FILE(SYSIN) EDIT(CARD)(A(80)); DO WHILE(NOTEOF); BADKEY='0'B; READ FILE(NOS) INTO(IOFIELD) KEY(NUMBER); IF ~BADKEY THEN PUT FILE(SYSPRINT) SKIP EDIT(IOFIELD)(A); GET FILE(SYSIN) EDIT(CARD)(A(80)); END; PUT SKIP LIST('END OF INPUT----'); CLOSE FILE(NOS); END MYPROG; /* //GO.NOS DD DSN=OZA058.VSAM.RRDS.PLI,DISP=OLD //GO.SYSIN DD * ACTION,G. 12 BAKER,R. 13 BRAMLEY,O.H. 28 KAMATH,M.S. 99 /* //STEP2 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //SYSIN DD *

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL PRINT INDATASET(OZA058.VSAM.RRDS.PLI) CHAR /* //

Page:-135 /154

VSAM ALTERNATE INDEXES Pairing an Alternate Index Path with a File When using an alternate index, you simply specify the name of the path in the DSNAME parameter if the DD statement associating the base data set/alternate index pair with your PL/l file. Given a PL/l file called PL1FILE and the alternate index path called PERSALPH, the DD statement required would be: //PL1FILE DD DSNAME=PERSALPH, DISP=OLD Alternate Indexes for KSDS or Indexed ESDSs

Three Access Method Services commands are used as below: DEFINE ALTERNATEINDEX defines the alternate index as a data set to VSAM. BLDINDEX places the pointers to the relevant records in the alternate index. DEFINE PATH defines an entity that can be associated with a PL/I file in a DD statement. EXAMPLE OF DEFINING A ALTERNATE INDEX FOR A VSAM KSDS FILE //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //STEP1 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //SYSIN DD * DEFINE ALTERNATEINDEX (NAME(OZA058.VSAM.KSDS.ALTINDX.PLI)VOLUMES(TSO001)UNIQUEKEY KEYS(3 20) RECORDSIZE(24 48)TRACKS(4 4)RELATE(OZA058.VSAM.KSDS.PLI)) /* // EXAMPLE OF THE ALTERNATE INDEX AND DEFINING THE PATH //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //STEP1 EXEC PGM=IDCAMS,REGION=512K //SYSPRINT DD SYSOUT=* //DD1 DD DSN=OZA058.VSAM.KSDS.PLI,DISP=OLD //DD2 DD DSN=OZA058.VSAM.KSDS.ALTINDX.PLI,DISP=OLD //SYSIN DD * BLDINDEX INFILE(DD1) OUTFILE(DD2) DEFINE PATH(NAME(OZA058.VSAM.KSDS.PATH.PLI)-

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL PATHENTRY(OZA058.VSAM.KSDS.ALTINDX.PLI)) /* //

Page:-136 /154

EXAMPLE OF ACCESSING THE KSDS FILE SEQUENTIALLY THROUGH THE ALTERNATE INDEX //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * %PROCESS ATTRIBUTES(FULL); MYPROG: PROCEDURE OPTIONS(MAIN); DCL DIREC FILE RECORD SEQUENTIAL INPUT ENV(VSAM); DCL INREC CHAR(23); DCL NOTEOF BIT(1) INIT('1'B); OPEN FILE(DIREC); ON ENDFILE(DIREC) NOTEOF='0'B; DO WHILE(NOTEOF); READ FILE(DIREC) INTO(INREC); IF NOTEOF THEN PUT SKIP EDIT('RECORD IS= ',INREC)(A); END; CLOSE FILE(DIREC); END MYPROG; /* //GO.DIREC DD DSN=OZA058.VSAM.KSDS.PATH.PLI,DISP=OLD // EXAMPLE OF ACCESSING THE KSDS FILE DYNAMICALLY THROUGH THE ALTERNATE INDEX //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * %PROCESS ATTRIBUTES(FULL); MYPROG: PROCEDURE OPTIONS(MAIN); DCL DIREC FILE RECORD DIRECT INPUT KEYED ENV(VSAM); DCL ONCODE BUILTIN; DCL CARD CHAR(80); DCL NAME CHAR(20) DEF CARD POS(1); DCL NUMBER CHAR(3) DEF CARD POS(21); DCL INREC CHAR(23); DCL NOTEOF BIT(1) INIT('1'B); DCL GOODKEYFLAG BIT(1) INIT('1'B); ON ENDFILE(SYSIN) NOTEOF='0'B; ON KEY(DIREC) BEGIN; PUT SKIP EDIT('INVALID KEY IN INPUT RECORD=',CARD)(A); PUT SKIP EDIT('ONCODE IS=',ONCODE)(A,F(8)); GOODKEYFLAG='0'B; END; OPEN FILE(DIREC); GET FILE(SYSIN) EDIT(CARD)(A(80)); DO WHILE(NOTEOF); READ FILE(DIREC) INTO(INREC) KEY(NUMBER); IF GOODKEYFLAG THEN PUT SKIP EDIT('RECORD IS= ',INREC)(A);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-137 /154

GOODKEYFLAG='1'B; GET FILE(SYSIN) EDIT(CARD)(A(80)); END; CLOSE FILE(DIREC); END MYPROG; /* //GO.DIREC DD DSN=OZA058.VSAM.KSDS.PATH.PLI,DISP=OLD //GO.SYSIN DD * ACTION,G. 162 BAKER,R. 152 KAMATH,M.S 999 BRAMLEY,O.H. 248 /* //

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-138 /154

SECTION 11-D

A data set with regional organization is divided into regions, each of which is identified by a region number. Each region can contain one record or more than one record, depending on the type of regional organization. The regions are numbered in succession, beginning with zero, and a record can be accessed by specifying its region number, and perhaps a key, in a data transmission statement. You can create a regional data set in a manner similar to a consecutive or indexed data set, presenting records in the order of ascending region numbers; If you use direct-access, you can present records in random sequence and insert them directly into pre-formatted regions. An existing regional data set can be accessed by using a file with the attributes SEQUENTIAL or DIRECT as well as INPUT or UPDATE. You do not need to specify either a region number or a key if the data set is associated with a SEQUENTIAL INPUT or SEQUENTIAL UPDATE file. When the file has the DIRECT attribute, you can retrieve, add, delete, and replace records at random. Records within a regional data set are either actual records containing valid data or dummy records (identified by (8)'1'B in 1st byte). The major advantage of regional organization over other types of data set organization is that it allows you to control the relative placement of records; by judicious programming, you can optimize record access in terms of device capabilities and the requirements of particular applications. Direct access of regional data sets in quicker than that of indexed data-sets, but regional data sets have the disadvantage that sequential processing can present records in random sequence; the order of sequential retrieval is not necessarily that in which the records were presented, nor need it be related to the relative key values. Use a file declaration with the following attributes to define a sequential regional data set: DCL filename FILE RECORD INPUT | OUTPUT | UPDATE SEQUENTIAL [KEYED] ENVIRONMENT (options); The ENVIRONMENT options applicable to regional data sets are: REGIONAL({1|2|3}) F|V|VS|U RECSIZE(record-length) BLKSIZE(block-size) KEYLENGTH(n)

DEFINING AND USING REGIONAL DATA SETS

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-139 /154

REGIONAL(1) Data set contains F-format records that do not have recorded keys. Each region in the data set contains only one record; REGIONAL(2) Specifies that the data set contains F-format records that have recorded keys. Each region in the data set contains only one record. REGIONAL(2) differs from REGIONAL(1) in that REGIONAL(2) records contain recorded keys and that records are not necessarily in the specified region; the specified region identifies a starting point. For files you create sequentially, the record is written in the specified region. For files with the DIRECT attribute, a record is written in the first vacant space on or after the track that contains the region number you specify in the WRITE statement. For retrieval, the region number specified in the source key is employed to locate the specified region. REGIONAL(3) specifies that the data set contains F-format, V-format, VS-format, or U-format records with recorded keys. Each region in the data set corresponds with a track on a directaccess device and can contain one or more records. REGIONAL(3) organization is similar to REGIONAL(2) in that records contain recorded keys, but differs in that a region for REGIONAL(3) data set employs the region number specified in a source key to locate the required region. Once the region has been located, a sequential search is made for space to add a record, or for a record that has a recorded key identical with that supplied in the source key. REGIONAL(1) organization is most suited to applications where there are no duplicate region numbers, and where most of the regions will be filled (reducing wasted space in the data set). REGIONAL(2) and REGIONAL(3) are more appropriate where records are identified by numbers that are thinly distributed over a wide range. In a REGIONAL(1) data set, since there are no recorded keys, the region number serves as the sole identification of a particular record. Source key should represent an unsigned decimal integer that should not exceed 16777215 RECORDS in a REGIONAL(1) data set are either actual records containing valid data or dummy records. A dummy record in a REGIONAL(1) data set is identified by the constant (8)1B in its first byte. Dummy records are not ignored when the data set is read; your PL/I program must be prepared to recognize them. Create a REGIONAL(1) data set either sequentially or by direct-access. SEQUENTIAL OUTPUT opening of the file causes all tracks on the data set to be cleared, and a capacity record to be written at the beginning of each track to record the amount of space available on that track. present records in ascending order of region numbers; If you use a DIRECT OUTPUT file to create the data set, the whole primary extent allocated to the data set is filled with dummy records when the file is opened. Example of creation of a REGIONAL(1) DataSet //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-140 /154

//PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL NOS FILE RECORD OUTPUT DIRECT KEYED ENV(REGIONAL(1)); DCL SYSIN FILE INPUT RECORD; DCL SYSIN_REC BIT(1) INIT('1'B); DCL 1 CARD, 2 NAME CHAR(20), 2 NUMBER CHAR(2), 2 CARD_1 CHAR(58); DCL IOFIELD CHAR(20); ON ENDFILE(SYSIN) SYSIN_REC='0'B; OPEN FILE(NOS); READ FILE(SYSIN) INTO(CARD); DO WHILE(SYSIN_REC); IOFIELD=NAME; WRITE FILE(NOS) FROM(IOFIELD) KEYFROM(NUMBER); PUT FILE(SYSPRINT) SKIP EDIT(CARD)(A); READ FILE(SYSIN) INTO(CARD); END; PUT SKIP LIST('END OF INPUT----'); CLOSE FILE(NOS); END MYPROG; /* //GO.NOS DD DSN=OZA058.REGIONAL.ONE,UNIT=SYSDA,SPACE=(20,100), // DCB=(RECFM=F,BLKSIZE=20,DSORG=DA),DISP=(OLD,KEEP) //GO.SYSIN DD * ACTION,G. 12 BAKER,R. 13 BRAMLEY,O.H. 28 /* // Notes: 1) Note that the DD statement gives the space parameter in terms of bytes(here the record or block size) and multiples of this (100) which is the number of regions. 2) Note that when this file is opened for output the operating system initialises each record with the first byte set to (8)'1'B.. This indicates a dummy record. On sequential input, these records have to be identified in user code and skipped . ACCESSING AND UPDATING A REGIONAL(1) DATA SET You can open the file and accesses it for SEQUENTIAL INPUT or UPDATE, or for DIRECT INPUT or UPDATE. You can open it for OUTPUT only if the existing data set is to be overwritten. Sequential Access To open a SEQUENTIAL file that is used to process a REGIONAL(1) data set, use either the INPUT or UPDATE attribute. Sequential access is in the order of ascending region numbers. All records are retrieved, whether dummy or actual, and you must ensure that your PL/I program recognizes dummy records.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-141 /154

Direct Access To open a DIRECT file that is used to process a REGIONAL(1) data set you can use either the INPUT or the UPDATE attribute. All data transmission statements must include source keys; the DIRECT attribute implies the KEYED attribute. Example of reading a REGIONAL(1) DataSet //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL NOS FILE RECORD INPUT DIRECT KEYED ENV(REGIONAL(1)); DCL SYSIN FILE INPUT RECORD; DCL SYSIN_REC BIT(1) INIT('1'B); DCL 1 CARD, 2 NAME CHAR(20), 2 NUMBER CHAR(2), 2 CARD_1 CHAR(58); DCL IOFIELD CHAR(20); ON ENDFILE(SYSIN) SYSIN_REC='0'B; OPEN FILE(NOS); READ FILE(SYSIN) INTO(CARD); DO WHILE(SYSIN_REC); READ FILE(NOS) INTO(IOFIELD) KEY(NUMBER); PUT FILE(SYSPRINT) SKIP EDIT('FILE =',IOFIELD)(A); PUT FILE(SYSPRINT) SKIP EDIT('SYSIN=',NAME)(A); READ FILE(SYSIN) INTO(CARD); END; PUT SKIP LIST('END OF INPUT----'); CLOSE FILE(NOS); END MYPROG; /* //GO.NOS DD DSN=OZA058.REGIONAL.ONE,UNIT=SYSDA,SPACE=(20,100), // DCB=(RECFM=F,BLKSIZE=20,DSORG=DA),DISP=(OLD,KEEP) //GO.SYSIN DD * ACTION,G. 12 BAKER,R. 13 BRAMLEY,O.H. 28 /* // Using REGIONAL(2) Data Sets In a REGIONAL(2) data set, each record is identified by a recorded key that immediately precedes the record. The actual position of the record in the data set relative to other records is determined not by its recorded key, but by the region number that you supply in the source key of the WRITE statement that adds the record to the data set. When you add a record to the data set by direct-access, it is written with its recorded key in the first available space after the beginning of the track that contains the region specified. When a record is read by direct-access, the search for a record with the appropriate recorded key begins at the start of the track that contains the region specified and continues right through to the end of the data set and then from the beginning until the entire data set

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-142 /154

has been covered. The closer a record is to the specified region, the more quickly it can be accessed. (This can be limited by LIMCT parameter of DD statement). Using Keys for REGIONAL(2) and (3) Data Sets The character value of the source key can be thought of as having two logical parts:-The region number and a comparison key. On output, the comparison key is written as the recorded key; for input, it is compared with the recorded key. Rightmost 8 characters of the source key make up the region number, that does not exceed 16777215.You can only specify the characters 0 through 9 and the blank character; leading blanks are interpreted as zeros. comparison key is a character string that occupies the left hand side of the source key, and can overlap or be distinct from the region number, from which it can be separated by other non significant characters. Specify the length of the comparison key either with the KEYLEN sub-parameter of the DD statement for the data set or the KEYLENGTH option of the ENVIRONMENT attribute. Examples KEY (JOHNbDOEbbbbbb12363251) The rightmost 8 characters make up the region specification, the relative number of the record. Assume that the associated DD statement has the sub-parameter KEYLEN=14. In retrieving a record, the search begins with the beginning of the track that contains the region number 12363251, until the record is found having the recorded key of JOHNbDOEbbbbbb. If the subparameter is KEYLEN=22, the search still begins at the same place, but since the comparison and the source key are the same length, the search would be for a record having the recorded key JOHNbDOEbbbbbb12363251. KEY(JOHNbDOEbbbbbbDIVISIONb423bbbb34627) In this example, the rightmost 8 characters contain leading blanks, which are interpreted as zeros. The search begins at region number 00034627. If KEYLEN=14 is specified, the character DIVISIONb423b will be ignored. Assume that COUNTER is declared FIXED BINARY(21) and NAME is declared CHARACTER(15). You could specify the key like so: KEY (NAME || COUNTER)

The value of COUNTER will be converted to a character string of 11 characters. (The rules for conversion specify that a binary value of this length, when converted to character, will result in a string of length 11 (three blanks followed by eight decimal digits.) The value of the rightmost eight characters of the converted string is taken to be the region specification. Then if the key-length specification is KEYLEN=15, the value of NAME is taken to be the comparison specification. Dummy Records. A dummy record consists of a dummy key and dummy data. A dummy key is identified by the constant (8)1B in its first byte. The program inserts dummy records either when the data set is created or when a record is deleted. The dummy records are ignored

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-143 /154

when the program reads the data set. However, you can replace dummy records with valid data. Creating a REGIONAL(2) Data Set You can create a regional(2) data set either sequentially or by direct-access. The data set on opening is initialized with capacity records specifying the amount of space available on each track. When you use SEQUENTIAL OUTPUT, present records in ascending order of region numbers; If you use a DIRECT OUTPUT file the whole primary extent allocated to the data set is filled with dummy records when the file is opened. You can present records in random order, and it is possible to place records with identical recorded keys in the data set. Example of creating a REGIONAL(2) DataSet //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL (LOANS,STOCK) FILE RECORD KEYED ENV(REGIONAL(2)); DCL 1 BOOK, 2 AUTHOR CHAR(25), 2 TITLE CHAR(50), 2 QTY FIXED DEC(3); DCL NUMBER CHAR(4); DCL INTER FIXED DEC(5); DCL REGION CHAR(8); DCL NOTEOF BIT(1) INIT('1'B); OPEN FILE(LOANS) DIRECT OUTPUT; CLOSE FILE(LOANS); ON ENDFILE(SYSIN) NOTEOF='0'B; OPEN FILE(STOCK) DIRECT OUTPUT; GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK); DO WHILE(NOTEOF); INTER=(NUMBER-1000)/9; /* regions 0 to 999 */ REGION=INTER; WRITE FILE(STOCK) FROM(BOOK) KEYFROM(NUMBER||REGION); PUT FILE(SYSPRINT) SKIP EDIT(BOOK)(A); GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK); END; PUT SKIP LIST('END OF INPUT----'); CLOSE FILE(STOCK); END MYPROG; /* //GO.LOANS DD DSN=OZA058.LOANS.REGIONAL.TWO, // UNIT=SYSDA,SPACE=(12,1000), // DCB=(RECFM=F,BLKSIZE=12,KEYLEN=7),DISP=(NEW,KEEP) //GO.STOCK DD DSN=OZA058.STOCK.REGIONAL.TWO, // UNIT=SYSDA,SPACE=(77,1000), // DCB=(RECFM=F,BLKSIZE=77,KEYLEN=4),DISP=(NEW,KEEP) //GO.SYSIN DD * '1015' 'W.SHAKESPEARE' 'MUCH ADO ABOUT NOTHIBG' 1 '1214' 'L.CARROL' 'THE HUNTING OF THE SNARK' 1 '3079' 'G.FLAUBERT' 'MADAME BOVARY' 1

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL /* //

Page:-144 /154

Notes: 1)Note that the DD statement space parameter indicates the space in Bytes (block/record size) and number of such blocks, that is, the number of regions which is 1000. 2)Since the book number varies from 1000 to 9999, divide this by 9 to get the region number which can vary from 0 to 999. 3)When a REGIONAL(2) file is opened for output, the O/S initialises each record with the key for that record set to (8)'1'B, indicating a deleted record. 4)Note that when you read this data set sequentially, you have to identify the dummy records and skip them. ACCESSING AND UPDATING A REGIONAL(2) DATA SET You can open the file that accesses it for SEQUENTIAL INPUT or UPDATE, or for DIRECT INPUT or UPDATE. Sequential Access To open a SEQUENTIAL file that is used to process a REGIONAL(2) data set, use either the INPUT or UPDATE attribute. Direct Access To open a DIRECT file that is used to process a REGIONAL(2) data set, use either the INPUT or the UPDATE attribute. You must include source keys in all data transmission statements; the DIRECT attribute implies the KEYED attribute. The search for each record is commenced at the start of the track containing the region number indicated by the key. Example of reading a REGIONAL(2) DataSet //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL STOCK FILE RECORD KEYED ENV(REGIONAL(2)); DCL 1 BOOK, 2 AUTHOR CHAR(25), 2 TITLE CHAR(50), 2 QTY FIXED DEC(3); DCL 1 BOOK1, 2 AUTHOR CHAR(25), 2 TITLE CHAR(50), 2 QTY FIXED DEC(3); DCL NUMBER CHAR(4); DCL INTER FIXED DEC(5); DCL REGION CHAR(8); DCL NOTEOF BIT(1) INIT('1'B); ON ENDFILE(SYSIN) NOTEOF='0'B; OPEN FILE(STOCK) DIRECT INPUT; GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK);

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-145 /154

DO WHILE(NOTEOF); INTER=(NUMBER-1000)/9; /* regions 0 to 999 */ REGION=INTER; READ FILE(STOCK) INTO(BOOK1) KEY(NUMBER||REGION); PUT FILE(SYSPRINT) SKIP EDIT('OUTP=',BOOK1)(A); PUT FILE(SYSPRINT) SKIP EDIT('INP =',BOOK)(A); GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK); END; PUT SKIP LIST('END OF INPUT----'); CLOSE FILE(STOCK); END MYPROG; /* //GO.STOCK DD DSN=OZA058.STOCK.REGIONAL.TWO, // UNIT=SYSDA,SPACE=(77,300), // DCB=(RECFM=F,BLKSIZE=77,KEYLEN=4),DISP=(OLD,KEEP) //GO.SYSIN DD * '1015' 'W.SHAKESPEARE' 'MUCH ADO ABOUT NOTHIBG' 1 '1214' 'L.CARROL' 'THE HUNTING OF THE SNARK' 1 '3079' 'G.FLAUBERT' 'MADAME BOVARY' 1 /* // USING REGIONAL(3) DATA SETS A REGIONAL(3) data set differs from a REGIONAL(2) data set (described above) only in the following respects: Each region number identifies a track on the direct-access device that contains the data set; the region number should not exceed 32767.A region in excess of 32767 is treated as modulo 32768; for example, 32778 is treated as 10. A region can contain one or more records, or a segment of a VS-format record. The data set can contain F-format, V-format, VS-format, or U-format records. you can create dummy records, but a data set that has V-format, VS-format, or U-format records is not pre-formatted with dummy records because the lengths of records cannot be known until they are written; however, all tracks in the primary extent are cleared and the operating system maintains a capacity record at the beginning of each track, in which it records the amount of space available on that track. Source keys for a REGIONAL(3) data set are interpreted exactly as those for a REGIONAL(2) data set are, and the search for a record or space to add a record is conducted in a similar manner. You can create a REGIONAL(3) data set either sequentially or by direct-access. When you use a SEQUENTIAL OUTPUT file to create the data set, you must present records in ascending order of region numbers, but you can specify the same region number for successive records. For F-format records, any record you omit from the sequence is filled with a dummy record.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-146 /154

If you use a DIRECT OUTPUT file to create the data set, the whole primary extent allocated to the data set is initialized when the data set is opened. For F-format records, the space is filled with dummy records, and for V-format, VS-format, and Uformat records, the capacity record for each track is written to indicate empty tracks. You can present records in random order, and no condition is raised by duplicate keys or duplicate region specifications. Example of creation of a REGIONAL(3) DataSet //OZA058VB JOB MSGCLASS=A,NOTIFY=OZA058 //MYSTEP EXEC PROC=IEL1CLG,REGION.PLI=1M,PARM.PLI='NOP', // PARM.LKED='LIST' //PLI.SYSIN DD * MYPROG: PROCEDURE OPTIONS(MAIN); DCL (LOANS,STOCK) FILE RECORD KEYED ENV(REGIONAL(3)); DCL 1 BOOK, 2 AUTHOR CHAR(25), 2 TITLE CHAR(50), 2 QTY FIXED DEC(3); DCL NUMBER CHAR(4); DCL INTER FIXED DEC(5); DCL REGION CHAR(8); DCL NOTEOF BIT(1) INIT('1'B); OPEN FILE(LOANS) DIRECT OUTPUT; CLOSE FILE(LOANS); ON ENDFILE(SYSIN) NOTEOF='0'B; OPEN FILE(STOCK) SEQUENTIAL OUTPUT; GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK); DO WHILE(NOTEOF); INTER=(NUMBER-1000)/2250; /* regions 0,1,2,3,4 for a device*/ /* holding 200 or more blocks per trk*/ REGION=INTER; WRITE FILE(STOCK) FROM(BOOK) KEYFROM(NUMBER||REGION); PUT FILE(SYSPRINT) SKIP EDIT(BOOK)(A); GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK); END; PUT SKIP LIST('END OF INPUT----'); CLOSE FILE(STOCK); END MYPROG; /* //GO.LOANS DD DSN=OZA058.LOANS.REGIONAL.THREE, // UNIT=SYSDA,SPACE=(TRK,3), // DCB=(RECFM=F,BLKSIZE=12,KEYLEN=7),DISP=(NEW,KEEP) //GO.STOCK DD DSN=OZA058.STOCK.REGIONAL.THREE, // UNIT=SYSDA,SPACE=(TRK,5), // DCB=(RECFM=F,BLKSIZE=77,KEYLEN=4),DISP=(NEW,KEEP) //GO.SYSIN DD * '1015' 'W.SHAKESPEARE' 'MUCH ADO ABOUT NOTHIBG' 1 '1214' 'L.CARROL' 'THE HUNTING OF THE SNARK' 1 '3079' 'G.FLAUBERT' 'MADAME BOVARY' 1 /*

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL //

Page:-147 /154

Notes:1) Note that the DD statement, space parameter specifies the number of tracks as 5. The region numbers are therefore 0,1,2,3,4. This is because there as many regions as number of tracks in a REGIONAL(3) data set. 2) Since there can be 8999 books and these have to be in four regions, we can divide the book number by 1800 to get the region number. In this example 2250 is used. This will result in using regions 0,1,2 and 3 only. 3) 1000 is subtracted from the book number as book numbers start from 1000 only. This is to illustrate that the range of keys need not start from 0. 4) Note that once a record is created and subsequently deleted, the space occupied by that record is lost. The record is flagged as deleted by changing the first byte of the recorded key to (8)'1'B. Example of reading a REGIONAL(3) DataSet MYPROG: PROCEDURE OPTIONS(MAIN); DCL STOCK FILE RECORD KEYED ENV(REGIONAL(3)); DCL 1 BOOK, 2 AUTHOR CHAR(25), 2 TITLE CHAR(50), 2 QTY FIXED DEC(3); DCL 1 BOOK1, 2 AUTHOR CHAR(25), 2 TITLE CHAR(50), 2 QTY FIXED DEC(3); DCL NUMBER CHAR(4); DCL INTER FIXED DEC(5); DCL REGION CHAR(8); DCL NOTEOF BIT(1) INIT('1'B); ON ENDFILE(SYSIN) NOTEOF='0'B; OPEN FILE(STOCK) DIRECT INPUT; GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK); DO WHILE(NOTEOF); INTER=(NUMBER-1000)/2250; /* regions 0,1,2,3,4 for a device*/ /* holding 200 or more blocks per trk*/ REGION=INTER; READ FILE(STOCK) INTO(BOOK1) KEY(NUMBERREGION); PUT FILE(SYSPRINT) SKIP EDIT('INPUT IS=',BOOK)(A); PUT FILE(SYSPRINT) SKIP EDIT('FILE IS=',BOOK1)(A); GET FILE(SYSIN) SKIP LIST(NUMBER,BOOK); END; PUT SKIP LIST('END OF INPUT--'); CLOSE FILE(STOCK); END MYPROG; /* //GO.SYSIN DD *

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-148 /154

'1015' 'W.SHAKESPEARE' 'MUCH ADO ABOUT NOTHIBG' 1 '1214' 'L.CARROL' 'THE HUNTING OF THE SNARK' 1 '3079' 'G.FLAUBERT' 'MADAME BOVARY' 1 /* Essential Information for Creating and Accessing Regional Data Sets Certain information must be provided to the O/S either in the DD statement that defines the data set or the PL/I program Device that will write your data set (UNIT or VOLUME parameter of DD statement). Block size: You can specify the block size either in your PL/I program (in the BLKSIZE option of the ENVIRONMENT attribute) or in the DD statement (BLKSIZE subparameter). If you do not specify a record length, unblocked records are the default and the record length is determined from the block size. If you want to keep a data set DD statement must name the data set and indicate how it is to be disposed of (DSNAME and DISP parameters). In the DCB parameter, you must always specify the data set organization as direct by coding DSORG=DA. For REGIONAL(2) and REGIONAL(3), you must also specify the length of the recorded key (KEYLEN) unless it is specified in the ENVIRONMENT attribute; Essential parameters of the DD statement are UNIT=, SPACE=, DCB=,DISP=, DSNAME=. DCB subparameters for a regional data set When required To specify Always required Record format (can also be specified in ENV attr) Sub-parameters RECFM=F or RECFM=V REGIONAL(3) only, or RECFM=U REGIONAL(3) only

Block size BLKSIZE= (can also be specified in ENV attribute) Data set organization key length (REGIONAL(2) and (3) only. Can also be specified in ENV attribute ) DSORG=DA KEYLEN=

Accessing a regional data set: essential parameters of the DD statement When required What you must state Parameters Always Name of data set DSNAME= Disposition of data set DISP=

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL If data set not catalogued Input device

Page:-149 /154 UNIT=

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-150 /154

SECTION-12 SAMPLE PLI-CICS-DB2 PROGRAM SKELETON


K19AP01: PROC(CPTR) OPTIONS (MAIN); %INCLUDE K19AM01; %INCLUDE DFHAID; %INCLUDE DFHBMSCA;

CICS/DB2 AND PL/I

/*EXEC SQL BEGIN DECLARE SECTION;*/ EXEC SQL INCLUDE SEAT ; EXEC SQL INCLUDE SINFO ; EXEC SQL INCLUDE SQLCA; EXEC SQL DECLARE PASSCUR CURSOR FOR SELECT PASSENGER_NAME, AGE, SEX FROM PASSENGER_INFO WHERE PNR_NO = :PI_PNR_NO; /*EXEC SQL END DECLARE SECTION;*/ /* PLI VARIABLES DCL TXT CHAR(40); DCL CHK FIXED DEC(10) INIT(0123456789); . . DCL CPTR POINTER; DCL 1 COMMAREA BASED (CPTR), 2 FLAG CHAR(2), 2 COMRM_TRAIN_NO FIXED BIN(15), 2 COMRM_STATION_CODE CHAR(4), 2 COMRM_DEP_DATE CHAR(10), 2 COMPNR_NO FIXED BIN(15), 2 COMRCPNO FIXED DEC(3), 2 COM_CLASS_ID CHAR(1), 2 COM_AMT FIXED DEC(9,2); MAIN_RTN: IF EIBCALEN = 0 THEN DO; ALLOCATE COMMAREA SET(CPTR); EXEC CICS SEND CONTROL ERASE; END; ELSE DO; SELECT(FLAG); WHEN('T1') CALL RESCAN_RECV_RTN; WHEN('T2') CALL RES1_RECV_RTN; WHEN('T3') CALL RES_RECV_RTN; WHEN('T4') CALL CANCHK1_RTN; */

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL WHEN('T5') CALL CAN_RECV_RTN; WHEN('T6') CALL CAN_RECV1_RTN; OTHERWISE CALL ERROR_MSG_RTN; END; END; EXEC CICS SEND MAP('RESCAN') MAPSET('K19AM01') FROM(RESCANO) FREEKB ; EXEC CICS RETURN TRANSID('K19A') COMMAREA(COMMAREA); EXEC CICS RECEIVE MAP('RESCAN') MAPSET('K19AM01') INTO (RESCANI); SELECT(EIBAID); WHEN(DFHENTER) CALL RESCHK1_RTN; WHEN(DFHPF3) CALL ERROR_MSG_RTN; OTHERWISE CALL ERROR_MSG_RTN; END; EXEC SQL SELECT TRAIN_NAME INTO :TR_TRAIN_NAME FROM TRN_INFO WHERE TRAIN_NO = :TR_TRAIN_NO; IF SQLCODE != 0 THEN CALL ERRR_RTN; EXEC CICS SEND TEXT FROM(TXT); EXEC CICS SEND CONTROL ERASE;

Page:-151 /154

EXEC SQL INSERT INTO CBK019.TICKET_INFO (PNR_NO, TRAIN_NO, DEP_DATE) VALUES(:TI_PNR_NO, :TI_TRAIN_NO, :TI_DEP_DATE); IF SQLCODE !=0 THEN CALL ERROR_MSG_RTN; EXEC SQL OPEN PASSCUR; EXEC SQL FETCH PASSCUR INTO :PI_PASS_NAME, :PI_AGE, :PI_SEX; EXEC SQL DELETE FROM CBK019.PASSENGER_INFO WHERE PNR_NO = :PI_PNR_NO; EXEC CICS RETURN; END K19AP01;

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-152 /154

SECTION-13 EXERCISES FOR PL/I General note:The exercises below, except the final project, are suggestions. Please feel free to experiment and try out your own ideas and programs.
1)SETTING UP AND GETTING ACQUAINTED WITH BASICS Examine and understand the IBM supplied procedures IEL1C,IEL1CL,IEL1CLG Create a PDS with DSN name USERID.PLICLASS.CNTL with DSORG=PO,LRECL=80,BLKSIZE=3200,UNIT=SYSDA,RECFM=FB,SPACE=(T RK,(3,3,10)) Create a PDS with DSN name USERID.PLICLASS.OBJ DSORG=PO,LRECL=80,BLKSIZE=3200,UNIT=SYSDA,RECFM=FB, SPACE=(TRK,(1,1,1)) Create a PDS with DSN name USERID.PLICLASS.LOADLIB DSORG=PO,UNIT=SYSDA,RECFM=U,SPACE=(TRK,(1,1,1)) with

with

Write a PL/I program to add two Fixed Decimal fields and output the operands and the results to SYSPRINT. Use IEL1CLG first. Subsequently try to create a LOADMODULE of your program in the above created LOADLIB and execute it using a separate run JCL. 2) Write a program in two parts to test separate compilation and linking. The first part is a PL/I external procedure called MINFD which accepts two Fixed Decimal numbers. The minimum of the first two numbers is assigned to an external Fixed decimal field called RESULT. This procedure is called from the main procedure with two values and RESULT is output on SYSPRINT. Now try coding MIN as a FUNCTION which returns the minimum value as a FIXED DECIMAL. 3) Using the editor create a book master (DSN=USERID.BOOK.DATA)with following fields COL 1 to 19 = Name of the author (char field) COL 20 = blank COL 21 to 23 = Book number (Numeric char field). Write a program to copy this to printer with first field starting at position 10 and the second field starting to position 40. Write a suitable heading every 10 lines and indicate page number in the heading. Read the input data via SYSIN using GET statements. You must be able to achieve the print positioning of COL 10 and 40 both via PUT LIST or PUT EDIT statements. 4) Write a program to generate a list of prime numbers from 1 to 100.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-153 /154

5) Write a program which converts military time (eg 1430 hrs is 2.30 PM) to civilian time and outputs it via SYSPRINT. You must be able to check out the program for various values input via SYSIN. 6) Write a loan amortisation program which accepts the following via SYSIN on each input record a)Loan amount, b)Interest on reducing balance calculated monthly c)Quantum of equal monthly instalments The program must calculate the number of instalments required with the above input for full loan payment, for each SYSIN card, and output on SYSPRINT.Generate suitable headings. 7) Write a program which reads an input file via SYSIN. The input file consists of names like KATHY THOMAS, GEORGE SMITH etc, There is one name per card, starting COL 1. Output the names on SYSPRINT after reversing first and last name. 8) read the same name file above and output it in sorted order on SYSPRINT. Use an ARRAY to read in the names for sorting purposes. FINAL PROJECT Objective:- To develop a mini Library maintenance system (batch operation, input via SYSIN data) to track and maintain:(a) Inventory of Books (b) Record of all members (c) Status of all books (d) Generate reports on I. Books available II. Members III. Status of books The design is left to you. The data, functionality and report formats will be determined by the class and followed by all. A suggested approach is given below. You are free to design and follow your own. Possible functionality of library maintenance system Create an KSDS for records with following format COL 1 to 20 Author name (char) COL 21 to 23 Book number (char). Index on this field. COL 24 flag (char). It is assumed that there is only one book per number, hence it is not necessary to have a count field in the data record. The flag field will be set to 'I' when the book is issued, else it will be 'R'. Write a program to add new books, delete old books to this file. The input data for adding new books or deleting old books, input via SYSIN is of the following format COL 1 to 20 Author name (char) COL 21 to 23 Book number (char). COL 24 operation field (char).

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

INDUS PL/I CLASS MATERIAL

Page:-154 /154

The field in COL 24 is 'A' for add, 'D' for delete, 'L' to list the file completely. The program should be able to handle input in error like duplicate keys, no record found and wrong operation field. In addition when deleting, check to make sure that the author names match between input and the KSDS file. b)Create a REGIONAL(2) file which is the member master. Record format is as below COL 1 to 20 member name (char) COL 21 to 23 member number (char) (key field) Write a program to perform additions and deletions with input data input via SYSIN in the following record format COL 1 to 20 member name (char) COL 21 to 23 member number (char) COL 24 operation field (char) The operation field in COL 24 will have 'A' for addition and 'D' for deletion, 'L' to list the data set. The program should be able to handle input in error like duplicate keys, no record found and wrong operation code. When deleting a record additionally check to ensure that the member name read via SYSIN and that in the REGIONAL data set match. c)Write a program to record issues and returns. This will be a KSDS file. The input record is COL 1 to 3 member number (char) COL 4 to 6 book number (char). COL 7 code (char) This is 'I' for issue and 'R' for return. If the book is available create a record in the issues/returns file in the following format COL 1 to 3 member name (char) COL 4 to 6 book number (char). This will be the key field COL 7 to 12 issue date in form YYMMDD Update the flag field in the book master file. Generate a status report of this operation indication requests which could not be processed with the reason as well as issues/returns processed Write a program to generate the following additional reports. To request the first report pass PARM '1' in the PARM field of the JCL. To get report (2) pass the PARM '2'. 1)List all borrowers whose issue date falls in last month. 2)List all borrowers with full details like book number, author, member name, member number and date of issue.

INDUS MAINFRAMES
87/2RT, KHALEEL MANZIL, S.R.NAGAR,HYD-38 Ph:040-64646341, Mobile:09948034596 www.mainframesguru.com

You might also like