How To Retrieve Your CL
Program's Name AND Library
Tech tip courtesy of Barsa Consulting, LLC and Dave Schnee
When you write a program in RPG, it's easy
to find your program's name and the library you're running it from – you simply
look in the program's Status Data Structure (SDS). There is no such easy means if your program is
written in CL. There are a number of
solutions that send a message to yourself, then retrieve that message to get
the "sender" information, but that just gives you the program's name,
not the library. You can (with some
effort) look at the program stack, but that also omits the library information.
Here's a relatively painless (it runs very
quickly and does not leave any messy footprints around) example of how to get
both the program's name AND the program's library in one go. The only "funny" thing is that you
have to change the source type from CLP to CLLE. That's not too hard, though – you can still compile it with PDM
option '14' (or use the CRTBNDCL command) and almost everything you do in CLP
works in CLLE (one notable exception is the TFRCTL command).
The example below retrieves the information
and then sends a message to *PRV with the results. You can try this from a command line and see the result in your
joblog. For the inquisitive minds, the
procedure call is to an unblocked machine instruction (Materialize Program
Name) that has been in the operating system since before 1996. (Be careful of the underscore character in
the procedure name.) Try it, you'll
like it.
Testpgm: PGM
DCL VAR(&DATA) TYPE(*CHAR) LEN(80)
DCL VAR(&MSG) TYPE(*CHAR) LEN(80)
CHGVAR VAR(%BIN(&DATA 1 4)) VALUE(80)
CHGVAR VAR(%BIN(&DATA 5 4)) VALUE(80)
CHGVAR VAR(%BIN(&DATA 9 4)) VALUE( 0)
CHGVAR VAR(%BIN(&DATA 13 4)) VALUE( 0)
CALLPRC PRC('_MATPGMNM') PARM(&DATA)
CHGVAR VAR(&MSG) VALUE('This program is ' *CAT +
%SST(&DATA 51 10) *BCAT 'in library ' +
*CAT %SST(&DATA 19 10))
SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) MSGDTA(&MSG)
ENDPGM