"COPY" statement with "REPLACING" in COBOL
Asked Answered
P

2

6

I am getting compilation error as,

A "COPY" statement with "REPLACING" phrase was found within a nested "COPY".

This is our compilation setting that we can not use REPLACING verb in nested copy. We have one copybook which is having multiple copy statements with replacing verb. Can anyone help me to resolve this error?

Palladium answered 12/9, 2011 at 10:14 Comment(3)
The error message says it all. Try to un-nest the copy. Show some of the code to get more help. (It's almost 30 years ago that I've worked with cobol, I never thought people are still using it :-)Kroll
We can un-nest the copybook..but for reusability of code we have created it in that way. I am just trying to search is there anny other way of doing that. My COBOL PGM contains: COPY ABC. and Copybook ABC has , COPY XYZ REPLACING ==:A:== BY ==B-==. COPY PQR REPLACING ==:A:== BY ==B-==. COPY LMN REPLACING ==:A:== BY ==B-==.Palladium
From mainframegurukul.com/tutorials/programming/cobol/…: COPY statements can be nested. However, a) Nested COPY statements cannot contain the REPLACING phrase, and a COPY statement with the REPLACING phrase cannot contain nested COPY statement b) Nested COPY statement cannot cause recursion. But it is not advisbale to use nested COPY statements, as it will impacts readability of the program.Kroll
A
13

Nesting COPYBOOKS in COBOL is a bit of a trick. In general you may nest copybooks only if they do not contain the REPLACING phrase and do not cause recursion.

Suppose you had the following two copybooks:

COPYBOOK ABC

  01 :A:-VAR-A1     PIC X.
  01 :A:-VAR-A2     PIC X.
  COPY XYZ REPLACING ==:A:== BY ==B==.

and

COBPYOOK XYZ

  01 :A:-VAR-X1     PIC X.
  01 :A:-VAR-X2     PIC X.

The nesting in COPYBOOK ABC is not allowed because it contains a REPLACING phrase.

However, you can do the following. Drop RELACING from COPYBOOK ABC so it becomes:

COPYBOOK ABC

  01 :A:-VAR-A1     PIC X.
  01 :A:-VAR-A2     PIC X.
  COPY XYZ.

Now include COPYBOOK ABC into your source program as follows:

  REPLACE ==:A:== BY ==B==.
  COPY ABC.
  REPLACE OFF.

The REPLACE directive causes all occurances of :A: to be replaced by B until a REPLACE OFF directive is encountered, and these replacements occur after all COPY directives have been actioned. The net result of the above statements would be:

  01 B-VAR-A1     PIC X.    <== from ABC
  01 B-VAR-A2     PIC X.    <== from ABC
  01 B-VAR-X1     PIC X.    <== Nested copy of XYZ from ABC
  01 B-VAR-X2     PIC X.    <== Nested copy of XYZ from ABC

This is the only 'legal' way of performing replacements to nested copybooks in COBOL that I am aware of.

Almonry answered 12/9, 2011 at 14:17 Comment(1)
Thanks a lot Neal :) I tried above solution and it is working fine and resolved my problem. Thanks a lot for saving lot of efforts and redundant coding :)Palladium
D
1

While Neal's answer may be correct for the Cobol variant he and the OP are using, it is worth noting that some Cobol compilers will allow nested COPY ... REPLACING statements.

Indeed we have many instances where we use this in our code.

For example, I have many Cobol module programs that include a copymember in the following way:

COPY 'uwxxxxsel.prg' REPLACING
             LEADING "xxxxSEL" BY "AREASEL".

and the 'uwxxxxsel.prg' copymember contains the following COPY of another file as follows:

COPY 'uwf8list.prg'
       REPLACING LEADING 'XXXXXX-F8-' BY 'SELECT-F8-'
                 ==FUNCTION-KEY== BY ==F8-FUNCTION-KEY==.

and this works fine.

I use this as a kind of 'inheritance' structure for common functionality.

There is one gotcha, though, with our Compiler. The higher level REPLACING does not continue after the nested REPLACING, but as long as the nested REPLACING is the last thing in the copymember, it is fine.

Deciduous answered 1/11, 2012 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.