What is Copybook in AS/400(RPG)

370 pts.
Tags:
AS 400
RPG/400
Can anyone please explain to me with an example what is copybook in AS/400?
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

Copybook is a bunch of source code statements.
Lets take an example:

I have around 100 programs which are doing addition for two numbers.
Now in this scenario I can write logic of adding two numbers but instead of writing same code in alll programs I can create a member and put the code for adding two numbers into it and just copy that member into the programs where I want to implement this logic.
The source of this copy book can be seen into the spool file of the program in which you ha ve included it.

let me know if you stilll need any more explanation.

===========================================================

A /COPY member is a source member that contains a set of source statements that you expect to use over and over in many programs. Instead of having the same source in each of those programs, you have a single source line with a /COPY directive. The compiler then inserts the statements from the /COPY member in place of the /COPY directive.

If you need to change the statements at a future time, you only change the one source member. When you recompile all of the programs, the new source is automatically “copied” into the source that is compiled.

In earlier versions of languages, you might copy data definitions or standard subroutines. Now, with ILE, /COPY members are best suited for data structures or prototype definitions. Use prototyped procedures instead of subroutines. The only time prototype /COPY members would change would be if you had to change the parameters for the prototype.

Tom

Discuss This Question: 6  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Gilly400
    Hi, You use the compile time directive /COPY to add the source from another source member into your program. These days it would be more advisable to use a module instead of a copybook. Regards, Martin Gilbert.
    23,730 pointsBadges:
    report
  • nehalika
    Can we compile copybook?
    10 pointsBadges:
    report
  • TheRealRaven
    Can we compile copybook ?

    It might be possible, but it makes no sense to do so.
    35,000 pointsBadges:
    report
  • TheRealRaven
    BTW, to create a copybook that can be compiled would take some thought. I don't recall seeing any copybook that could be (successfully) compiled in 40+ years of programming. As a general rule, copybooks are incomplete as modules or programs, and compiles would fail.
    35,000 pointsBadges:
    report
  • GregManzo
    We do it - all it takes is a couple of compiler directives. 
    We keep the prototype definitions in the same source as the actual functions (so it can be maintained in one place) and have a compile parameter in the source to DEFINE(Module), then after all prototype declarations but before any internal code we have:
    /IF NOT DEFINED(Module) 
    /EOF 
    /ENDIF
    This means that when compiling the module we get all the code for the functions, but any program that wants to use the functions can just 
    /COPY Module 
    and get only the prototypes. Tip: If your module includes F-specs or anything that needs to go before the PR definitions, wrapper that in:
    /IF DEFINED(Module) 
    /ENDIF
    As coding standards go, we find this is an easy one to get programmers to stick to (cos it makes their job easier).
    2,960 pointsBadges:
    report
  • TheRealRaven
    @GregManzo: Good point and one that I should have thought of. It's essentially a full *MODULE source member by demand; but the compiler directives turn it logically into merely a "copybook" by default.

    I do the same, and it didn't occur to me to think of the full member as just a "copybook". But in practice, it is.
    35,000 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: