MainframeSupports
tip week 1/2011:

Sometimes you end up in situations where you think that there is no easy way out. Then to your big surprise you discover that the easy solution to the problem is already implemented. That is how I feel abut this tip. I was convinced that the problem I was facing could not be solved easily, but one of my colleagues found an easy solution.

Our problem was that we originally created a COBOL program to be used as the main program and it worked fine. Then we wanted to use it as a sub program which was called several times. At call number two the program acted quite weird. After some debugging we realised that the problem was caused by the fact that COBOL working-storage survives from call to call. One solution was to declare all variables as local-storage instead. Then my colleague discovered the CANCEL statement which re-initializes working-storage to the contents it had before the first call to the program specified in CANCEL. Here is an example of this. Imagine the following sub program called mysubpgm where eyecatcher is defined in working-storage section:

...
if eyecatcher not = 'NICELEGS'
  display 'Where was Kilroy'
  move 'NICELEGS' to eyecatcher
else
  display 'Kilroy was here'
end-if
...

And this main program:

...
call mysubpgm
call mysubpgm
cancel mysubpgm
call mysubpgm
...

When you execute the main program you receive the following output:

...
Where was Kilroy
Kilroy was here
Where was Kilroy
...

If eyecatcher was declared in local-storage section the output from the execution had been three times "Where was Kilroy". CANCEL enables you to control working-storage in a sub program from the calling program. We solved our problem by using CANCEL as it did not require any changes to the program used both as a main program and a sub program. In fact it is more flexible to use CANCEL instead of using local-storage. By the way CANCEL closes all files which the cancelled program has opened except the indirect opening of SYSOUT caused by executing the first DISPLAY statement.

There are a lot of tricky things about using CANCEL which you can read by following the link on the first occurence of the text CANCEL in this tip. I have not investigated all of them. There is a strange reference about dynamic called programs which makes me believe that CANCEL only works for dynamic called programs, but I have not tried it for static called programs. I will urge you to try this if you find this tip useful.

Previous tip in english        Sidste danske tip        Tip list