The /COPY directive in RPG III and RPG IV allows you to selectively include source code from outside the current source member. This means you can store commonly used code--such as procedure prototypes, global field declarations, and perhaps even compiler options--in separately maintained source members. How cool is that!
When I've asked my students why they don't use /COPY, two answers represent 99% of their responses: 1) They can't see the source when they edit the main source member, and 2) they simply never started using it.
I think the first response pinpoints what, to me, is the biggest flaw in the way AS/400 RPG programmers write code; they use SEU (edit source). This is perhaps the most significant practice that is keeping RPG programmers a generation behind their PC, Linux, and UNIX counterparts. Stop using SEU!
The second response is more understandable. After all, there are many features in RPG that the general programming community does not use. But /COPY or the ability to include separate source members at compile time has become a fundamental programming practice--so much so that IBM recently enhanced RPG with a second include feature, the /INCLUDE directive.
/COPY and /INCLUDE allow the included source member to contain any regular RPG source code, including another /COPY or /INCLUDE statement. This is known as nested /COPY. /COPY differs from /INCLUDE in that the current SQL preprocessor doesn't know about /INCLUDE, so it lets /INCLUDE pass through the compiler's SQL preprocessor without being processed. That is, unlike /COPY, /INCLUDE statements are not expanded by the SQL preprocessor; rather, they are expanded when the actual RPG compiler is evoked.
Why the difference? The SQL preprocessor, it appears, is aging. It does not recognize contemporary compiler features such as compiler directives and nested /COPY statements. In fact, it fails if a source member being included by /COPY contains a /COPY statement itself. Since the SQL preprocessor doesn't know about /INCLUDE, it allows /INCLUDE to pass through the preprocessor phase with being processed. This allows the RPG compiler itself to process the /INCLUDE member and any of its statements, similar to what happens with the regular /COPY when not using embedded SQL. Aside from the SQL preprocessor issue, the two directives are identical.
Prototypes and /COPYs
One of the biggest advantages of using the /COPY
statement is that it allows you to import prototypes for procedures. Since most
new code written in RPG IV will probably take advantage of procedures and every
procedure requires a prototype, the benefit of /COPY and /INCLUDE should become
obvious.
Let's assume we have several procedures that we've created for
an application. Most of those procedures are written in RPG IV, and some are
written in another language, such as C. It turns out that we want to use some of
these procedures in multiple applications. For example, we have several "date
and time" procedures and perhaps some CGI procedures.
In order to use
these procedures, the prototype for the procedure needs to be included in each
source member that refers to the procedure--that is, in source members where the
procedure is called and in the source member that defines the procedure
itself.
So if we have three source members that contain the procedure
definitions for several procedures, and those source members are named DATERTN,
STRRTN, and SYSTOOLS, we would have something like the following:
Member: DATERTN
|
Member: STRRTN
|
Member: SYSTOOLS
|
GetDay()
GetDayAsString() GetEndOfMonth() |
ToUpper()
ToLower() FindReplace() |
RtvMbrD()
RunCmd() RtvFldList() |
When these source members are compiled, an individual *MODULE object is
created that contains the compiled code for the procedure. We can then bind by
copy (physically bind or copy these modules into our application) or bind by
reference (create a service program containing these modules).
In order
to call and then bind to any of these procedures, we need to include their
prototypes in the source member that contains the call to the procedure. So we
create a second set of source members that contains only the prototypes for the
procedures, and we end up with something like the following:
Member: DATES
|
Member: STRING
|
Member: TOOLS
|
GetDay()
GetDayAsString() GetEndOfMonth() |
ToUpper()
ToLower() FindReplace() |
RtvMbrD()
RunCmd() RtvFldList() |
These source members will be /COPYed into the source member that contains
the call to the procedure. For example, if we need to call the GetEndOfMonth()
procedure, we need to include the prototype for that procedure. We can
physically code the prototype (bad idea!), or we can simply include the DATES
source member by using a /COPY or /INCLUDE directive.
Let's use the
GetEndOfMonth procedure as an example. Figure 1 shows the source for the
GetEndOfMonth procedure:
|
Figure 1: This is the source for the GetEndOfMonth procedure
example.
Lines 2 and 3 of the GetEndOfMonth procedure contain the
procedure interface (the parameter list) for the procedure. The prototype for
this procedure would be as shown in Figure 2:
|
Figure 2: This is the prototype for the GetEndOfMonth procedure
example.
The difference between the procedure interface (lines 2 and
3 of Figure 1) and the prototype (line 1 of Figure 2) is that the letters "PI"
appear in the procedure definition in positions 24 and 25 of line 2 in Figure 1,
whereas the letters "PR" appear in the prototype in the same positions of line 1
in Figure 2. The letters "PR" indicate that the source is a prototype.
A
prototype can be included even if its procedure is not called by the source in
which it is included. Essentially, prototypes simply help the compiler syntax
check the call to the procedure. If the procedure isn't called, its prototype
isn't used, so there's no wasted overhead associated with including procedure
prototypes you don't use.
You could simply hard code the prototype in the
same source member that the procedure is defined in, but then when you need to
call the GetEndOfMonth procedure, you'd have to physically copy the prototype
into each source member that needed it, and what fun is that?
Using the
/COPY or /INCLUDE statements, however, you can simply store the prototype and
other related prototypes in a separate source member and then /COPY it into the
source that needs it.
For example, suppose an application needs to read
a Customer master file and, using a payment due date, calculate a new due date
based on a grace period that extends through the end of the month. Then, a
report needs to be printed with the customer number and the revised due
date.
Figure 3 shows the source for this example application.
|
Figure 3: Here's an example of where to use the /COPY
statement.
The /COPY statement on line 4 of Figure 3 illustrates the
ease of including another source member. That's all there is to it. Nothing else
needs to be done. When the compiler processes the /COPY statement, the source in
Figure 5 is produced.
|
Figure 5: Here, the /COPY source is expanded.
Note that the
prototype (actually, all the source from the included member) is copied into the
main source member exactly where the /COPY statement was specified.
Syntax of /COPY and /INCLUDE
The syntax is the same for both /COPY and /INCLUDE,
and they can include either regular OS/400 source file members or text files
stored in the Integrated File System (IFS). The syntax to include regular OS/400
source members is as follows:
The library name and source file name are optional, but the member name
is required. In addition, the /COPY and /INCLUDE statement must have one and
only one space between the directive itself and the member name. Here are a few
examples:
/COPY qrpglesrc,dates
/COPY dates
All three of these /COPY statements are effectively the same. If the
library name is not specified, the library list is searched. If the source file
name appears in a library name that is high on the library list and it does not
contain the member name, the search continues to the next source file on the
library list and then the next until the member name is located. The library
name (if specified) must be qualified to the source file name, using the forward
slash, as in the traditional libname/srcf syntax. If the source file name is not
specified, QRPGLESRC is used as the default name, and the library name may not
be specified. Any name is valid as long as it is a valid source file object. If
the source file name is specified, it is qualified to the member name using a
comma, as in the srcf,mbrnam syntax. The member name is always required. The
syntax to include source from the IFS is as follows:
/COPY "/cozzi/rpgsrc/dates"
/COPY /cozzi/rpgsrc/dates
/COPY /qsys.lib/payroll.lib/qrpglesrc.file/dates.mbr
When using the IFS file system naming, both the IFS and the so-called
QSYS file system can be included. That is, in addition to text files stored on
the IFS, regular OS/400 source file members may be included using the IFS naming
convention.
The first /COPY includes the text file named DATES.RPG
located in the "/cozzi/rpg source" folder of the IFS. Note the embedded blank.
When the directory includes an embedded blank, the entire directory and file
name must be enclosed in quotes (single or double). When no blanks are embedded,
quotes are optional.
The second /COPY statement also includes the
dates.rpg file, but it avoids specifying the suffix. If no suffix is specified,
the compiler looks for the file without the suffix, and if it does not find it,
it looks for the file with a suffix of .rpg. If it cannot find that, it
looks for the file with a .rpginc suffix.
The third /COPY statement is
the same as the second example, except the quotes are not specified.
The
fourth /COPY statement illustrates how the IFS syntax can be used to include
regular OS/400 source file members. By starting the IFS directory name with
/QSYS.LIB, the so-called "QSYS File system" is searched for the file
name.
Start using /COPY or /INCLUDE today! Your code will be happier, and
it will create less maintenance issues than you currently have by allowing you
to modify prototypes, algorithms, or data variables in one place and then simply
recompile.
LATEST COMMENTS
MC Press Online