Module 11:- Library Facility and sub-programming
Library Facility- COPY Verb
- Assume that, there is a set of source code which is common across multiple programs. In such cases instead of repeatedly writing same set of source code in all these programs, we can write that code in one place (i.e. in member of some PDS) and can include(copy) it into all these programs simply using COPY statement
- In simple words, COPY statement allow us to include pre-written source code into our source program(during compilation)
- The place where frequently used source code is stored is called as copybook. It is just a member of any PDS
- Basic syntax of including copybook in your program:-
COPY copy-book-name [{OF/IN} library-name] [REPLACING string-to-be-replaced BY replacing-string]
- Before, including any copybook in your program make sure it is available in copybook library. By default, the copybook library is SYSLIB and it can be changed using IN or OF phrase of COPY statement.
- copy-book-name is name of copybook that we want to include in our program
- Sometimes, there is few programs that may need some change to copybook before including it. In such case REPLACING phrase can be used to replace strings of our choice. Note that, it does not impact actual copybook stored in library. It just take effect only in programs where replacing is used
- Important note about REPLACING:-
- string-to-be-replaced and replacing-string should be enclosed by double equal (==) sign when you want to replace series of words or characters as opposed to individual identifier, literal or word.
- Example Scenario 1:-
COPY EMPLOYEE REPLACING ‘EMP-’ BY ‘EMPLOYEE-’In above statement, ‘EMP-‘ and ‘EMPLOYEE-‘ are individual string thus no need to enclose it in double equal sign (==)
- Example Scenario 2:-COPY EMPLOYEE REPLACING ==10 RECORDS== BY ==90 RECORDS==In above statement, ’10 RECORDS’ contains series of two words separated by space thus it needs to be enclosed by double equal sign. Sam goes for ’90 RECORDS’IMP: ‘==’ is not a part of actual string to be replaced or new replacing string
- Copybooks can be used in:- SOURCE-COMPUTER, OBJECT-COMPUTER, SPECIAL-NAMES, FILE-CONTROL, FD SECTION, PARAGRAPHS IN PROCEDURE DIVISION.
- Nested COPY not allowed i.e. the copybooks cannot have COPY statement
- Whenever any changes are made in any copybook, all the programs using it needs to be recompiled for changes to take effect
- Benefits:-
- Common source code is stored within a single copybook
- It is easier to change: Only copybook needs to be changed instead of changing all programs. (However, recompilation of all program is needed)
- It reduces coding and debugging time
- Common paragraphs like ERROR-PARA, DATE-VALIDATION can be used in multiple programs
- Common files are usually used in across multiple programs. Thus, file layout can be defined in copybook and that copybook can be included in all programs using that file
- DB2 table’s DCLGEN can also be placed in copybook as tables are used in multiple programs
- Example:-
Suppose below is source code in copybook EMPFD:-01 EMP-REC 05 EMP-ID PIC 9(05). 05 EMP-NAME PIC 9(10).In main COBOL program:-DATA DIVISION. WORKING-STORAGE SECTION. COPY EMP-REC.At compilation, it will become:-DATA DIVISION. WORKING-STORAGE SECTION. COPY EMP-REC. 01 EMP-REC 05 EMP-ID PIC 9(05). 05 EMP-NAME PIC 9(10).