Reading — step 1 of 4
Learn
Copybooks are COBOL's #include — text files of code copied into your program at compile time. They're how you share record layouts and constants across many programs.
The basics
A copybook is just a text file (typically .cpy or .cob) with COBOL declarations:
*> file: customer.cpy
01 CUSTOMER-RECORD.
05 CUST-ID PIC X(10).
05 CUST-NAME PIC X(40).
05 CUST-ADDRESS.
10 STREET PIC X(30).
10 CITY PIC X(20).
10 ZIP PIC 9(5).
05 CUST-BALANCE PIC 9(7)V99.
Use it with COPY:
DATA DIVISION.
WORKING-STORAGE SECTION.
COPY "customer.cpy".
PROCEDURE DIVISION.
MOVE "123" TO CUST-ID.
...
The COPY statement is replaced verbatim by the file contents at compile time.
Replacing
COPY "customer.cpy" REPLACING ==CUST== BY ==BUYER==.
This would substitute BUYER for every CUST in the copybook — useful when you want to use the same layout twice with different prefixes.
Why copybooks matter
In mainframe shops, a single record layout might be used by 100+ programs. Storing it in one place means:
- Schema changes update everywhere on rebuild
- Programs can't disagree about field offsets
- A central data dictionary is enforced
Most financial mainframe shops have thousands of copybooks defining records, constants, screen formats, etc.
Common copybook contents
- Record layouts — for files, messages, screens
- Constants — error codes, status flags
- Procedure code — common sequences (validation, logging) included into PROCEDURE DIVISION
- CICS / IMS structures — system parameters for transaction processing
Conventions
- File extension:
.cpy,.cob,.cobcopy, varies by shop - Naming:
<DOMAIN>-<DESCRIPTION>— e.g.,CUST-DTLS.cpy,ORDER-HDR.cpy - One record/group per copybook (not always followed)
- Comment header with revision history
Modern alternatives
- GnuCOBOL supports COPY (we'd use it if Judge0 had a real filesystem)
- Procedural COBOL on z/OS uses CA Endevor or similar SCM tools to manage copybooks
- Modern COBOL (Cobol-2002+) has better module systems but copybooks remain ubiquitous
For this lesson we can't use real COPY (no filesystem). Below: simulate the pattern by inlining what would be a copybook.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…