Wednesday, February 5, 2025

Retrieve procedure name in the ON-EXIT section

Prior to the latest Technology Refreshes, IBM i 7.5 TR5 and 7.4 TR11, it was not possible to get the procedure's name using the %PROC built in function in the ON-EXIT section of any procedure.

IBM explained to me that the ON-EXIT section of a procedure is another procedure, which is why they would not allow the procedure name to be returned, as it would not be what I expected.

In the latest TR IBM has provided us with a solution. There are now three versions of the %PROC BiF:

01  %proc() ;

02  on-exit ;
03    %proc(*onexit) ;
04    %proc(*owner) ;
05  end-proc ;

Line 1: The original way to return the procedure name is still valid when not in the ON-EXIT section.

Line 3: Only valid in the ON-EXIT section. Returns the name of the procedure that implements the ON-EXIT section.

Line 4: Again this is only valid in the ON-EXIT section. Returns the name of the procedure that contains the ON-EXIT section.

An example always help explains the specifics. Here an example of using the different forms of %PROC.

01  **free
02  ctl-opt main(Main) dftactgrp(*no) ;

03  dcl-proc Main ;
04    ExtremelyVeryLongProcedureNameThatIsJustTooLong() ;
05  on-exit ;
06    snd-msg 'Main %PROC(*ONEXIT) = ' + %proc(*onexit) ;
07    snd-msg 'Main %PROC(*OWNER) = ' + %proc(*owner) ;
08  end-proc ;


09  dcl-proc ExtremelyVeryLongProcedureNameThatIsJustTooLong ;
10    snd-msg 'P_1 %PROC() = ' + %proc() ;
11  on-exit ;
12    snd-msg 'P_1 %PROC(*ONEXIT) = ' + %proc(*onexit) ;
13    snd-msg 'P_1 %PROC(*OWNER) = ' + %proc(*owner) ;
14  end-proc ;

The above program contains two procedures. The first is a Main procedure, and the second is just a procedure with an exceptionally long name.

Line 4: All the Main procedure does is call the other procedure, the one with the long name.

Lines 5 - 7: Main has an ON-EXIT section. Within the section I have two statements that uses the Send Message operation code, SND-MSG, to write messages to the job log. Why did I use SND-MSG rather than DSPLY? The message string with the long procedure name turns out to be more than 52 characters, which is DSPLY's maximum length.

Lines 9 – 14: The ExtremelyVeryLongProcedureNameThatIsJustTooLong procedure just contains statements with the %PROC BiF.

Line 10: This version of the %PROC will return the name of the procedure.

Line 12: This %PROC returns the name of the procedure that implements the ON-EXITsection.

Line 13: And this %PROC returns the name of the procedure that contains the ON-EXITsection.

Having created/compiled this program, I call it. After it has completed when I look in the joblog I see:

01  P_1 %PROC() = EXTREMELYVERYLONGPROCEDURENAMETHATISJUSTTOOLONG

02  P_1 %PROC(*ONEXIT) =
      _QRNI_ON_EXIT_EXTREMELYVERYLONGPROCEDURENAMETHATISJUSTTOOLONG

03  P_1 %PROC(*OWNER) = EXTREMELYVERYLONGPROCEDURENAMETHATISJUSTTOOLONG

04  Main %PROC(*ONEXIT) = _QRNI_ON_EXIT_MAIN

05  Main %PROC(*OWNER) = MAIN

I have added a line space between each line to make it easier to read.

Lines 2 and 4 show the name of the procedure that implements the name of the ON-EXIT section.

IMHO this is a useful addition to the ON-EXIT as I can capture the name of the procedure even when the procedure fails.

 

You can learn more about the %PROC procedure from the IBM website here.

 

This article was written for IBM i 7.5 TR5 and 7.4 TR11.

No comments:

Post a Comment

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.