CL Odds and Ends
October 11, 2006 Hey, Ted
I hope this question is not too simple for a newsletter that claims to be for gurus. An RPG program or module retains variables, open data paths, and other resources from one invocation to the next, as long as the LR indicator is not on. CL programs and modules, on the other hand, suffer from Alzheimer’s disease. They don’t remember anything from one invocation to the next. How do you handle this limitation? –Richard I can help you with part of this problem, Richard. Create a temporary data area to hold the variable values from one call to the next. In the following example, the program saves the values of two variables–&DecVar and &CharVar. pgm dcl &SaveVars *char 16 dcl &DecVar *dec 3 dcl &CharVar *char 12 dcl &MsgKey *char 4 dcl &PgmName *char 10 dcl &RtnType *char 2 dcl &Sender *char 80 /* Determine the name of this program. */ sndpgmmsg msg('Dummy message') topgmq(*same) + msgtype(*info) keyvar(&msgkey) rcvmsg pgmq(*same) msgtype(*info) msgkey(&MsgKey) + rmv(*yes) sender(&Sender) chgvar var(&PgmName) value(%sst(&Sender 27 10)) rtvdtaara (qtemp/&PgmName (1 16)) rtnvar(&SaveVars) monmsg cpf1015 exec(do) crtdtaara qtemp/&PgmName type(*char) len(16) enddo /* Restore the value of the variables from the previous invocation */ chgvar &DecVar %sst(&SaveVars 1 4) chgvar &CharVar %sst(&SaveVars 5 12) /* Do whatever */ /* Save the value of the variables for the next invocation */ chgvar %sst(&SaveVars 1 4) &DecVar chgvar %sst(&SaveVars 5 12) &CharVar chgdtaara (qtemp/&PgmName ( 1 16)) value(&SaveVars) endpgm When storing decimal numbers, allow an extra byte for the sign, and if there are decimal positions, allow another byte for the decimal point. Can you see a potential problem with this technique? If the data area already exists, from a previous run, say, then you will start off with old data. The caller needs to check for the existence of the data area of the data area and delete it before the first call to the CL routine. As for open data paths, locked data areas, or whatever, I don’t know a way to help you, but I’ve never found any of those things to be show-stoppers. And as for simple questions, be aware that readers of this publication come in all ranges of experience level. Don’t forget about the IT Jungle Forums, which you can find at https://www.itjungle.com/itjforums/. You’ll find some helpful and knowledgeable people there.
I was recently working on a CL program that someone else had written, and came across a line that looked something like this: CALL SOMEPGM PARM(RESET) Why is there no ampersand on the beginning of RESET? –Sarah RESET is not a variable, but a character literal. If you had written the routine, you would have coded the command this way: CALL SOMEPGM PARM('RESET') You could also have coded it like this: CALL SOMEPGM PARM(reset) Character literals do not have to be quoted if:
In all three cases, the value RESET, in all uppercase letters, would have been passed to program SOMEPGM. Here another oddity you may not know. CL ignores everything after the ENDPGM command. Put anything down there you like: free-form comments without delimiters, code that you intend to put in later, code that you’ve removed but aren’t ready to throw away, etc. –Ted |