I noticed last to last week that every third visitor to this website searches for "copybooks". I am sorry that I could not write about copybooks earlier because of extremely hectic schedule last week. If you search something related to AS400 or RPG ILE and do not find it here, Please ask your question in the forum. Someone will certainly answer your question.
Anyways, Coming to this article, we will learn here everything (Or most of them ;-) about copybooks.
Before we begin here's what I intend to write.
- What is an AS400 copybook?
- Declare copybooks in RPGLE/SQLRPGLE programs?
- Example of a program using copybook.
- How the compiler processes a copybook.
- Using copybooks to define procedure prototypes.
- Conditional Copying from a copybook!
- Copybook related confusing questions.(A must read)
So, Now that you know exactly what the scope of this article is, choose the section you like to read from the links given at the bottom. If you like to begin from the beginning, well, this is the opening page of this article and it answers the first question about the copybook. Which is
What is a Copybook anyway(s?)?
Copybooks are "reusable components", rather "reusable RPG IV scripts" which have been tucked into different members of a source physical file. The main idea behind RPG Copybooks is reusability. You know about reusable components, Right? (If you forget, Do you remember the famous date format conversion program on your system which you call whenever you want to convert a date's format from *YMD to *MDY. Yes! that is a reusable component). In the same fashion as reusable components, copybooks are reusable RPGLE source codes (I do not know much about COBOL copybooks, but I know they do exist but I know RPG only...by now).
How is a copybook reusable?
I will explain this with an example.
You have two source codes, one for program A and another for program B. However, much of the source codes are common! Common? The common source code may be some variable definitions, some subroutine or just any huge chunk of code. Also, you have a hunch that the "commons code" may become again common with some programs you will be coding in near future!
Here, Copybook comes in the picture. What you can do here is that you create a new separate source code just as any other RPG ILE program source and copy it into the source codes of Program A and Program B both! In future if you require the same source (copybook source) in some other Program C, you can easily copy it into it also.
Benefits of RPGLE Copybooks
Several. Few of them
- More structured source codes.
- Easier maintenance. If you need to change something, you have to change it in the copybook and recompile Programs A, B and C...etc. That's all.
- Conditional Variable copying. You can define the exported and imported variables in a copybook under different conditions and include them conditionally in you programs. So, it lets you keep track of the exported and imported variables in easy way.
Now you have at least some idea about copybooks. Haven't you?
Copybooks are not different things. They are just RPGLE sources copied/included in other source codes. Now let's learn how to declare them.