Many a times we want to conditionally copy some RPGLE script. For this purpose the copybook requires /if, /define, /undefine and /EndIf compiler directives. To use a copybook conditionally we need to follow the following two steps.
Conditionally copy a source code.
- Surround the script you want to be conditionally copied by the /If Defined xxx and /Endif compiler directives.
- When you intend to copy this script in your main RPGLE source, surround your /COPY compiler directive by /Define xxx and /Undefine xxx.
That's it. The following example will clarify the things further.
Example of conditional copying.
The copybook:
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords
D W@Var1 S 10P 0 Inz
/If Defined(Export)
D Export
/Else
D Import
/EndIf
If you see the error "The keyword is not allowed following keyword EXPORT; keyword is ignored." Please ignore it, because this is just a copybook not a compilable source.
Notice the syntax of the Defined(Export).
Now we will declare the copybook in between definitions of Export.
The following code will Export the variable
.. 1 ...+... 2 ...+.
/Define Export
/Copy QCPYLESRC,QBC
/Undefine Export
And the following code will import the variable
.. 1 ...+... 2 ...+.
/Define Import
/Copy QCPYLESRC,QBC
/Undefine Import
Note: To import the variable it is not required Define import. It's just to clarify the things.
Recommendation: Duplicate definition error is very common when using conditional copybooks, ensure that EVERY LINE OF CODE IS WRITTEN IN SOME CONDITION IN COPYBOOK. Also, avoid using /ELSE directive when using large copybooks or you include copybooks several times in a source code.
That's it with conditional copybook. In the next section we will see some common examples of copybooks.