Getting Started With RPG
Getting Started With RPG
Started With
Agenda
• There isn’t enough time to teach a
whole programming language in this
session!
• But we can look at:
• Basic examples of the syntax
• How it makes your life easier
• What makes RPG a great business language
• A few powerful features of RPG
What is
• A business-oriented programming language
• Makes it very easy to work with databases, write business rules, work with
screens, and print reports.
• It does not stand for Report Program Generator.
• It used to -- however... In 1994 IBM changed it to officially not stand for anything!
• Even amongst season professionals, there is a lot of confusion about the
name, the "versions", and languages released.
• Versions are backward compatible, allowing code to move forward to new
versions when they are available.
• Very much under development, new enhancements released every spring
and fall. More than 30 major enhancements in the past 5 years.
Languages
Language Introduc Stopped
ed Enhancing/Supporting
FARGO 1959 IBM 1401 1960 / 1971
(Fourteen-O-One Automatic
Report Generation Operation)
RPG II 1968 IBM System/3, 34, 36, Mainframes, Others, 1988 / still supported
AS/400 with System/36 Environment
RPG III 1978 IBM System/38, AS/400, Windows, Others 1993 / still supported
RPG IV 1994 Windows, IBM i, Others still enhanced / supported
NOTE: The numbers I, II, III, IV are not properly known as "versions" – but different
languages. Each of the above has many different versions available.
Compilers
Compiler Language Nicknames
System/36 Compatible RPG II RPG II RPG/36
System/38 Compatible RPG III RPG III RPG/38
RPG/400 RPG III
Visual RPG (discontinued) RPG III VRPG
VisualAge for RPG (discontinued) RPG IV VARPG
ILE RPG for IBM i (formerly ILE RPG/400) RPG IV RPGLE, RPGILE, RPG/FREE
NOTE: Versions of the ILE RPG compiler that are still supported are 7.2, 7.3, 7.4
and 7.5. (Sometimes called V7R2, V7R3, etc)
Fixed vs Free-Format
C *INLR DOUEQ *ON dou *inlr = *on;
C ENDDO enddo;
It surprises a lot of people who are new to RPG that these aren't different languages,
or even different versions of RPG. They are both compiled with the ILE RPG compiler,
both compatible with version 7.5, and both considered RPG IV!
Recommendation: Don't Bother Learning Fixed!
dou *inlr = *on;
enddo;
Hello World
snd-msg just writes an informational
snd-msg 'Hello World';
message.
*inlr = *on;
*inlr = *on ends the program.
Sometimes you declare data structures, prototypes, procedure interfaces, and other things.
• DCL-S = declare standalone
• DCL-C = declare constant
• DCL-DS = declare data structure
• DCL-F = declare file (database table, screen, printer, tape)
• DCL-PR = prototype (for calling other routines)
• DCL-PI = procedure interface (parameter interface to a routine)
• DCL-PROC = procedure/function
Declaring Variables
DCL-S <variable name> <data type> (length) keywords;
To prove my point, here are examples in both C and Java. (Other languages
are similar – even RPG would do the same if you forced it to use floating
point math)
Loops
**Free DOW = Do While
I'm just scratching the surface of what the different types of loops can do –
just to give you a feel for it.
There are many, many, many more options available!
Sub-Procedures ("functions")
name = MyProcedure(last: first);
snd-msg name; You can write your own functions and make
them available within the current program.
. . . other stuff could be here . . . Or export them to make them available to
other programs as well.
dcl-proc MyProcedure; RPG calls them "sub-procedures".
dcl-pi *n;
last varchar(15) const; Notice that RPG's built-in functions always
begin with a % character. The functions you
first varchar(15) const; write cannot begin with that character.
end-pi;
This makes it easy to distinguish the origin of a
function, and also makes it easy to avoid
dcl-s fullname varchar(30); naming clashes.
end-proc;
Integrated Database
DCL-F (declare file)
dcl-f CUSTFILE disk keyed;
• Declares a database table
CUSTNO = 1500; • keyed = Allows keyed (indexed) access.
chain CUSTNO CUSTFILE; • Automatically declares variables for all of
the columns ("fields") in the table ("file").
// the CUSTFILE database table contains columns named • CHAIN = loads a record by it's key.
// CUSTNO, NAME, CONTACT, STREET, CITY, STATE, POSTAL
// and BALANCE -- all are ready to use!
// connect to database
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Or, in RPG:
dcl-f CUSTFILE disk keyed;
The bulk of work in a business application is
CUSTNO = 1500; calculations and database:
chain CUSTNO CUSTFILE;
• True decimal arithmetic
if %found; • Easier database operations
// do something with the column values
endif;
NOT FAIR! Not an Apples-To-Apples Comparison
dcl-ds CUSTFILE ext end-ds;
dcl-s output varchar(500);
Easy Dates/Times
Just a quick example.
LeadTime = 14;
DeliveryDate = %date() + %days(LeadTime);
*inlr = *on;
Screens Are Easy
For basic text screens
• IBM provides an easy-to-use WYSIWYG
screen editor, where you can build your
screens using drag/drop, etc.
• Then you refer to these screens as "files" in
your program.
if EXIT or CANCEL;
return QUIT;
endif;
if DSP1.INVNO <= 0;
DSP1.MSG = 'Please enter an invoice number!';
iter;
endif;
enddo;
Reports Are Easy Very much like screens, IBM provides a GUI
tool to create reports.
You then refer to the report as a file in your
RPG code.
In this case, I laid out the variable data for an
invoice.
write HEADING;
if overflow;
write FOOTER;
write HEADING;
first = *on;
endif;
write DETAIL; The fixed headings and graphical elements of
the invoice were designed in Microsoft Word
reade (myInvNo) INVDETF; – I then told it to generate an "overlay".
enddo;
The overlay gets merged with the data from
write FOOTER; the printer file to make this invoice.
Easy REST APIs
To write a quick & dirty REST API, all you need
Ctl-Opt DFTACTGRP(*NO) ACTGRP('WEBAPI') PGMINFO(*PCML:*MODULE); to do is write a program that gets it's
Dcl-F CUSTFILE Usage(*Input) Keyed PREFIX('CUST.');
input/output through parameters.
31
*inlr = *on;
32
Handling XML or JSON Yourself
If you need to process XML or JSON from a
Ctl-Opt OPTION(*SRCSTMT: *NODEBUGIO) DFTACTGRP(*NO); file, parameter, or other means (aside from
the Integrated Web Services) or if you want to
Dcl-F CUSTFILE Usage(*Input) Keyed prefix('CUST.');
write the API yourself without a special tool,
dcl-ds CUST ext extname('CUSTFILE') qualified end-ds;
it's relatively easy to do.
Dcl-PR getenv Pointer extproc('getenv');
var Pointer value options(*string);
End-PR; RPG provides a built-in way to map XML or
JSON to an RPG variable called DATA-INTO. (or
dcl-s custno like(CUST.custno); the older XML-INTO.)
Dcl-S pos int(10);
Dcl-S uri varchar(1000);
Dcl-S json varchar(1000); Likewise, it can generate XML or JSON using
Dcl-C ID1 '/cust/'; another tool called DATA-GEN.
Dcl-C ID2 '/custinfo/';
33
34
Rich Community of Developers and Tools
Although this session focuses on what's in RPG itself, it's worth mentioning the huge
community of developers and tools out there:
• Vendors provide tools that make a lot of this stuff even easier... I can't list them all, but
some of my own are:
• MDCMS = Change Management / devops / agile
• MDREST4i = Easier APIs
• PROFOUND UI (previous job) make GUI web-based screens just as easily as text based ones.
• Many other vendors offer these types of things – lots of good choices available!
35
Recommended Resources:
• Programming in ILE RPG (5th Edition) by Jim Buck & Bryan Meyers
(in-depth book with exercises, 664 pages)
https://www.amazon.com/Programming-ILE-RPG-Jim-Buck/dp/1583473793
• imPOWER Technologies
(instructor-led online courses)
https://impowertechnologies.com
Questions?
http://www.scottklement.com/presentations/