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.
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.
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.
The difference between a DOUNTIL and a DOWHILE loop always seems to pop up at interviews.
The LEAVE command drops out of a DO loop.
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.
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.
If we look at the compiler list, we can now see the entire source from Error1 include at compile time.
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.
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).
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.
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.
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