21
Tue, May
3 New Articles

RPG Academy: Binding It All Together

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

The previous TechTip of this series explains the module, program, and service program concepts. It's now time to learn how to create these objects and to find out a bit more about binding directories.

 

Creating an OPM RPG program is an easy, one-step operation: Just type "14" in the respective source member line of the Work with Members Using PDM screen and you're done. Creating an RPG ILE program requires a few more steps, but it doesn't have to be hard. It's just an operation with a few more steps.

 

Instead of dabbling in theory, let me bring back the service program scenario shown in Figure 3 of the previous TechTip to guide you through the whole process:

 

022814RafaelFigure1

Figure 1: This is the module, program, and service program scenario.

 

It's important to mention a few basic rules in order to avoid wasting time with strange compilation and run-time errors:

  1. Always compile all the modules first.
  2. Compile the service programs next.
  3. If you have service programs that use other service programs, start with the ones that don't.
  4. Then, and only then, compile the programs, starting with those that don't use other programs.

 

 Actually, if you have a program that is called by more than one other program, consider transforming it into a service program. This will avoid cascade recompilations whenever the program is changed and recompiled.

 

The first step is compiling the modules. The command is CRTRPGMOD, or option 15 in the Work with Members Using PDM screen. I won't explain all the parameters (that's what IBM manuals are for), but I'll mention two that I consider important: DBGVIEW(*SOURCE) allows you to view the source code when debugging the service program or program to which the module will be bound. I'll discuss the debugger in a future TechTip, but for now just know that the "new" STRDBG command is much powerful and programmer-friendly than STRISDB! The other option, which only makes sense to use if the source isn't written in free-format, is INDENT(|); this option indents the code using the character you specify, "|" in this case. I like to use this character because it forms a nice continuous vertical line throughout the source code, but feel free to use whatever you think is best. So, if I use this command to create module M1, this is what I should write:

 

CRTRPGMOD MODULE(MYLIB/M1) DBGVIEW(*SOURCE) INDENT(|)

 

Then I'd just repeat the same operation for all four modules. This was easy, right?

 

Creating a service program requires a bit more effort and an explanation about binding. I've mentioned before that you should start the service program compilation step with those service programs that don't use other service programs. Let me explain this a little bit better: When the service programs are being created, the compiler needs to locate the origin of all the procedures and functions that the modules composing that service program use. These procedures and functions can be in the module itself, in other modules of the service program, or in modules of other service programs. It's this last possibility that requires us to use binding directories. A binding directory is a list of modules and/or service programs in which the compiler will look for the procedures and functions that are mentioned in the module(s) of the service program that is being compiled. Whenever a module calls a procedure, that's called an import; whenever a module makes a procedure available outside itself, that's called an export. What happens at compilation time is that the compiler is going to use the binding directory that you specify to locate the exports that match the service program's imports. Since my example service program doesn't use any other service programs, I'll come back to binding directories, imports, and exports when I discuss the compilation of the programs A and B. However, it's a good practice to always create a binding directory to all your programs and service programs, even though it might be empty in some situations, like the one of service program C! It's a way to standardize compilation commands and avoid forgetting the binding directory all together.

 

OK, having said that, it's time to compile service program C. The command is CRTSRVPGM and the most important parameters, in my opinion, are the following:

  • EXPORT(*ALL)The *ALL option will export all the variable and procedure names of the service program. This way, each module that is part of the service program will export all its variables and procedures. However, if you want to control what is exported by the service program, the other option for this parameter allows you to specify a source member that contains the list of exports for this service program. This may have some advantages, which I'll discuss when I talk about signatures in a future TechTip.
  • BNDDIR(*LIBL/<Binding Directory Name>)I'd say this is self-explanatory. Let me just mention that is possible, although I wouldn't advise it, to specify more than one binding directory. Even though it's possible to create generic binding directories and reuse them, it's a good practice to create a binding directory for each program or service program. Generic binding directories tend to be too big, and that slows down the compilation. It also makes the programmer's life harder when he/she needs to figure out where a certain variable or procedure comes from.
  • OPTION(*DUPVAR *DUPPROC *RSLVREF)The first two options allow the co-existence of variables and procedures with the same name within the service program, respectively. Ideally, you shouldn't have duplicate procedures or variables in the modules that compose the service program. However, think of this as a temporary measure, until you get the hang of all this new stuff. Later, I advise you to drop the *DUPPROC and clean up your code. Never mind the duplicate variables, as long as you have it under control. You can always use debug to figure out what's going on. The third one forces the compiler to resolve all the references for the compilation to be successful. In other words, all the imports must have a matching export.

 

The complete command to compile service program C would be the following:

 

CRTSRVPGM SRVPGM(MYLIB/C) MODULE(MYLIB/M3 MYLIB/M4) EXPORT(*ALL) BNDDIR(*LIBL/C) OPTION(*DUPVAR *DUPPROC *RSLVREF)

 

Note that I'm specifying M3 and M4 in the MODULE parameter. If there's only one module and it has the same name as the service program, I could leave the default value *SRVPGM for the MODULE parameter.

 

Finally, it's time to compile the programs A and B. Remember that these programs use procedures that are in modules M3 and M4, which belong to service program C. This means that I need to tell the compiler where to find the "Business Rules Validations" and "Database Operations" procedures and functions that M1 and M2 call. The way to do it is to create a binding directory for each of the programs that includes service program C. Let's do it for program A. Just type CRTBNDDIR BNDDIR(MYLIB/A) TEXT(<same description as PGM A>) and you're done. There's a command to add a binding directory entry (ADDBNDDIRE), but it's easier to use WRKBNDDIR BNDDIR(MYLIB/A), followed by option 9 (note that is not 12, as you might expect from PDM's standard) to work with the binding directory entries and finally option 1 to add the service program C:

 

022814RafaelFigure2

Figure 2: Add a binding directory entry.

 

I just need to repeat the same process for program B's binding directory, and I can now create the programs. For that I'll use the CRTPGM command. The complete command for compiling program A is the following:

 

CRTPGM PGM(MYLIB/A) MODULE(MYLIB/M1) BNDDIR(A) OPTION(*DU PVAR *DUPPROC *RSLVREF)

 

Note that the program is called A, but the module's name is M1. This is not the ideal situation, because it might be hard to keep track of what belongs where. Ideally, the program and module names should be the same. The parameters are the same as the service program, so I won't repeat the explanation.

 

If something goes wrong while creating a service program or a program, you won't have the error listing that you are used to with OPM programs. You need to check the job log for the errors that occurred during the creation process. The most common errors are related to unresolved imports, so it's important to always have your binding directories in order before issuing the creation commands.

 

I'll talk about code organization and naming conventions later in this series, but for now let me just stress something I've mentioned above: it's really, really important to keep your service programs and programs as small as possible. This means using only one or two modules in each. Try to stick to one module in programs, preferably using the same name for both module and program.

 

I'll end this TechTip with a Pro Tip: if you're a big fan of option 14 (who isn't?), know that you can create PDM shortcuts for the module, service program, and program creation commands. Just press F16 in the Work with Members Using PDM screen and create the following user-defined options:

  • Build Module (BM)CRTRPGMOD MODULE(&L/&N) DBGVIEW(*SOURCE) INDENT(|)
  • Build Service Program (BS)CRTSRVPGM SRVPGM(&L/&N) EXPORT(*ALL) BNDDIR(&N) OPTION(*DUPVAR *DUPPROC *RSLVREF)
  • Build Program (BP)CRTPGM PGM(&L/&N) BNDDIR(&N) OPTION(*DUPVAR *DUPPROC *RSLVREF)

 

The next RPG Academy TechTip will be about procedures: how, when, and why to create them.

Rafael Victoria-Pereira

Rafael Victória-Pereira has more than 20 years of IBM i experience as a programmer, analyst, and manager. Over that period, he has been an active voice in the IBM i community, encouraging and helping programmers transition to ILE and free-format RPG. Rafael has written more than 100 technical articles about topics ranging from interfaces (the topic for his first book, Flexible Input, Dazzling Output with IBM i) to modern RPG and SQL in his popular RPG Academy and SQL 101 series on mcpressonline.com and in his books Evolve Your RPG Coding and SQL for IBM i: A Database Modernization Guide. Rafael writes in an easy-to-read, practical style that is highly popular with his audience of IBM technology professionals.

Rafael is the Deputy IT Director - Infrastructures and Services at the Luis Simões Group in Portugal. His areas of expertise include programming in the IBM i native languages (RPG, CL, and DB2 SQL) and in "modern" programming languages, such as Java, C#, and Python, as well as project management and consultancy.


MC Press books written by Rafael Victória-Pereira available now on the MC Press Bookstore.

Evolve Your RPG Coding: Move from OPM to ILE...and Beyond Evolve Your RPG Coding: Move from OPM to ILE...and Beyond
Transition to modern RPG programming with this step-by-step guide through ILE and free-format RPG, SQL, and modernization techniques.
List Price $79.95

Now On Sale

Flexible Input, Dazzling Output with IBM i Flexible Input, Dazzling Output with IBM i
Uncover easier, more flexible ways to get data into your system, plus some methods for exporting and presenting the vital business data it contains.
List Price $79.95

Now On Sale

SQL for IBM i: A Database Modernization Guide SQL for IBM i: A Database Modernization Guide
Learn how to use SQL’s capabilities to modernize and enhance your IBM i database.
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: