Program segments (COPY elements) can be stored in libraries as distinct entities independent of the compilation unit in which they are used. This is especially recommended when identical program segments are used in different compilation units.
In the compilation unit itself, such program segments are represented by a COPY statement. COPY statements may be located at any position in the compilation unit (except for comment lines and non-numeric literals).
When the compiler encounters a COPY statement in the compilation unit being compiled, it inserts the element specified in the COPY statement from the appropriate library. The COPY element is then compiled as if it were a part of the compilation unit itself.The COPY statement format is shown and explained in chapter “Controlling the compiler” in the “COBOL2000 Reference Manual” [1].
Input of COPY elements from PLAM libraries
Before invoking the compiler, the libraries that contain the COPY elements must be assigned to the compiler using the ADD-FILE-LINK command and linked to the file link names specified below.
If a library name is specified in the COPY statement, the link name is formed from the first 8 characters of the library name.
If no library name has been declared in the COPY statement, up to ten libraries can be linked using the standard link names COBLIB, and COBLIB1 through COBLIB9. The compiler then searches the assigned libraries in hierarchical order until the required COPY element is found.
Depending on how the COPY statement is formulated in the compilation unit, the following assignments are required in the SET-FILE-LINK command:
COPY statement | ADD-FILE-LINK command | ||
|
| ||
textname | element name (max. 31 characters long) | standard-linkname | COBLIB |
libname | Name of the cataloged library in | ||
|
| ||
library | library name (max. 31 characters long) | linkname | The first eight characters of the name of the |
libname | Name of the cataloged library in |
Input of COPY elements from the POSIX file system
If the POSIX subsystem is available, you can also pass COPY texts from the POSIX file system to the compiler. To do this you use an S variable with the default name of SYSIOL-COBLIB or SYSIOL-libraryname. The formulation of the COPY statement in the compilation unit influences the S variable as follows (see also "Example 2-6"); this does not apply to BC (basic configuration):
COPY statement | S variable | ||
|
| ||
textname | name of POSIX file (max. 31 characters long) containing the COPY text. | pathname | Absolute pathname (beginning with /) of the directory in which a search is made for the file textname |
|
| ||
library | Library name (max. 31 characters long) for forming the S variable with the name SYSIOL-library. | libname | The first 8 characters of library |
pathname | Absolute pathname (beginning with /) of the directory in which a search is made for the file textname |
Example 2-4
Input of two COPY elements
IDENTIFICATION DIVISION. PROGRAM-ID. PROG. ... COPY XYZ.——————————————————————————————————————————————————(1) COPY ABC OF LIBRARY.———————————————————————————————————————(2) ... Assignment and linkage: /ASSIGN-SYSDTA EXAMPLE1 ———————————————————————————————————————(3) /ADD-FILE-LINK COBLIB,LIB1 ————————————————————————————————————(4) /ADD-FILE-LINK LIBRARY,LIB2 ———————————————————————————————————(5) Call to compiler |
The compilation unit in the file EXAMPLE1 includes the following COPY statements:
(1) | XYZ is the name of the element under which the COPY element is stored in the PLAM library LIB1. |
(2) | ABC is the name of the element under which the COPY element is stored in the PLAM library LIB2 with the link name LIBRARY. |
(3) | SYSDTA is assigned to the file EXAMPLE1. From this file, the compiler receives a compilation unit in which two COPY statements are written. |
(4) | The first ADD-FILE-LINK command assigns the PLAM library LIB1 and links it to the standard link name COBLIB. |
(5) | The second ADD-FILE-LINK command assigns the PLAM library LIB2 and links it to the library name LIBRARY specified in the COPY statement. |
Example 2-5
Input of several COPY elements from different libraries
IDENTIFICATION DIVISION. PROGRAM-ID. PROG1. ... COPY A1. (1) COPY B1. | COPY D1. (1) ...
/ASSIGN-SYSDTA EXAMPLE2 ———————————————————————————————————————— (2) /ADD-FILE-LINK COBLIB,A (3) /ADD-FILE-LINK COBLIB1,B | /ADD-FILE-LINK COBLIB3,D (3) Call to compiler ——————————————————————————————————————————————— (4) |
The compilation unit EXAMPLE2 includes the following COPY statements:
(1) | A1, B1 and D1 are the names under which the COPY elements have been stored in the cataloged libraries A, B and D. |
(2) | SYSDTA is assigned to the cataloged file EXAMPLE.2. From this file, the compiler receives a compilation unit in which three COPY statements are written. |
(3) | Libraries A, B and D are assigned and linked to standard link names. Whereas the standard link name COBLIB must always be assigned, the number and sequence of links to COBLIB1 through COBLIB9 are freely selectable. |
(4) | After invocation, the compiler searches COBLIB, COBLIB1, and COBLIB3 in the given order for the elements specified in the COPY statements. |
Example 2-6
Input of a COPY element from the POSIX file system
IDENTIFICATION DIVISION. PROGRAM-ID. PROG1. ... COPY ATEXT. ————————————————————————————————————————————————————(1) ... Assigning the POSIX file system by declaring and setting an S variable: /DECL-VAR SYSIOL-COBLIB,INIT='*POSIX(/usr/dir1),*POSIX(/usr/dir2)', — / SCOPE=*TASK —————————————————————(2) /START-COBOL2000-COMPILER? —————————————————————————————————————(3) |
(1) | The COPY element ATEXT is a file in the POSIX file system. |
(2) | The SDF-P command DECL-VARIABLE sets the variable to the paths of the POSIX directories dir1 and dir2 which are to be searched for ATEXT. |
(3) | Access to the POSIX file system is only possible when the compiler is invoked under SDF control. By means of the “?” appended to the call command, the user is placed in SDF menu mode (see section "SDF menu mode") in which further entries on controlling the compiler run can be made. |
(4) | The compiler accepts COPY elements from the POSIX file system only if their file names consist only of uppercase letters. |