A copybook is a section of code written in assembly language or a high-level programming language that can be copied (from a master) and inserted into several different programs (or multiple places in a single program). It is often used to define the physical layout of program data, pieces of procedural code and prototypes.
The term “copybook” may have originated from use of COBOL on IBM mainframe operating systems: COPY is the COBOL keyword for this, and the master item was stored as a “book” within a larger library of source code.[1] Major reasons for using COPYBOOKs in assembly language or COBOL are to: • • • •
Ensure that everyone uses the same version of a data layout definition or procedural code. Make it easier to cross reference where components are used in a system. Make it easier to change programs when needed (only one master copy to change). Save programmer time by not needing to code extensive data layouts (minor, but useful).
Since COBOL defined the COPY statement in 1959, many other languages have included a similar feature, often using “include” as a keyword (e.g. Fortran, ALGOL, PL/I, C, C++ and HTML). The term “copybook” is mainly used in an IBM mainframe context; other languages have different names and syntaxes: COPY textname. (COBOL), /COPY QCPYLESRC,QBC (RPG IV – first argument is the filename, second argument is the copybook), include ... (Fortran), %include ... (PL/I), #include ... (C and C++), include ... (MASM), (HTML SSI), <%@ include ... %> (JSP). Several languages have a replacement feature in their copybook syntax. This allows a programmer to replace certain text in the copybook with other text when copying it into the program. This makes the copybook more versatile, since it can be re-used in the same program. For example, the same copybook can be used to describe the input and output files within the same program. Replacements in COBOL are invoked using the second form of the COPY statement: COPY text-name REPLACING item-1 BY item-2. Modern languages (e.g. Haskell and Java) tend to avoid copybooks or includes, preferring modules and import/export systems for namespace control.