Informing the IBM Community

Why you should embrace CL’s new features


Abstract technology background with hexagonal shapes

From the AS/400’s launch in 1988 through to V5R3 in 2004, IBM made no enhancements to CL programming. We thought we were burdened with the same CL forever. We were stuck with restrictions like only being able to open one file in a program or the inability to use printer or ICF files. We couldn’t use sub-files within display files, nor could we use program-described display files.

Then IBM got very busy on the CL-front and we now have many great features. I would like to take this opportunity to walk through some of them.

One of the first enhancements we had was the ability to use up to, and including, five files in a program. In the figure below, we can see three files being declared and then used in a program.

AY Mar Fig1

This enhancement saved us from having a one file, one CL program restriction but the main enhancement, in my opinion, was the addition of structured programming commands. Now we are really talking about a programming language, so let us take a look at each of these structured commands in detail.

DOFOR – This command is basically a For Loop. It will execute commands a number of times.

AY Mar Fig2

In the figure above, we can see the DOFOR loop will increment the count variable and execute the send program message command ten times.

In other programming languages, the DOFOR command behaves in a similar fashion to a FOR command.

DOUNTIL – Performs loop processing until a condition is reached. The DOUNTIL command always executes the DO loop at least once.
AY Mar Fig3

DOWHILE – Very similar to the DOUNTIL command, except this one will check the condition before the loop is performed, not after.
AY Mar Fig4

The difference between a DOUNTIL and a DOWHILE loop always seems to pop up at interviews.

ITERATE – This command interrupts the processing of a DO loop and passes control to the associated ENDDO command. This is shown in the figure below.
AY Mar Fig5

SELECT – This is used in a block to perform conditional processing of groups. It is used in conjunction with the WHEN and OTHERWISE commands.
AY Mar Fig6

The LEAVE command drops out of a DO loop.

AY Mar Fig7

As you can see in the above examples, we have to code ENDDO/ENDSELECT to mark the end of either a DO or SELECT structured block.

In 2006, IBM kindly gave us V5R4 and more improvements. This time we had subroutines and pointer-type variables. Subroutines give us the ability to name a section of our program, then reuse it as many times as we like.
AY Mar Fig8

As you can see in the above figure, a subroutine starts with the SUBR command and it ends with ENDSUBR. All the code in between is our subroutine. To call this subroutine we use the CALLSUBR anywhere in our CL program.

In 2008, version 6.1 came along. This time, we had only one major update, the INCLUDE command.  Programmers of other languages will again be familiar with this command as it copies in, at compile time, source from another source member.
AY Mar Fig9

If we look at the compiler list, we can now see the entire source from Error1 include at compile time.

AY Mar Fig10

Then, in 2010, we had 7.1, and IBM added support for built-in functions to work with character strings. I am sure our RPG readers will recognise these. Please note, PTF SI49061 must be loaded and applied in order to use these new CL built-in functions.

Let us go through each of them. The Check functions, %CHECK and %CHECKR, give us the ability to check if certain characters appear, or not, in a string. %CHECK will let you know the first position of a string that does not exist in the compare string.

AY Mar Fig11

The %CHECKR (Check Reverse) function will inform you of the last position of the string that does not appear.
AY Mar Fig12

Trim functions, which perform like the RPG equivalent, will trim, or strip, characters from a string. There are %TRIML (trim character from the left only), %TRIMR (trim characters from the right only) and %TRIM (trim characters from both left and right).
AY Mar Fig13

Also introduced was the Scan Function %SCAN. This one gave us the ability to search for a string within another string. If it does not find it, %SCAN returns zero.
AY Mar Fig14

With last year’s 7.2 release, IBM once again focused on the built-in functions to enhance CL. The %LOWER function will convert any upper-case characters to lower-case. The %UPPER function will convert any lower-case characters to upper-case. Both of these functions are demonstrated in the figure below.
AY Mar Fig15

The %CHAR function converts a non-character to a character. For example, from an integer to a character string as seen below.
AY Mar Fig16

The %DEC, as I’m sure you can guess, converts a non-numeric to a numeric. The %INT function converts a value to an integer. The %UINT function will convert a value to an unsigned integer.

Next, we have two new functions for the sizing of variables. The %LEN function will inform us of the number of digits, or characters, in a variable. The %SIZE function returns the number of bytes occupied by a variable. Both the %LEN and %SIZE functions are presented below.
AY Mar Fig17

For those who think the above two functions seem the same, the difference between %LEN and %SIZE is that %SIZE is calculated at compile time and %LEN is calculated at run time.

These are just some of the major enhancements to CL programming. You can visit the IBM information centre for full details.

We are grateful to IBM for providing us with all these cool new features over the past 11 years. But when I am at customer sites, I very rarely see them used. This may be because the majority of people who use CL to program are either operators or technical support staff who, by their own admission, have no desire to be programmers.

However, now we have these new features, it is up to us. Let us all rise to the challenge and embrace them.

Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.