20
Mon, May
7 New Articles

What's New with RPG IV?

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

My kids and I enjoy reading fairy tales. Together, we’ve seen an ugly duckling become a swan, a beast become a handsome prince, and a wooden puppet become a boy.

These stories are not unlike the transformation I’ve seen in RPG since I wrote my first program on a System/3 Model 12 so many years ago. The RPG we use today is much more powerful and elegant than what I used back then.

With V4R2, IBM continues this transformation, with more enhancements to the RPG IV compiler. I think we’re going to like some of these a lot!

Figure 1 contains a short example program I threw together to illustrate some of these changes. I’ll be referring to it throughout this article.

File Operations

Let me start by introducing the new built-in functions that help with file I/O. They are %EOF, %FOUND, %EQUAL, and %OPEN. These new built-in functions will reduce the number of indicators we have to use in our programs by providing an alternative to specifying the traditional resulting indicators for input/output operations. The %EOF function returns *ON if the specified file is at end of file for the READ, READC, or READE operations (beginning of file for READP or READPE operations), or *OFF otherwise. You can also use %EOF to test for failed write (WRITE) operations to a subfile.

Look at Label A in Figure 1. Notice the READ has no resulting indicator. It doesn’t need one because of the %EOF function in the next line.

For CHAIN, SETLL, SETGT, and DELETE operations, the %FOUND function accomplishes the same sort of thing. It returns *ON if the desired record was found (e.g., the CHAIN succeeded), or *OFF if not. You can see an example of this at Label B.

If you prefer, you can use the %EQUAL function instead of %FOUND on SETLL operations. (%EQUAL also works for the LOOKUP table or array operation.)

To test whether or not a file is open, use the %OPEN function. Like the other functions we’ve already seen, this returns a value of *ON (open) or *OFF (closed). At Label C, the program in Figure 1 uses this function to decide whether or not it needs to open the error report file. If no errors are found while the program runs, the file never gets opened. The program also uses %OPEN at Label E to ensure it doesn’t try to print an error message if the error report file open fails.

Error Handling

There are also two new functions to help with error handling—%ERROR and %STATUS. To use them, you must use the new E op code extender on the operation that might end in error. You omit the resulting indicator in the “low” position.

The %ERROR function accepts no parameters, and returns *ON if the last executed operation with an E extender ended in error. Look at Label D. The E operation extender keeps the program from canceling if the open fails. The %ERROR function on the following line will prove true (*ON) if the open fails, and the abend routine will execute.

The %STATUS function may take a parameter, but it doesn’t have to. Without a parameter, %STATUS returns a program or file status code, whichever changed most recently. You can get the most recent status code for a certain file by specifying the file name in the parameter.

Better Access to Logical Data

V4R2 adds support for logical (a.k.a. Boolean) variables to RPG, but they’re not

called logical variables. Instead, they’re called indicator variables. You define indicator variables on the D-specs, specifying no length, and a data type of N in position 40. Indicator variables can have values of *ON and *OFF.

In Figure 1, variables GoodCustomer and PostToGL are indicator variables. If you’ll look through the calculations, you’ll see two places where GoodCustomer is set on or off and one place where it is tested with an IF. I could have compared GoodCustomer to *ON, but that’s not necessary.

IBM also added RPG support for logical data areas. The PostToGL indicator variable gets its value from the logical data area GLACTIVE, found somewhere in the library list.

Named Indicators for Device Files

You can now use your own data structure to define the indicators used by workstation and printer files that specify the INDARA keyword. Such a data structure must be 99 bytes long, and all subfields must be of the indicator data type.

This means your RPG programs do not have to refer to the indicators in device files by number. Instead, you can assign names to the indicators and refer to them by name.

The few display file specs shown in Figure 2 define indicators 03, 12, and 20 through 23. In Figure 3, data structure Conditions maps these indicators to indicator variables so that you can refer to SflClr instead of *IN22.

New Character Manipulation Abilities

The character variables we’ve been using all these years are fixed-length. V4R2 adds support for variable-length character and graphic variables.

Variable-length character variables are declared with a length from 1 to 32,767 and a data type of A, just like fixed-length character variables. To make the variable variable- length, add the keyword VARYING. RPG makes the variable two bytes larger than its declared length. The first two bytes contain the significant length (the position of the last nonblank character in the field) of the data in binary format.

You can use the %LEN (length) function to set or retrieve the current length of a variable-length character variable.

Don’t confuse these variable-length character variables with null-terminated strings like those used in the C language. They are not the same things.

The %REPLACE function is new. It allows you to replace a substring of a character or graphic variable with another substring. The two substrings don’t have to be the same length, nor do they have to be the same format (one can be fixed-length, the other variable-length), but they do have to be the same data type.

%REPLACE has four parameters. The first two are required; the last two are not. The first parameter is the string to be inserted into the character variable. The second parameter is the character variable containing the data to be replaced. The third parameter is the starting position of modification. The default value is 1, which means the replacement data will be placed at position 1 of the character variable. The fourth parameter is the number of characters being replaced. It defaults to the length of the first parameter.

Assume the program in Figure 1 retrieves a customer master record in which the customer name is “Jack Sprat & Sons, Inc.” The %SCAN function finds a blank, an ampersand, and another blank beginning at position 12 of the customer name. The %REPLACE operation replaces & with and and stores “Jack Sprat and Sons, Inc.” in CustName.

%CHAR converts graphic, time stamp, date, or time data to character format. Time stamp, date, and time values will be in edited format.

Changes to the H-Spec

You may find yourself using control (H) specs more once you move to V4R2 because IBM has added the ability to specify compiler options there. The CRTBNDRPG and CRTRPGMOD commands will read these compiler options.

In Figure 1, I’ve included three compiler options: DftActGrp(*no), ActGrp(*new), and Indent(‘..’) in the control spec. Figure 4 contains a list of the compiler options allowed in H-specs.

This is exciting to me. It means I no longer have to put compiler options in comments and hope that whoever compiles my code follows my instructions. What a load off a programmer’s mind! Maybe IBM should add control specs to DDS and CL.

There’s another enhancement that people who run their applications internationally will like. The control specs now allow you to dynamically set the DECEDIT value at runtime. If you specify DECEDIT(*JOBRUN) in the H-spec, the program will use the decimal editing character of the job.

Miscellaneous Changes and Enhancements

Here are other ways V4R2 RPG IV differs from previous compilers:
• The %EQUAL and %FOUND functions can be used with table and array LOOKUPs to indicate that an exact match was found.

• The %FOUND function can indicate a match from a CHECK, CHECKR, or SCAN operation.

• RPG programs can now import and export procedures and variables with mixed- case names. To do so, add a parameter to the IMPORT or EXPORT keywords in the D- specs (not the EXPORT keyword in the P-specs). The parameter must be a literal or a constant.

• Date formats *CMDY (cmmddyy), *CDMY (cddmmyy), and *LONGJUL (yyyyddd) are now supported.

• Values 0 (19xx) through 9 (28xx) are now supported for century digits in dates.

• The first parameter of the OVERLAY keyword in D-specs may now be the name of the data structure itself.

• CRTRPGMOD now has a BNDDIR parameter, allowing you to specify a binding directory.

• CRTBNDRPG and CRTRPGMOD have a new PRFDTA (profile data) parameter to support application profiling.

• The FIXNBR parameter of the CRTBNDRPG and CRTRPGMOD commands now accepts the value *INPUTPACKED, meaning that the program should handle invalid packed data from input files.

• You can place an N in position 36 of an input spec or position 52 of an output spec to define an indicator variable.

• You can also specify a variable-length field by placing the value *VAR in positions 31 through 34 of an input spec or 53 through 80 of an output spec.

• RPG now defines a new status code value, 115, for errors with variable-length

The Tale Continues…

So that’s what’s new with RPG. Stay tuned to these pages. I have a feeling I’ll be writing a sequel to this tale in the not-too-distant future.

In case it hasn’t hit you, it is now possible to write an RPG program with no indicators. RPG has come a long way. Whether or not you think it’s become a swan, maybe you can at least agree that RPG is not the ugly duckling it once was.

fields.

Reference

ILE RPG/400 Reference (SC09-2508, CD-ROM QB3AGZ00)

H DftActGrp (*no) ActGrp(*new) Indent(‘..’)

FXacts if e k disk

FCustMas if e k disk

FsomeRpt o e printer

FErrorRpt o e printer usropn

D GoodCustomer s N

D PostToGL s N dtaara(GLACTIVE)

D ScanX s 5I 0

D CustName s 35 varying

C in PostToGL

C unlock PostToGL

C
BEGIN LABEL A

C read XactsR

C dow not %eof( Xacts)
END LABEL A

C

C eval % len( CustName) 0
BEGIN LABEL B

C CusNo chain CustMasR

C if %found( CustMas)
END LABEL B

C eval GoodCustomer = *on

C eval ScanX = %scan(‘ & ‘: CusNm)

C if ScanX > *zero

C eval CustName =

C %replace(‘ and ‘: CusNm: ScanX: 3)

C endif

C if PostToGL

... do whatever you do if GL is interfacing to this application

C endif

... do some more stuff

C else

C eval GoodCustomer = *off

C exsr ErrorRoutine

C endif

C

C if GoodCustomer

... do whatever you do for valid customer numbers

C else

... do whatever you do for invalid customer numbers

C endif

C

C write RptLine

C

C read XactsR

C enddo

C

C eval * inLR = *on

C*****

C ErrorRoutine begsr

C
BEGIN LABEL C

C if not %open(ErrorRpt)
END LABEL C

BEGIN LABEL D

C open (e) ErrorRpt
END LABEL D

C if %error

... do an abend routine

C endif

C endif

C
BEGIN LABEL E

C if %open( ErrorRpt)
END LABEL E

C write ErrorLine

C endif

C

C endsr A INDARA

... more specs

A R SFLCTL SFLCTL(SFL)

A CF03(03 ‘Exit’)

A CF12(12 ‘Previous screen’)

A 21 SFLDSP

A 20 SFLDSPCTL

A 22 SFLCLR

A 23 SFLEND(*MORE)

A SFLSIZ(0011)

A SFLPAG(0010)

Figure 2: A fragment of a display file containing conditioning indicators

FCustListDFcf e workstn sfile(sfl: rrn)

F indds(Conditions)

... more F specs

D Conditions ds 99

D EndProgramWasRequested...

D 3 3n

D CancelScreen 12 12n

D SflDspCtl 20 20n

D SflDsp 21 21n

D SflClr 22 22n

D SflEnd 23 23n

... some C specs go here

C

C eval SflClr = *off

C eval SflDspCtl = *on

C eval SflDsp = *on

C eval SflEnd = *on

C exfmt sflctl

C

C if EndProgramWasRequested

Figure 1: This program illustrates some of the new features of RPG IV

Figure 3: This RPG program does not have to refer to device file indicators by number

ACTGRP

ALWNULL

AUT

BNDDIR

CVTOPT

DFTACTGRP

ENBPFRCOL

FIXNBR

GENLVL

INDENT

LANGID

OPTIMIZE

OPTION

PRFDTA

SRTSEQ

TEXT

TRUNCNBR

USRPRF

Figure 4: You can include these compiler options in RPG control specifications

TED HOLT

Ted Holt is IT manager of Manufacturing Systems Development for Day-Brite Capri Omega, a manufacturer of lighting fixtures in Tupelo, Mississippi. He has worked in the information processing industry since 1981 and is the author or co-author of seven books. 


MC Press books written by Ted Holt available now on the MC Press Bookstore.

Complete CL: Fifth Edition Complete CL: Fifth Edition
Become a CL guru and fully leverage the abilities of your system.
List Price $79.95

Now On Sale

Complete CL: Sixth Edition Complete CL: Sixth Edition
Now fully updated! Get the master guide to Control Language programming.
List Price $79.95

Now On Sale

IBM i5/iSeries Primer IBM i5/iSeries Primer
Check out the ultimate resource and “must-have” guide for every professional working with the i5/iSeries.
List Price $99.95

Now On Sale

Qshell for iSeries Qshell for iSeries
Check out this Unix-style shell and utilities command interface for OS/400.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: