IBM i5/iSeries Primer(c) Concepts and Techniques for Programmers, Administrators, and Sys[... ]ators

It is hard to imagine an IBM midrange computer professional who is not at least acquainted with the language known as Report Program Generator, or RPG. This is because RPG is widespread among all members of the midrange computer family, beginning with IBM's System/3.

Overview

When IBM announced the System/3 in the late 1970s, it announced a second generation of RPG and aptly named it RPG II. This language was further refined with additional improvements when the System/34 was developed. The third generation of RPG came with the System/38 and included some relatively novel concepts, such as the ability to work with a fully integrated database.

The first AS/400 systems to ship were very much like the System/38, but with an improved user interface. The RPG III language that was developed for the System/38 was carried over to the AS/400 with very few changes, except for the name, which became RPG/400.

Even though there were initially few changes to RPG on the AS/400, the language continued to evolve. In 1994, IBM announced the long-awaited fourth generation of RPG and named it ILE RPG, but it is known more widely as RPG IV.

The Integrated Language Environment or ILE was an attempt by IBM to bring a variety of languages and environments together under a single framework. Although the concept was announced with V2R3 in the mid-1990s, the jury is still out on the overall success of the ILE concept. Such is not the case with RPG IV, however, because its success has been widely documented.

One very important aspect of ILE does apply to RPG IV, however: A series of RPG source statements that used to be compiled to make up a program are now compiled to make up a module instead. Modules of one or more languages may then be bound together to make up a program. A single series of RPG source statements may still be compiled to make up an RPG module, which may, in turn, be bound into a program. This methodology makes the ILE concept largely transparent to RPG programmers who were more familiar with the Original Program Model, which was used to describe the earlier version of RPG.

The success of RPG IV may be largely predicated on the many significant enhancements that were made to the language, many of which had existed in other programming languages for many years. Access to new data types, limited free-form expressions, new built-in functions (BIFs), and 10-character file and field names are just a few of the improvements that were made in the fourth generation of the RPG programming language. The 64-bit architecture of the AS/400 RISC systems had made many of the RPG language limitations and size constraints somewhat archaic, so RPG IV eliminated or largely increased many of the language-imposed limitations.

In spite of its humble beginnings (when RPG was used for creating report-type programs), RPG on the i5 is a full-fledged, high-level language that is capable of performing complex database operations, interactive applications that support subfiles, and heavy mass-update programs. Of course, RPG also creates report programs, thereby living up to its name.

The i5 supports five versions of RPG in V5R3. The System/36-compatible RPG II compiler creates native program objects from source code that was developed on or for a System/36 system. An RPG III compiler also is compatible with the AS/400's other predecessor, the System/38. The native RPG III compiler, often referred to as RPG/400, is much like the System/38 compiler, but with additional features. The native ILE RPG compiler, also known as RPG IV or ILE RPG/400, is the only host-based compiler that continues to be enhanced. Support for outdated versions of RPG shows IBM's commitment to customers by ensuring that they can continue to maintain their old but adequate programs without having to modify them.

The fifth version of RPG, Visual Age RPG, is based on RPG IV and runs on PCs. You can use VARPG to develop Windows-based client-server applications that access an i5 server.

IBM publishes several large volumes that explain RPG in great detail. If you need any information not provided in this chapter (which is just a presentation of the language, not a treatise on it), you should refer to IBM's manuals.

RPG Specifications

RPG IV is remarkably different from most other computer languages. While other languages are written on a blank sheet of paper, so to speak, RPG is largely columnar in nature and was originally hand-written onto preprinted forms (called coding sheets) where the programmer filled out the blanks with letters and numbers. The coding sheets were then given to a keypunch operator to prepare a deck of punched cards, ready to be submitted to the System/3 or System/360. Nowadays, no one would think of using coding sheets, but the "columnar" concept remains largely intact. However, exceptions do exist, as you will see.

An RPG module consists of a series of statements that are each generally one physical line long. The purpose of each statement is determined by the specification ID, which is a single letter located in column 6. The sequence of the specifications is important, and some of the specifications are optional. The pertinent specifications are those shown in Table 24.1.

Table 24.1: RPG IV Specifications.

Column 6 Designator

Description

H

Header (or Control) Specification

F

File Description Specification

D

Definition Specification

I

Input Specification

C

Calculation Specification

O

Output Specification

P

Procedure Specification

The Structure of an RPG Module

As with many things in life, there is no single way to describe how you would construct all RPG IV program modules. Different ways are possible to construct the module because there are different reasons that it is required. But, the following model describes the way most of your RPG IV program modules will be constructed:

Tip 

All the preceding specifications are optional. But, when they are used, the specifications must be entered into the source module in the order in which they are listed above (with exception to the procedure boundary specification). If only one specification is out of sequence, the compilation aborts.

Fixed Format

RPG IV is unique because, for the most part, it has a fixed format. You can't code your program statements anywhere you like. The RPG compiler forces you to utilize predefined spaces. If you do not use those spaces, the RPG compiler may not recognize your code as valid, or may misinterpret it.

For example, an F in column 18 of the F-specs means that the file is fullprocedural (an RPG term meaning that the file is manipulated manually using READ and WRITE operations). Shift that F four columns to the right (column 22) and its meaning changes. An F in column 22 means that the file is program described. An F in column 6 identifies the statement as a file specification.

Although it is true that the fixed format presents problems and limitations, it is also true that most business application programs can be coded in fixed format both quickly and efficiently. Efficiency is precisely why RPG has outlasted so many other programming languages through the years. RPG is designed and optimized for business. It was not designed to be all things for all programmers.

RPG Names

All RPG IV names must begin with a letter of the alphabet (A to Z) or the symbols @, #, or $. RPG IV names also can contain the underscore (_) character anywhere except the first position. The remaining characters can be more of the same or any of the 10 digits (0 to 9). All other symbols are invalid. All characters in a name may be written in uppercase, lowercase, or mixed case.

Indicators

RPG indicators are logical variables. An indicator can be either on (true) or off (false). Some indicators are pre-defined. You may define indicators of your own in the definition specifications.

Predefined RPG indicators are two-character codes:

Other indicators have special functions:

The current tendency among programmers is to avoid the use of predefined indicators whenever possible, but using such indicators can be advantageous in some circumstances. For example, you can use indicators to control the attributes of fields in an interactive program's display file.

Tip 

The indiscriminate use of indicators can reduce the readability of your program considerably, because indicators have completely meaningless names. An indicator such as ‘43’ could mean "end of file" or "character scan ended abnormally," just to name two possibilities. Try to reduce indicator usage to only absolutely essential ones. An alternative option would be to code a named indicator variable. A variable named "EndOfFile" would probably make more sense than indicator ‘43’.

The RPG Cycle

RPG provides a built-in processing cycle. Because most business application programs must perform input, calculations, and output, the RPG compiler provides this process automatically in the RPG cycle. This cycle is not unlike the built-in logic of widely used UNIX utilities, such as grep, sed, awk, and Perl.

A file can be designated as the "Primary Input." When you use a primary file, the RPG cycle automatically reads it, performs the calculations coded in the C-specs, and then performs whatever output you indicate (to itself, if the file is being updated, or to a different file such as a printer file).

In addition, the RPG cycle offers other effort-saving features. You can code control-level breaks by using L1–L9 indicators on the I-specs that define certain fields of the primary file. When the system reads a record (automatically, because you are using the cycle), and it detects that those fields have changed, it turns on the indicator. You can use that indicator to command the program to print subtotals or to perform other operations.

Even if you do not use a primary file, the cycle is still active, and you must indicate to the program when to end by turning on the LR indicator.

Категории