TExcelTAD
Home › COBOL › COBOL Copybooks

COBOL Copybooks

In a system with hundreds of programs, the same record layouts are used everywhere: the customer record, the transaction record, the parameter block of a subprogram. Writing them out in every program would be a recipe for mismatches. COBOL solves this with copybooks: source fragments kept in a library and pulled into a program at compile time with the COPY statement.

A copybook and its use

A copybook is just COBOL text in its own file, usually with no divisions of its own. For example CUSTREC.cpy:

cobol
      *> CUSTREC.cpy - customer master record (100 bytes)
       01  CUSTOMER-RECORD.
           05  CUST-ID                PIC 9(8).
           05  CUST-NAME              PIC X(30).
           05  CUST-BALANCE           PIC S9(9)V99 COMP-3.
           05  CUST-STATUS            PIC X.
               88  CUST-ACTIVE                    VALUE "A".
               88  CUST-CLOSED                    VALUE "C".
           05  FILLER                 PIC X(55).

Any program that needs it writes one line where the text should appear:

cobol
       DATA DIVISION.
       FILE SECTION.
       FD  CUSTOMER-FILE.
           COPY CUSTREC.

       WORKING-STORAGE SECTION.
           COPY ERRCODES.

The compiler replaces COPY CUSTREC. with the contents of the copybook before compiling, exactly as if you had typed it there. GnuCOBOL looks for CUSTREC, CUSTREC.cpy, CUSTREC.cob and similar names in the current directory and in the directories given with -I or COB_COPY_DIR; on a mainframe the copybooks are members of a partitioned data set (a copy library) named in the compile JCL.

The playground compiles a single file, so it cannot COPY a separate copybook. The program below shows what the compiler sees after the copybook has been expanded:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COPY-EXPANDED.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
      *> ---- start of COPY CUSTREC (expanded by the compiler) ----
       01  CUSTOMER-RECORD.
           05  CUST-ID                PIC 9(8).
           05  CUST-NAME              PIC X(30).
           05  CUST-BALANCE           PIC S9(9)V99 COMP-3.
           05  CUST-STATUS            PIC X.
               88  CUST-ACTIVE                    VALUE "A".
               88  CUST-CLOSED                    VALUE "C".
           05  FILLER                 PIC X(55).
      *> ---- end of COPY CUSTREC ----
       PROCEDURE DIVISION.
           MOVE 10004711 TO CUST-ID
           MOVE "Sofia Rossi" TO CUST-NAME
           MOVE -42.10 TO CUST-BALANCE
           SET CUST-ACTIVE TO TRUE
           DISPLAY "Record length: " FUNCTION LENGTH(CUSTOMER-RECORD)
           DISPLAY CUST-ID " " FUNCTION TRIM(CUST-NAME) " "
                   CUST-BALANCE " " CUST-STATUS
           STOP RUN.
Record length: 100
10004711 Sofia Rossi -000000042.10 A

The packed balance takes 6 bytes (11 digits plus a sign nibble), so the record is 8 + 30 + 6 + 1 + 55 = 100 bytes, matching the comment in the copybook. Keeping a size comment in each copybook is a common and useful habit.

COPY … REPLACING

The same copybook can be reused with different names. REPLACING substitutes text while copying. A widespread convention is a placeholder prefix such as :PFX: that each program replaces with its own prefix:

cobol
      *> ADDRESS.cpy
           05  :PFX:-STREET           PIC X(30).
           05  :PFX:-CITY             PIC X(20).
           05  :PFX:-POSTCODE         PIC X(10).

      *> In the program: two copies with different prefixes
       01  BILLING-ADDRESS.
           COPY ADDRESS REPLACING ==:PFX:== BY ==BILL==.
       01  SHIPPING-ADDRESS.
           COPY ADDRESS REPLACING ==:PFX:== BY ==SHIP==.

The == delimiters mark pseudo-text: the exact characters to find and their replacement.

The REPLACE statement

The REPLACE statement performs the same compile-time substitution on the program's own source, from where it appears to the end of the program (or to REPLACE OFF). Since it needs no external file, it runs in the playground, and it shows exactly how REPLACING behaves:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. REPLACE-DEMO.
       REPLACE ==:PFX:== BY ==BILL== ==:LEN:== BY ==20==.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  ADDRESS-1.
           05  :PFX:-STREET           PIC X(:LEN:).
           05  :PFX:-CITY             PIC X(:LEN:).
       PROCEDURE DIVISION.
           MOVE "1 Harbour Way" TO BILL-STREET
           MOVE "Bristol" TO BILL-CITY
           DISPLAY "[" BILL-STREET "][" BILL-CITY "]"
           DISPLAY "Group length: " FUNCTION LENGTH(ADDRESS-1)
           STOP RUN.
[1 Harbour Way       ][Bristol             ]
Group length: 40

After replacement the compiler sees 05 BILL-STREET PIC X(20)., so the program can refer to BILL-STREET.

What goes into copybooks

Copybook content Why
File record layouts Every program reading the file sees the same fields
CALL parameter blocks Caller and subprogram agree byte for byte (LINKAGE SECTION and WORKING-STORAGE)
DB2 table layouts (DCLGEN output) Generated host variables for SQL
CICS screen maps (BMS symbolic maps) Generated field layouts for terminals
Common error-handling paragraphs Shared PROCEDURE DIVISION code (less common)

Warning: Changing a copybook changes every program that copies it, but only after each one is recompiled. A layout change in a copybook without recompiling all its users is a classic production incident: old programs keep using the old layout.

Test yourself

When is a copybook's text inserted into a program?

  1. At run time, when the program starts
  2. At compile time, where the COPY statement appears
  3. When the program calls it
  4. Only when linking

What does COPY ADDRESS REPLACING ==:PFX:== BY ==SHIP==. do?

  1. Calls a program named ADDRESS
  2. Copies ADDRESS and replaces every :PFX: with SHIP
  3. Renames the copybook file
  4. Copies only the lines containing SHIP

Why do programs need recompiling after a copybook changes?

  1. They don't
  2. Because the copybook text was compiled into each program; old executables still contain the old layout
  3. Because COPY is dynamic
  4. To refresh the copy library index