23
Sat, Nov
1 New Articles

OOP in CL's Clothing

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

What if CL were an object-oriented programming (OOP) language? How would it differ from its present form? My purpose in this article is not to say that CL should be an OOP language. Instead, I explain the basic difference between procedural programming and OOP for those people who have spent years in the former. In doing so, I use CL as a common reference point to which everyone can relate.

I know that procedural programmers are often bewildered when they try to understand OOP. Because its many terms, such as polymorphism, encapsulation, instantiation, and superclass, give the impression that OOP is beyond mere mortals, I avoid all these terms. I hope this approach helps readers grasp the concepts of OOP. After you have an understanding of the concepts, the jargon should come to you more easily.

Object-based and Object-oriented: Common Ground

The AS/400 is an object-based computer. Although object-based and object-oriented are not the same, they are similar. In both cases, each type of object has certain operations that can be performed on it.

For example, one type of object is the data area. CL uses commands like Create Data Area (CRTDTAARA), Delete Data Area (DLTDTAARA), and Change Data Area (CHGDTAARA). Look at the DLTDTAARA command in Figure 1. It deletes a data area called REVISION in a library named ATLANTA. DLTDTAARA has already been designed and coded by people at IBM Rochester. For that command to work properly, you must give it some information (in this case, the name of a data area). Therefore, DLTDTAARA exists separately from the data area objects on the system. When you want to delete a data area, you must think about the command you need to use and then move your thoughts toward the object that you need to work with.

If CL were an OOP language, you wouldn’t have commands like DLTDTAARA. Instead, you would declare objects to be of a certain class (type), and this declaration would give you immediate access to methods (commands) that would let you manipulate the object. As a result, you might declare a CL variable to be of class *DTAARA, as the Declare (DCL) command shows in Figure 2.

However, the DCL command would only create a variable. You would then have to tie the variable to the real data area, using syntax similar to CHGVAR in Figure 2. Execution of these two commands would let you use such methods as CRT, DLT, and CHG to manipulate the data area represented by variable &INFO.

In OO CL, you wouldn’t use DLTDTAARA to delete the REVISION area in the ATLANTA library. You would instead invoke a deletion method like the one in Figure 3. Note that I’ve separated the variable from the method with a period (commonly referred to as a dot operator) and I’ve used the parentheses that follow DLT to show that DLT was a method, not a variable. I’ve chosen the conventions of the dot qualification operator and method parentheses because they are used in C++ and Java.

Consequently, if CL were an OOP language, your thought process would be reversed. You would first think about the data area to be deleted and then move your attention to how to delete it.

Inheritance

Because a data area is a type of object, you can also use commands such as Create Duplicate Object (CRTDUPOBJ) and Move Object (MOVOBJ) to manipulate data areas. If CL were CL++, certain methods would be defined for use with the “object” class. The “object” class would have methods such as Move (MOV), Create Duplicate (CRTDUP), Check (CHK), Compress (CPR), Decompress (DCP), and Edit Authority (EDTAUT). Any of the specific types of objects, such as data areas, data queues, and programs, could use these methods. The code in Figure 4 shows how a variable of the *DTAARA type would use the MOV method of the *OBJECT type.

Getters and Setters

If CL were an OOP language, it would also have getters and setters. A getter is a method that retrieves a value from an object; a setter changes a value in the instance of an object.

First, I should review how to “get” and “set” values when working with objects in CL. To “get” a value from an object usually requires a Retrieve (RTV) command. You can have Retrieve Job Attributes (RTVJOBA), Retrieve Member Description (RTVMBRD), Retrieve Object Description (RTVOBJD), and so on.

To “set” a value, CL usually uses a Change (CHG) command, such as Change Data Area (CHGDTAARA), Change Job (CHGJOB), and Change User Profile (CHGUSRPRF).

Figure 5 shows how to retrieve from and change a data area in CL. However, if CL were an OOP language, you would use RTV and CHG methods instead, as shown in Figure 6. In fact, you might have more than one of each method. For instance, you might have one RTV method that extracts a substring of a data area and another RTV method that extracts the entire data area, as in Figure 7. The compiler would know which of the two methods to invoke by referring to the parameter list.

A Bigger Example

Figure 8 shows a CL program that creates a data queue (if it doesn’t already exist), receives information from a second data queue, changes that information, and sends it to the first data queue. If CL were an OOP language, you might write it as it is written in Figure 9. Again, the primary difference is that the commands begin with the object being manipulated and move from there to the action being taken.

Are There Any Objections?

If you’ve looked at OOP before but were put off by its strange terminology, don’t give up. OOP is logical in design, as is procedural programming, and you can master both.

DLTDTAARA DTAARA(ATLANTA/REVISION)

Figure 1: Use CL’s DLTDTAARA command to delete a data area.

DCL VAR(&INFO) TYPE(*DTAARA)

CHGVAR VAR(&INFO) VALUE(NEW DTAARA('ATLANTA/REVISION'))

Figure 2: OO CL would declare a variable to reference a data area object.

&INFO.DLT( )

Figure 3: Here’s one method you might use to delete a data area in OO CL.

&INFO.MOV(CHARLOTTE)

Figure 4: A data area in OO CL would be able to use methods defined for the *OBJECT type.

RTVDTAARA DTAARA(ATLANTA/REVISION (26 8)) RTNVAR(&OPTION)

CHGDTAARA DTAARA(ATLANTA/REVISION (121 8)) VALUE('CLEANUP')

Figure 5: CL accomplishes “getting and setting” with CHG and RTV commands.

&INFO.RTV((26 8) &OPTION)
&INFO.CHG((121 8) 'CLEANUP')

Figure 6: OO CL would use “getters and setters” to manipulate objects.

&INFO.RTV((26 8) &OPTION)
&INFO.RTV(&PLANTINFO)

Figure 7: Methods of the same name are common in OOP.

&INFO.RTV((26 8) &OPTION)
&INFO.CHG((121 8) 'CLEANUP')

Figure 8: This simple CL program manipulates data queues.

PGM

&PRODDATA.DCL(*CHAR 80) /* production data */

&DTALEN.DCL(*DEC (5 0) 80)

&WAIT.DCL(*DEC (5 0) -1)

&FROMFLOOR.DCL(*DTAQ) /* from shop floor */

&TOPRODCTL.DCL(*DTAQ) /* to production control */

/* Instantiate the variables */

&FROMFLOOR.CHG(NEW DTAQ('*LIBL/FROMSHOP'))

&TOPRODCTL.CHG(NEW DTAQ('*CURLIB/TOPRODCTL'))

/* Create the to-production-control data queue, if it doesn't exist */

&TOPRODCTL.CREATE(*STD 80 *N *FIFO *N +

'send info to production control' *N)

MONMSG CPF0000

/* Receive data from the shop floor */

&FROMFLOOR.RCV(&DTALEN &PRODDATA &WAIT)

/* Change the data */

&PRODDATA.CHG((56 2) '00')

/* Send it to production control */

&TOPRODCTL.SND(&DTALEN &PRODDATA)

IF (&PRODDATA.RTV(1 4) *NE QUIT) THEN(GOTO RCV)

ENDPGM

Figure 9: This fictitious program code illustrates how data queues might be manipulated if CL were an OOP language.

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

$

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: