.st0{fill:#FFFFFF;}

What is an RPG Copybook? 

 October 10, 2008

By  NickLitten

A copybook is a technique that us RPG PROGRAMMERS use to suck in piece of re-useable code without having to type it every time into lots of programs.

A typical use of a copybook might be something like this scenario:

  • Every program within an application uses the *LDA (Local Data Area) to store specific pieces of information.
  • Every program expects the *LDA to be defined in the same way.
  • Defining the *LDA takes 30 lines of code, which would be duplicated through the entire application in every single program source code.
  • We define these 30 lines in a COPYBOOK member — think of this as a ‘chapter’ of a book.
  • Then we add a simple ‘import chapter 7 of the copybook’ into every program — so one line of code in every program that imports the same 30 lines from the copybook. This import takes place at compile time.

Declaring a copybook means declaring its location to the program that will be importing the code from the copybook.

Why do we use copybooks?

Using our example of the *LDA – One huge advantage is that if we want to change the layout of our *LDA (perhaps to add a new field) then we can simply make the code change to the copybook member. Then as we recompile the programs that import it, they will pull in the new version and compile with it. The obvious advantage is that we would have to actually make any code changes to any of the programs that use the copybook, we just simply recompile them.

To declare a copybook we use the compiler directive /COPY or /INCLUDE. After this we write the name of the copybook.

D* This is a copy book importing RPG code into our program
 /COPY lib/file,member

The compiler directive begins at the 7th position — this means the slash ‘/’ is on the character after the specification character (F/D/C/O etc)

We simply specify the name of the copybook. In the above example I have qualified the library name but we could always just use the library list to find and select the copybook: This is the preferred technique as used in most change management environments

D* This is a copy book importing RPG code into our program from somewhere in the
 /COPY file,member

Remember that if we are using *LIBL then the first copybook FILE will be found in the *LIBL and it will attempt to pull the member source into it from there.

Summary:

1. Declaring a copybook means telling the compiler its location.
2. On AS400, iSeries and IBM i computers we require three things to uniquely identify a source code. Library, Source Physical File’s Name and Member Name
3. Library: The first occurrence in the library list.
4. Source Physical File’s Name: It’s declared immediately after the compiler directive /COPY with a blank space.
5. Member: This is the actual copybook we are interested in. It’s declared after specifying the name of source PF with a comma. Refer the example above for clarification.

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

Iā€™m always looking for awesome input, feedback and critique!

>