One of the most frequently asked questions I get is how to use the nested /COPY feature in RPG IV. This feature allows you to use /COPY to copy a source member and, in the copied source member, also have another /COPY directive that copies in yet another member.
This capability is supported up to 2,048 levels deep and is controlled with the COPYNEST keyword on the Header specification. The COPYNEST keyword allows you to adjust the default maximum nesting level up or down (the default is 32).
Nested /COPYs are powerful and very useful; however, the popular IBM SQL preprocessor does not support nested /COPY statements. Therefore, many applications and third-party software tools cause the SQL preprocessor to generate an error during the preprocessor "compile" phase. This can create hours, if not days, of headaches and research for those who run into this problem.
To resolve this problem, IBM introduced the /INCLUDE directive. This directive is unknown to the SQL preprocessor. Since the SQL preprocessor was written prior to the introduction of /INCLUDE, this directive simply passes unnoticed through the SQL preprocess phase of the compiler. Here's all you have to do:
Old /COPY style:
New /INCLUDE style:
Aside from SQL's ignoring /INCLUDE, it functions exactly like /COPY. In fact, there's no reason to continue to use /COPY at all unless you need the copied source to be used by the SQL preprocessor. But even in that case, you can embed a nested /INCLUDE in a source member that is /COPYed into another source member. The SQL preprocessor sees and processes the /COPY while it ignores the nested /INCLUDE.
As a side note, both /COPY and /INCLUDE allow you to store and include source code on the IFS and /COPY it into your compiled RPG IV source. Simply specify the IFS file (including the full path name) enclosed in quotes on the /INCLUDE statement as follows:
The quotes are actually double quotes, not the traditional CL-style apostrophes that you're used to using.
Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.