SlideShare a Scribd company logo
Using the
COPY Verb
In
COBOL
Introduction
COPY statements in a COBOL program are very different from other COBOL
statements. While other statements are executed at run time, COPY
statements are executed at compile time.
A COPY statement is similar to the "Include" used in languages like C
and C++. It allows programs to include frequently used source code
text.
When a COPY statement is used in a COBOL program the source code text
is copied into the program from from a copy file or from a copy library
before the program is compiled.
A copy file, is a file containing a segment of COBOL code.
A copy library, is a collection code segment each of which can be
referenced using a name. Each program that wants to use items described
in the copy library uses the COPY verb to include the descriptions it
requires.
When COPY statements include source code in a program, the code can be
included without change or the text can be changed as it is copied into
the program. The ability to change the code as it being included
greatly adds to the versatility of the COPY verb.
Using the COPY verb in large software systems
The COPY verb is generally used when creating large software systems.
These systems are subject to a number of problems that the COPY verb
helps to alleviate.
For instance, when data files are used by a number of programs in a
large software system, it is easy for programmers describing those
files to make errors in defining the key fields (Indexed files) or the
file organization or the type of access allowed or the number, type and
size of the fields in a record. Any errors in these descriptions may
result in difficult-to-find bugs. For instance, if one program writes a
file using an incorrect record description and the others read it using
the correct description, a program crash may occur in in one of the
correct subprograms rather than the one which actually has the problem.
Using copy libraries or files helps to reduce programmer transcription
errors and also makes implementation simpler by reducing the amount of
coding required. For instance, when a number of programs need to access
the same file, the relevant file and record descriptions can be copied
from a copy library instead of each programmer having to type their own
(and possibly get them wrong).
Another advantage of using copy libraries is that they permit item
descriptions such as file, record and table descriptions, to be kept
and updated centrally in a copy library, often under the control of a
copy librarian. This makes it more difficult for programmers to make ad
hoc changes to file and record formats. Such changes generally have to
be approved by the COPY librarian.
In a large software system using the COPY verb makes some maintenance
tasks easier and safer. Certain changes may only require an update to
the text in the copy library and a recompilation of any affected
programs. If a copy library were not used each affected program would
have to be changed.
COPY Syntax
Text Words
The matching procedure in the REPLACING phrase operates on text words.
A text word is defined as :
o Any COBOL text enclosed in double equal signs (e.g. ==ADD 1==). This
text is known as PseudoText and it allows us to replace a series of
words or characters as opposed to an individual identifier, literal
or word.
o Any literal including opening and closing quotes.
o Any separator other than
- A space
- A Pseudo-Text delimiter
- A Comma
o Any COBOL reserved word.
o Any Identifier.
o Any other sequence of contiguous characters bounded by separators.
Text Word examples
MOVE
1 Text Word
MOVE Total TO Print-Total
4 Text Words - MOVE Total TO Print-Total
MOVE Total TO Print-Total.
5 Text Words - MOVE Total TO Print-Total .
PIC S9(4)V9(6)
9 Text words - PIC S9 ( 4 ) V9 ( 6 )
“PIC S9(4)V9(6)”
1 Text word - “PIC S9(4)V9(6)”
How the COPY works
If the REPLACING phrase is not used the compiler simply copies the text
into the client program without change.
If the COPY does use the REPLACING phrase the text is copied and each
occurrence of TextWord1 in the library text is replaced by the
corresponding TextWord2 in the REPLACING phrase.
Some example COPY statements
COPY CopyFile2 REPLACING XYZ BY 120.
COPY CopyFile5 REPLACING X(3) BY X(19).
COPY CopyFile3 IN EGLIB.
COPY CopyFile5 REPLACING ==X(3)== BY ==X(19)==.
COPY CopyFile4 IN EGLIB.
How the REPLACING phrase works
The REPLACING phrase tries to match the TextWord1 items with text words
in the library text. If a match is achieved, then as the text is copied
from the library, the matched text is replaced by the TextWord2 items.
For purposes of matching, each occurrence of a separator comma,
semicolon, space or comment line in the library text or in TextWord1 is
considered to be a single space.
Each sequence of one or more space separators is also considered to be
a single space.
Comment lines in the library text is or in TextWord2 are copied into
the target text unchanged.
COPY Rules
1. When TextWord1 is PseudoText it must not be null, nor can it
consist solely of either the character space(s) or comment lines.
2. When TextWord2 is PseudoText it can be null. Null PseudoText is
indicated by four equal signs (e.g. ====)
3. A COPY statement can occur anywhere a character-string or a
separator can occur except that a COPY statement must not occur
within another COPY statement.
4. TextName defines a unique external filename which conforms to the
rules for user defined words.
5. If the word COPY appears in a comment-entry or in the place where
a comment entry can appear, it is considered part of the comment-
entry.
6. The text produced as a result of the complete processing of a
COPY statement must not contain a COPY statement.
7. Normally a textword in the copy text is replaced by a textword in
the replacement text but it is possible to replace only part of a
textword by enclosing the text to be replaced by either
parentheses or colons.
For example: (Prefix) or :Prefix:
COPY Example1
Source Text
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG1.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
COPY COPYFILE1.
PROCEDURE DIVISION.
BeginProg.
OPEN INPUT StudentFile
READ StudentFile
AT END SET EndOfSF TO TRUE
END-READ
PERFORM UNTIL EndOfSF
DISPLAY StudentNumber SPACE StudentName SPACE
CourseCode SPACE FeesOwed SPACE AmountPaid
READ StudentFile
AT END SET EndOfSF TO TRUE
END-READ
END-PERFORM
STOP RUN.
Text in COPYFILE1
01 StudentRec.
88 EndOfSF VALUE HIGH-VALUES.
02 StudentNumber PIC 9(7).
02 StudentName PIC X(60).
02 CourseCode PIC X(4).
02 FeesOwed PIC 9(4).
02 AmountPaid PIC 9(4)V99.
Source Text after processing
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG1.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
COPY CopyFile1.
01 StudentRec.
88 EndOfSF VALUE HIGH-VALUES.
02 StudentNumber PIC 9(7).
02 StudentName PIC X(60).
02 CourseCode PIC X(4).
02 FeesOwed PIC 9(4).
02 AmountPaid PIC 9(4)V99.
PROCEDURE DIVISION.
BeginProg.
OPEN INPUT StudentFile
READ StudentFile
AT END SET EndOfSF TO TRUE
END-READ
PERFORM UNTIL EndOfSF
DISPLAY StudentNumber SPACE StudentName SPACE
CourseCode SPACE FeesOwed SPACE AmountPaid
READ StudentFile
AT END SET EndOfSF TO TRUE
END-READ
END-PERFORM
STOP RUN.
Text in green is the copy statement in the source text before processing.
Text in red is the copied library text which may have been altered by the REPLACING phrase if
one was present and found matching text in the library file.
Text in grey is the COPY statement, which has been applied.
COPY Example2
Source Text
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG2.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NameTable.
*See note1
COPY CopyFile2 REPLACING XYZ BY 120.
01 CopyData.
*See note2
COPY CopyFile3 IN EGLIB
REPLACING ==R== BY ==4==.
*See note3
COPY CopyFile4 REPLACING ==V99== BY ====.
*See note4
COPY CopyFile5 REPLACING "KEY" BY "ABC".
*See note5
COPY CopyFile5 REPLACING KEY BY ABC.
*See note6
COPY CopyFile5 REPLACING "CustKey" BY "Cust".
*See note7
COPY CopyFile5 REPLACING "KEY" BY =="ABC".
02 CustNum PIC 9(8)==.
PROCEDURE DIVISION.
BeginProg.
STOP RUN.
Text in CopyFile2
02 StudName PIC X(20) OCCURS XYZ TIMES.
Text in CopyFile3
02 CustOrder PIC 9(R).
Text in CopyFile4
02 CustOrder2 PIC 9(6)V99.
Text in CopyFile5
02 CustKey PIC X(3) VALUE "KEY".
Source Text after processing
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG2.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NameTable.
*See note1
COPY CopyFile2 REPLACING XYZ BY 120.
02 StudName PIC X(20) OCCURS 120 TIMES.
01 CopyData.
*See note2
COPY CopyFile3 IN EGLIB
REPLACING ==R== BY ==4==.
02 CustOrder PIC 9(4).
*See note3
COPY CopyFile4 REPLACING ==V99== BY ====.
02 CustOrder2 PIC 9(6).
*See note4
COPY CopyFile5 REPLACING "KEY" BY "ABC".
02 CustKey PIC X(3) VALUE "ABC".
*See note5
COPY CopyFile5 REPLACING KEY BY ABC.
02 CustKey PIC X(3) VALUE "KEY".
*See note6
COPY CopyFile5 REPLACING "CustKey" BY "Cust".
02 CustKey PIC X(3) VALUE "KEY".
*See note7
COPY CopyFile5 REPLACING "KEY" BY =="ABC".
02 CustNum PIC 9(8)==.
02 CustKey PIC X(3) VALUE "ABC".
02 CustNum PIC 9(8).
PROCEDURE DIVISION.
BeginProg.
STOP RUN.
Note1
The TextWord XYZ in the library text is replaced by 120.
Note2
The PseudoText symbols tell us that we are looking for the text word R in the library text. Other
R's in the text which might be part of a word are not matched because they are not text words.
The replaced R is a text word because it is bounded by two other text words i.e. the parentheses
( and ).
Note3
The PseudoText ==V99== is replaced by nothing; effectively deleting it.
Note4
The literal "KEY" in the library text is replaced by "ABC"
Note5
In this case the library text is copied without change because the literal "KEY" in the library text is
not the same as the word KEY in TextWord2.
Note6
Similar to the previous example demonstrates the difference between CustKey and the literal
"CustKey". A literal text word includes the opening and closing quotes.
Note7
Replaces "KEY" by two lines of text. Note that we are only replacing the literal "KEY" in the library
text. So the full stop after "KEY" is not replaced and that is why there is no full stop after the PIC
9(8) in the TextWord2 replacement text.
COPY Example3
Source Text
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG3.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CopyData.
*See note1
COPY CopyFile5 REPLACING ( BY @.
*See note2
COPY CopyFile5 REPLACING 3 BY three.
*See note3
COPY CopyFile5 REPLACING ) BY &.
*See note4
COPY CopyFile5 REPLACING X BY ==Replace the X==.
*See note5
COPY CopyFile5 REPLACING ==X(3)== BY ==X(19)==.
*See note6
COPY CopyFile5 REPLACING X(3) BY X(19).
*See note7
COPY CopyFile5 REPLACING PIC BY ==Pic is Replaced==.
*See note8
COPY CopyFile5 REPLACING P BY ==But P in PIC not replaced==.
PROCEDURE DIVISION.
BeginProg.
STOP RUN.
Library Text in CopyFile5
02 CustKey PIC X(3) VALUE "KEY".
Source Text after processing
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG3.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CopyData.
*See note1
COPY CopyFile5 REPLACING ( BY @.
02 CustKey PIC X@3) VALUE "KEY".
*See note2
COPY CopyFile5 REPLACING 3 BY three.
02 CustKey PIC X(three) VALUE "KEY".
*See note3
COPY CopyFile5 REPLACING ) BY &.
02 CustKey PIC X(3& VALUE "KEY".
*See note4
COPY CopyFile5 REPLACING X BY ==Replace the X==.
02 CustKey PIC Replace the X(3) VALUE "KEY".
*See note5
COPY CopyFile5 REPLACING ==X(3)== BY ==X(19)==.
02 CustKey PIC X(19) VALUE "KEY".
*See note6
COPY CopyFile5 REPLACING X(3) BY X(19).
02 CustKey PIC X(19) VALUE "KEY".
*See note7
COPY CopyFile5 REPLACING PIC BY ==Pic is Replaced==.
02 CustKey Pic is Replaced X(3) VALUE "KEY".
*See note8
COPY CopyFile5 REPLACING P BY ==But P in PIC not replaced==.
02 CustKey PIC X(3) VALUE "KEY".
PROCEDURE DIVISION.
BeginProg.
STOP RUN.
Note that these COPY statements, which are themselves syntactically correct, result in
statements which cause syntax errors. This is a clear indication that COPY statements are
processed before the resulting program is compiled.
Note1
The text word ( in the library text is replaced by @. Demonstrates that ( is a text word.
Note2
Demonstrates that the 3 between ( and ) is a text word.
Note3
Demonstrates that ) is a textword.
Note4
Demonstrates that the X before (3) is a textword and is replaced by the PseudoText "Replace the
X"
Note5
The PseudoText X(3) is replaced by X(19)
Note6
This looks the same as number 5 but what is actually happening is that the series of textwords
X and ( and 3 and ) is replaced by the series X ( 19 )
Note7
The textword PIC (bounded by separator spaces) is replaced by the PseudoText Pic is Replaced
Note8
But the letter P in PIC is not a textword by itself and so is not replaced.
COPY Example4
Source Text
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG4.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE 3.
01 Num2 PIC 9 VALUE 5.
01 Result PIC 99 VALUE ZEROS.
01 CopyData.
*See note1
COPY CopyFile6 REPLACING "Mike" BY "Tony"
?? BY 15.
PROCEDURE DIVISION.
BeginProg.
*See note2
COPY CopyFile7 REPLACING N1 BY Num1
N2 BY Num2
R1 BY Result.
DISPLAY "The result is = " Result.
STOP RUN.
Library Text in CopyFile6
02 CustKey PIC X(4) VALUE "Mike".
01 NameTable PIC X(10) OCCURS ?? TIMES.
Library Text in CopyFile7
ADD N1, N2 GIVING R1.
Source Text after processing
$SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. COPYEG4.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE 3.
01 Num2 PIC 9 VALUE 5.
01 Result PIC 99 VALUE ZEROS.
01 CopyData.
*See note1
COPY CopyFile6 REPLACING "Mike" BY "Tony"
?? BY 15.
02 CustKey PIC X(4) VALUE "Tony".
01 NameTable PIC X(10) OCCURS 15 TIMES.
PROCEDURE DIVISION.
BeginProg.
*See note2
COPY CopyFile7 REPLACING N1 BY Num1
N2 BY Num2
R1 BY Result.
ADD Num1, Num2 GIVING Result.
STOP RUN.
Note1
This example demonstrates that a single COPY statement can be used to replace more than one
set of items.
Note2
As above.

More Related Content

PDF
topic_perlcgi
PDF
Perl_Part7
PDF
6 preprocessor macro header
PPT
Php5 vs php7
PDF
ANSI C Macros
PPTX
POLITEKNIK MALAYSIA
PPTX
Fundamentals of programming
PDF
Cp week _2.
topic_perlcgi
Perl_Part7
6 preprocessor macro header
Php5 vs php7
ANSI C Macros
POLITEKNIK MALAYSIA
Fundamentals of programming
Cp week _2.

What's hot (9)

PDF
dylibencapsulation
PDF
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
PPTX
Chapter 2
PDF
Pdf open parameters
PDF
Working with the IFS on System i
PDF
PPT
Ch3 gnu make
PDF
Core python programming tutorial
PPTX
Preprocessor directives in c language
dylibencapsulation
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Chapter 2
Pdf open parameters
Working with the IFS on System i
Ch3 gnu make
Core python programming tutorial
Preprocessor directives in c language
Ad

Viewers also liked (18)

PDF
St John Ambulance Youth Launch10
PDF
3 01
PPT
PDF
DINE Hozho Workshop Notes Aug 14, 15, 18, 2014 (1)
PDF
Portfolio Evins Design 2011
PPTX
Stem cell therapy
PPTX
Pharmacodynamics
PPTX
Depression and mania
PPTX
Good Clinical Practices
PPTX
Coagulants and anti coagulants
PPTX
Effect of drugs on frog’s heart
PPTX
Cholinergic system and drugs
PPTX
Beta adrenergic blockers
PPTX
Colorimetry and spectroscopy
PPTX
Anti anginal drugs, uses, mechanism of action, adverse effects
PPTX
Drug discovery and development
St John Ambulance Youth Launch10
3 01
DINE Hozho Workshop Notes Aug 14, 15, 18, 2014 (1)
Portfolio Evins Design 2011
Stem cell therapy
Pharmacodynamics
Depression and mania
Good Clinical Practices
Coagulants and anti coagulants
Effect of drugs on frog’s heart
Cholinergic system and drugs
Beta adrenergic blockers
Colorimetry and spectroscopy
Anti anginal drugs, uses, mechanism of action, adverse effects
Drug discovery and development
Ad

Similar to Copy verb in cobol (20)

PPT
Bb Tequila Coding Style (Draft)
PDF
POLITEKNIK MALAYSIA
PDF
Python Namespace.pdf
PDF
Oracle DBA interview_questions
PDF
PPTX
cbybalaguruswami-e-180803051831.pptx
PPTX
cbybalaguruswami-e-180803051831.pptx
PPTX
6 assembly language computer organization
DOCX
1 CMPS 12M Introduction to Data Structures Lab La.docx
PPTX
programming for problem solving in C and C++.pptx
PPTX
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
PPTX
Switch case and looping statement
DOCX
NamingConvention
PPTX
Switch case looping
TXT
microsoft help
PDF
topic_perlcgi
PPTX
Fundamentalsofprogrammingfinal 121011003536-phpapp02
Bb Tequila Coding Style (Draft)
POLITEKNIK MALAYSIA
Python Namespace.pdf
Oracle DBA interview_questions
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
6 assembly language computer organization
1 CMPS 12M Introduction to Data Structures Lab La.docx
programming for problem solving in C and C++.pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Switch case and looping statement
NamingConvention
Switch case looping
microsoft help
topic_perlcgi
Fundamentalsofprogrammingfinal 121011003536-phpapp02

Recently uploaded (20)

PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PPTX
Modernising the Digital Integration Hub
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
1. Introduction to Computer Programming.pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PPT
What is a Computer? Input Devices /output devices
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Final SEM Unit 1 for mit wpu at pune .pptx
A comparative study of natural language inference in Swahili using monolingua...
Developing a website for English-speaking practice to English as a foreign la...
WOOl fibre morphology and structure.pdf for textiles
A contest of sentiment analysis: k-nearest neighbor versus neural network
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Modernising the Digital Integration Hub
NewMind AI Weekly Chronicles – August ’25 Week III
NewMind AI Weekly Chronicles - August'25-Week II
1. Introduction to Computer Programming.pptx
Getting Started with Data Integration: FME Form 101
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
TLE Review Electricity (Electricity).pptx
What is a Computer? Input Devices /output devices
Getting started with AI Agents and Multi-Agent Systems
From MVP to Full-Scale Product A Startup’s Software Journey.pdf

Copy verb in cobol

  • 2. Introduction COPY statements in a COBOL program are very different from other COBOL statements. While other statements are executed at run time, COPY statements are executed at compile time. A COPY statement is similar to the "Include" used in languages like C and C++. It allows programs to include frequently used source code text. When a COPY statement is used in a COBOL program the source code text is copied into the program from from a copy file or from a copy library before the program is compiled. A copy file, is a file containing a segment of COBOL code. A copy library, is a collection code segment each of which can be referenced using a name. Each program that wants to use items described in the copy library uses the COPY verb to include the descriptions it requires. When COPY statements include source code in a program, the code can be included without change or the text can be changed as it is copied into the program. The ability to change the code as it being included greatly adds to the versatility of the COPY verb. Using the COPY verb in large software systems The COPY verb is generally used when creating large software systems. These systems are subject to a number of problems that the COPY verb helps to alleviate. For instance, when data files are used by a number of programs in a large software system, it is easy for programmers describing those files to make errors in defining the key fields (Indexed files) or the file organization or the type of access allowed or the number, type and size of the fields in a record. Any errors in these descriptions may result in difficult-to-find bugs. For instance, if one program writes a file using an incorrect record description and the others read it using the correct description, a program crash may occur in in one of the correct subprograms rather than the one which actually has the problem. Using copy libraries or files helps to reduce programmer transcription errors and also makes implementation simpler by reducing the amount of coding required. For instance, when a number of programs need to access the same file, the relevant file and record descriptions can be copied from a copy library instead of each programmer having to type their own (and possibly get them wrong). Another advantage of using copy libraries is that they permit item descriptions such as file, record and table descriptions, to be kept and updated centrally in a copy library, often under the control of a copy librarian. This makes it more difficult for programmers to make ad hoc changes to file and record formats. Such changes generally have to be approved by the COPY librarian.
  • 3. In a large software system using the COPY verb makes some maintenance tasks easier and safer. Certain changes may only require an update to the text in the copy library and a recompilation of any affected programs. If a copy library were not used each affected program would have to be changed. COPY Syntax Text Words The matching procedure in the REPLACING phrase operates on text words. A text word is defined as : o Any COBOL text enclosed in double equal signs (e.g. ==ADD 1==). This text is known as PseudoText and it allows us to replace a series of words or characters as opposed to an individual identifier, literal or word. o Any literal including opening and closing quotes. o Any separator other than - A space - A Pseudo-Text delimiter - A Comma o Any COBOL reserved word. o Any Identifier. o Any other sequence of contiguous characters bounded by separators. Text Word examples MOVE 1 Text Word MOVE Total TO Print-Total 4 Text Words - MOVE Total TO Print-Total MOVE Total TO Print-Total. 5 Text Words - MOVE Total TO Print-Total . PIC S9(4)V9(6) 9 Text words - PIC S9 ( 4 ) V9 ( 6 )
  • 4. “PIC S9(4)V9(6)” 1 Text word - “PIC S9(4)V9(6)” How the COPY works If the REPLACING phrase is not used the compiler simply copies the text into the client program without change. If the COPY does use the REPLACING phrase the text is copied and each occurrence of TextWord1 in the library text is replaced by the corresponding TextWord2 in the REPLACING phrase. Some example COPY statements COPY CopyFile2 REPLACING XYZ BY 120. COPY CopyFile5 REPLACING X(3) BY X(19). COPY CopyFile3 IN EGLIB. COPY CopyFile5 REPLACING ==X(3)== BY ==X(19)==. COPY CopyFile4 IN EGLIB. How the REPLACING phrase works The REPLACING phrase tries to match the TextWord1 items with text words in the library text. If a match is achieved, then as the text is copied from the library, the matched text is replaced by the TextWord2 items. For purposes of matching, each occurrence of a separator comma, semicolon, space or comment line in the library text or in TextWord1 is considered to be a single space. Each sequence of one or more space separators is also considered to be a single space. Comment lines in the library text is or in TextWord2 are copied into the target text unchanged. COPY Rules 1. When TextWord1 is PseudoText it must not be null, nor can it consist solely of either the character space(s) or comment lines. 2. When TextWord2 is PseudoText it can be null. Null PseudoText is indicated by four equal signs (e.g. ====) 3. A COPY statement can occur anywhere a character-string or a separator can occur except that a COPY statement must not occur within another COPY statement. 4. TextName defines a unique external filename which conforms to the rules for user defined words. 5. If the word COPY appears in a comment-entry or in the place where a comment entry can appear, it is considered part of the comment- entry.
  • 5. 6. The text produced as a result of the complete processing of a COPY statement must not contain a COPY statement. 7. Normally a textword in the copy text is replaced by a textword in the replacement text but it is possible to replace only part of a textword by enclosing the text to be replaced by either parentheses or colons. For example: (Prefix) or :Prefix: COPY Example1 Source Text $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG1. AUTHOR. Michael Coughlan. ENVIRONMENT DIVISION. FILE-CONTROL. SELECT StudentFile ASSIGN TO "STUDENTS.DAT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD StudentFile. COPY COPYFILE1. PROCEDURE DIVISION. BeginProg. OPEN INPUT StudentFile READ StudentFile AT END SET EndOfSF TO TRUE END-READ PERFORM UNTIL EndOfSF DISPLAY StudentNumber SPACE StudentName SPACE CourseCode SPACE FeesOwed SPACE AmountPaid READ StudentFile AT END SET EndOfSF TO TRUE END-READ END-PERFORM STOP RUN. Text in COPYFILE1 01 StudentRec. 88 EndOfSF VALUE HIGH-VALUES. 02 StudentNumber PIC 9(7). 02 StudentName PIC X(60). 02 CourseCode PIC X(4). 02 FeesOwed PIC 9(4). 02 AmountPaid PIC 9(4)V99.
  • 6. Source Text after processing $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG1. AUTHOR. Michael Coughlan. ENVIRONMENT DIVISION. FILE-CONTROL. SELECT StudentFile ASSIGN TO "STUDENTS.DAT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD StudentFile. COPY CopyFile1. 01 StudentRec. 88 EndOfSF VALUE HIGH-VALUES. 02 StudentNumber PIC 9(7). 02 StudentName PIC X(60). 02 CourseCode PIC X(4). 02 FeesOwed PIC 9(4). 02 AmountPaid PIC 9(4)V99. PROCEDURE DIVISION. BeginProg. OPEN INPUT StudentFile READ StudentFile AT END SET EndOfSF TO TRUE END-READ PERFORM UNTIL EndOfSF DISPLAY StudentNumber SPACE StudentName SPACE CourseCode SPACE FeesOwed SPACE AmountPaid READ StudentFile AT END SET EndOfSF TO TRUE END-READ END-PERFORM STOP RUN. Text in green is the copy statement in the source text before processing. Text in red is the copied library text which may have been altered by the REPLACING phrase if one was present and found matching text in the library file. Text in grey is the COPY statement, which has been applied. COPY Example2 Source Text $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG2. AUTHOR. Michael Coughlan.
  • 7. DATA DIVISION. WORKING-STORAGE SECTION. 01 NameTable. *See note1 COPY CopyFile2 REPLACING XYZ BY 120. 01 CopyData. *See note2 COPY CopyFile3 IN EGLIB REPLACING ==R== BY ==4==. *See note3 COPY CopyFile4 REPLACING ==V99== BY ====. *See note4 COPY CopyFile5 REPLACING "KEY" BY "ABC". *See note5 COPY CopyFile5 REPLACING KEY BY ABC. *See note6 COPY CopyFile5 REPLACING "CustKey" BY "Cust". *See note7 COPY CopyFile5 REPLACING "KEY" BY =="ABC". 02 CustNum PIC 9(8)==. PROCEDURE DIVISION. BeginProg. STOP RUN. Text in CopyFile2 02 StudName PIC X(20) OCCURS XYZ TIMES. Text in CopyFile3 02 CustOrder PIC 9(R). Text in CopyFile4 02 CustOrder2 PIC 9(6)V99. Text in CopyFile5 02 CustKey PIC X(3) VALUE "KEY". Source Text after processing $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG2. AUTHOR. Michael Coughlan.
  • 8. DATA DIVISION. WORKING-STORAGE SECTION. 01 NameTable. *See note1 COPY CopyFile2 REPLACING XYZ BY 120. 02 StudName PIC X(20) OCCURS 120 TIMES. 01 CopyData. *See note2 COPY CopyFile3 IN EGLIB REPLACING ==R== BY ==4==. 02 CustOrder PIC 9(4). *See note3 COPY CopyFile4 REPLACING ==V99== BY ====. 02 CustOrder2 PIC 9(6). *See note4 COPY CopyFile5 REPLACING "KEY" BY "ABC". 02 CustKey PIC X(3) VALUE "ABC". *See note5 COPY CopyFile5 REPLACING KEY BY ABC. 02 CustKey PIC X(3) VALUE "KEY". *See note6 COPY CopyFile5 REPLACING "CustKey" BY "Cust". 02 CustKey PIC X(3) VALUE "KEY". *See note7 COPY CopyFile5 REPLACING "KEY" BY =="ABC". 02 CustNum PIC 9(8)==. 02 CustKey PIC X(3) VALUE "ABC". 02 CustNum PIC 9(8). PROCEDURE DIVISION. BeginProg. STOP RUN. Note1 The TextWord XYZ in the library text is replaced by 120. Note2 The PseudoText symbols tell us that we are looking for the text word R in the library text. Other R's in the text which might be part of a word are not matched because they are not text words. The replaced R is a text word because it is bounded by two other text words i.e. the parentheses ( and ). Note3 The PseudoText ==V99== is replaced by nothing; effectively deleting it. Note4 The literal "KEY" in the library text is replaced by "ABC"
  • 9. Note5 In this case the library text is copied without change because the literal "KEY" in the library text is not the same as the word KEY in TextWord2. Note6 Similar to the previous example demonstrates the difference between CustKey and the literal "CustKey". A literal text word includes the opening and closing quotes. Note7 Replaces "KEY" by two lines of text. Note that we are only replacing the literal "KEY" in the library text. So the full stop after "KEY" is not replaced and that is why there is no full stop after the PIC 9(8) in the TextWord2 replacement text. COPY Example3 Source Text $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG3. AUTHOR. Michael Coughlan. DATA DIVISION. WORKING-STORAGE SECTION. 01 CopyData. *See note1 COPY CopyFile5 REPLACING ( BY @. *See note2 COPY CopyFile5 REPLACING 3 BY three. *See note3 COPY CopyFile5 REPLACING ) BY &. *See note4 COPY CopyFile5 REPLACING X BY ==Replace the X==. *See note5 COPY CopyFile5 REPLACING ==X(3)== BY ==X(19)==. *See note6 COPY CopyFile5 REPLACING X(3) BY X(19). *See note7 COPY CopyFile5 REPLACING PIC BY ==Pic is Replaced==. *See note8 COPY CopyFile5 REPLACING P BY ==But P in PIC not replaced==. PROCEDURE DIVISION. BeginProg. STOP RUN.
  • 10. Library Text in CopyFile5 02 CustKey PIC X(3) VALUE "KEY". Source Text after processing $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG3. AUTHOR. Michael Coughlan. DATA DIVISION. WORKING-STORAGE SECTION. 01 CopyData. *See note1 COPY CopyFile5 REPLACING ( BY @. 02 CustKey PIC X@3) VALUE "KEY". *See note2 COPY CopyFile5 REPLACING 3 BY three. 02 CustKey PIC X(three) VALUE "KEY". *See note3 COPY CopyFile5 REPLACING ) BY &. 02 CustKey PIC X(3& VALUE "KEY". *See note4 COPY CopyFile5 REPLACING X BY ==Replace the X==. 02 CustKey PIC Replace the X(3) VALUE "KEY". *See note5 COPY CopyFile5 REPLACING ==X(3)== BY ==X(19)==. 02 CustKey PIC X(19) VALUE "KEY". *See note6 COPY CopyFile5 REPLACING X(3) BY X(19). 02 CustKey PIC X(19) VALUE "KEY". *See note7 COPY CopyFile5 REPLACING PIC BY ==Pic is Replaced==. 02 CustKey Pic is Replaced X(3) VALUE "KEY". *See note8 COPY CopyFile5 REPLACING P BY ==But P in PIC not replaced==. 02 CustKey PIC X(3) VALUE "KEY". PROCEDURE DIVISION. BeginProg. STOP RUN. Note that these COPY statements, which are themselves syntactically correct, result in statements which cause syntax errors. This is a clear indication that COPY statements are processed before the resulting program is compiled.
  • 11. Note1 The text word ( in the library text is replaced by @. Demonstrates that ( is a text word. Note2 Demonstrates that the 3 between ( and ) is a text word. Note3 Demonstrates that ) is a textword. Note4 Demonstrates that the X before (3) is a textword and is replaced by the PseudoText "Replace the X" Note5 The PseudoText X(3) is replaced by X(19) Note6 This looks the same as number 5 but what is actually happening is that the series of textwords X and ( and 3 and ) is replaced by the series X ( 19 ) Note7 The textword PIC (bounded by separator spaces) is replaced by the PseudoText Pic is Replaced Note8 But the letter P in PIC is not a textword by itself and so is not replaced. COPY Example4 Source Text $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG4. AUTHOR. Michael Coughlan. DATA DIVISION. WORKING-STORAGE SECTION. 01 Num1 PIC 9 VALUE 3. 01 Num2 PIC 9 VALUE 5. 01 Result PIC 99 VALUE ZEROS. 01 CopyData. *See note1 COPY CopyFile6 REPLACING "Mike" BY "Tony" ?? BY 15. PROCEDURE DIVISION. BeginProg. *See note2 COPY CopyFile7 REPLACING N1 BY Num1 N2 BY Num2 R1 BY Result. DISPLAY "The result is = " Result. STOP RUN.
  • 12. Library Text in CopyFile6 02 CustKey PIC X(4) VALUE "Mike". 01 NameTable PIC X(10) OCCURS ?? TIMES. Library Text in CopyFile7 ADD N1, N2 GIVING R1. Source Text after processing $SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. COPYEG4. AUTHOR. Michael Coughlan. DATA DIVISION. WORKING-STORAGE SECTION. 01 Num1 PIC 9 VALUE 3. 01 Num2 PIC 9 VALUE 5. 01 Result PIC 99 VALUE ZEROS. 01 CopyData. *See note1 COPY CopyFile6 REPLACING "Mike" BY "Tony" ?? BY 15. 02 CustKey PIC X(4) VALUE "Tony". 01 NameTable PIC X(10) OCCURS 15 TIMES. PROCEDURE DIVISION. BeginProg. *See note2 COPY CopyFile7 REPLACING N1 BY Num1 N2 BY Num2 R1 BY Result. ADD Num1, Num2 GIVING Result. STOP RUN. Note1 This example demonstrates that a single COPY statement can be used to replace more than one set of items. Note2 As above.