The Nearly Forgotten DSPLY Operation
September 10, 2014 Paul Tuohy
I am sometimes asked strange questions at conferences. A recent example is: “Which operation code do you use most in RPG?” After overcoming my initial reaction of wondering what the questioner was smoking (and where I could get some), I gave the question some thought. And the answer is: The DSPLY operation. Now, before you start to wonder what I am smoking (and where you can get some), let me explain. Firstly, the DSPLY operation will never see its way into production code, I only use it in test programs (more in a minute). Secondly, and what a lot of programmers have missed, the DSPLY operation can be used to input data as well displaying data. This means that I can avoid the necessity of display files for simple test programs. So, let’s talk about the DSPLY operation and how I use it. The DSPLY Operation The DSPLY operation may be used to communicate with a message queue. The operation can display a message or display a message and accept a response. The message may be a literal or may identify a message ID. The operation has the following format: DSPLY{(E)} {message {message-queue {response}}} The major restriction of the DSPLY operation is that the combined message and response may not exceed 52 characters. Full details may be found in the RPG Reference manual. Making Use Of DSPLY Nearly every program I write consists mainly of subprocedures. I try to keep my subprocedures simple, so they do one thing. This means, for the most part, that my testing consists of testing subprocedures, which is a simple process, pass parameters and check whether or not the correct values are returned. Therefore, a simple test program will prompt for parameters, call the subprocedure, and display the returned values. Let’s take a simple example. The next piece of code shows a subprocedure that accepts a single character field (Y or N) and returns a corresponding word (Yes, No, or ??? for an unidentified parameter value). p get_Word b d pi 3a d wordIn 1a const d wordOut s 3a inz('???') /free if (wordIn = 'Y'); wordOut = 'Yes'; elseIf (wordIn = 'N'); wordOut = 'No'; endIf; return wordOut; /end-Free p e Now let’s look at how the test program made use of the DSPLY operation program to get_Word() subprocedure. h dftActGrp(*no) d forChar s 1a d showWord s 3a /free doU (forChar = 'E'); dsply 'Enter Character (E/Y/N/?)' '*EXT' forChar; if (forChar <> 'E'); showWord = get_Word(forChar); dsply showWord; endIf; endDo; *inLR = *on; /end-Free The logic of the program is very simple:
Calling the test program and performing a test result in the interaction is shown here: DSPLY Enter Character (E/Y/N/?) Y DSPLY Yes DSPLY Enter Character (E/Y/N/?) Y N DSPLY No DSPLY Enter Character (E/Y/N/?) N X DSPLY ??? DSPLY Enter Character (E/Y/N/?) X E There are a couple of points worth noting:
The 52-Character Problem A lot of the time, 52 characters is just not enough. In which case, you may find the subprocedure shown below to be of some use. The dsplyLong() subprocedure takes up to 5,200 characters of data and display them in 52-character chunks. Adjust the size of the parameter to suit your needs. p dsplyLong b d pi d longDataIn 5200a varying const d ds d longData 5200a d show 52a dim(100) overlay(longData) d i s 10i 0 d times s 10i 0 /free longData = longDataIn; times = (%len(%trimR(longDataIn)) / %len(show(1))); if (%rem(%len(%trimR(longDataIn)): %len(show(1))) > 0); times += 1; endIf; for i = 1 to times; dsply show(i); endFor; /end-Free p e Lots Of Test Programs I have hundreds of little test programs like these on my system. Whenever I need to change a subprocedure, I have a test program to hand to check my changes. Although DSPLY may not be the most elegant operation code in existence and may not be up to production standards, it definitely does the job for me when it comes to simple interfacing and saves me the hassle of defining more complex interfaces for simply promoting for parameters and showing results. Alternatively, you may want to make use of the QUILNGTX API to display long text. You can find a description of how to do that in my article A Better Way to Display Quick 5250 Messages in RPG. Paul Tuohy is CEO of ComCon, an iSeries consulting company, and is one of the co-founders of System i Developer, which hosts the RPG & DB2 Summit conferences. He is an award-winning speaker who also speaks regularly at COMMON conferences, and is the author of “Re-engineering RPG Legacy Applications,” “The Programmers Guide to iSeries Navigator,” and the self-study course called “iSeries Navigator for Programmers.” Send your questions or comments for Paul to Ted Holt via the IT Jungle Contact page. RELATED STORY A Better Way to Display Quick 5250 Messages in RPG
|