First Edition First
PrintingApril 2000
Every attempt has been made to provide correct information. However, the publisher and the author do not guarantee the accuracy of the book and do not assume responsibility for information included in or omitted from it.
IBM is a registered trademark, and AS/400, OS/400, and 400 are trademarks of International Business Machines Corporation. All other product names are trademarked or copyrighted by their respective manufacturers.
Printed in the United States of America. All rights reserved. No part of this publication may be reproduced in any form without prior permission of the copyright owner.
2000 Midrange Computing
ISBN: 1-58347-012-3
Midrange Computing
5650 El Camino Real, Suite 225
Carlsbad, CA 920089711 USA
www.midrangecomputing.com
For information on translations or book distributors outside the USA or to arrange bulk-purchase discounts for sales promotions, premiums, or fund-raisers, please contact Midrange Computing at the above address.
To DAnna, Andrew, and Kevin McCall.
Thank you for all your support and encouragement.
A CKNOWLEDGMENTS
I would like to express my most sincere appreciation to the following people for their assistance in the creation of this book:
- DAnna McCall
- Chris Peters
- David Morris
- Jim Utsler
- Merrikay Lee
- Steven Bolt
C ONTENTS
I NTRODUCTION
W hile collecting material for the first AS/400 Programmers Handbook, I quickly realized that the sheer volume of information was far more than I could present within a single text. As time passed, I found myself thinking, I wish I could have included database examples like SQL and trigger programs and I wish I had put in more API examples. I finally came to the realization that a sequel was in order.
When developing applications, I find myself drawing from a collection of coding examples as a starting point or template. Rarely will I begin writing a new application from scratch without having a previously written source member from which to draw on. This book is a collection of more than 70 prototypical techniques and coding examples that you can use and adapt for hundreds of potential applications. The material presented here encompasses the most useful and powerful features of the AS/400, and each example is backed by a thorough explanation of the techniques employed.
Each example begins with a quick synopsis that is followed by the supporting source code. This structure allows you to quickly get in, find what you need, and use the example code. If you need more information, detailed explanations of the employed technique are included.
Every effort has been made to make each example stand on its own. This makes some of the information redundant, but the last thing you want to do when you need an example is to have to read other chapters to build an understanding of the topic. Whenever possible, every example is designed to work as a standalone instrument to allow you get in, get what you need, and get back to work.
I hope you will find this book to be a useful and valuable programming tool.
Part 1
RPG Programming Examples
T he vast majority of AS/400 application software continues to be written in RPG. However, the RPG of today is barely recognizable from that used several years ago. RPG has evolved from a language encumbered by its rigid structure to a far more dynamic and flexible tool. The advent of ILE RPG, sometimes called RPG IV, has breathed new life into a tired language.
ILE RPG allows you to do things you simply cant do with RPG/400. For this reason, all of the RPG examples presented here use ILE RPG. The contents of this section include:
1
M ODULAR C ODING T ECHNIQUES
I n programming, much of what is done is redundant, a fact that seasoned programmers who have been in the trenches for many years are well aware of. By the time a programmer has been on the job for more than a couple of years, he or she probably does more copying and adapting of code that already exists than writing new code from scratch. It is simply more efficient to leverage work that has already been done than to start from scratch every time. This technique of reusing and reapplying work that has already been done can lead to tremendous gains in productivity.
Another major benefit of reusing software components is reliability. Modular components are easier to test more thoroughly than full applications. Once tested and verified to work correctly, a component can be reused without being fully retested.
When thinking about designing code that can be used and reused throughout an application, one must maintain a balance between achieving modularity and maintaining acceptable performance levels.
C OMMON M ODULAR RPG C ODING T ECHNIQUES
The introduction of the Integrated Language Environment (ILE) for RPG empowered the language with the tools necessary for building reusable component-based software. ILE allows you to call on services from external providers without suffering from the poor performance that plagues dynamic program calls. Prior to ILE, the ability to build component-based software relied strictly on dynamic program calls or methods of sharing source code.
This chapter presents examples based on a variety of modular or component-based design techniques. The following is a list of todays most commonly used techniques to reuse software in RPG on the AS/400:
- /C OPY compiler directives
- Internal subroutines
- External subprograms
- ILE subprocedures
- ILE service programs
E XAMPLE
Using the /C OPY Compiler Directive
This example employs the /COPY compiler directive to copy source code from a central repository, often called a copybook, into a desired program. This technique of reusing source code is one of the oldest available to AS/400 programmers. While this technique has become outdated for many applications, copybooks remain an effective tool for storing ILE Procedure Interface definitions. The example takes a common generic programming task, determining the day of week for a given date, and deposits the code into a copybook, where it can be used by multiple programmers in multiple programs.
W HAT THE E XAMPLE D OES
The example consists of three source members. Members MOD002R and MOD003R contain copybook source that determines the day of week for a given date. Member MOD001R employs the copybooks using the /COPY directive. The mainline program logic in MOD001R accepts a date as an input parameter, then uses the copied source to determine the dates show the copybook source members.
Listing 1.1: ILE RPG member MOD001R uses /COPY compiler directives.
Listing 1.2: ILE RPG member MOD002R D-spec copybook.
Next page