RPGIV @ Work

A unique site for RPG and System i Lovers

Welcome!

Hi, this site will provide all what you need in System i and RPG developments.

My Name is Chamara Withanachchi, System i Expert and RPG Developer. And in the field for last 11 years.

I hope you will find lot of valuable information from this site

Local Files in Sub Procedures Print E-mail
User Rating: / 0
PoorBest 
Written by Chamara   

Let’s first address the situation of local files in subprocedures. If you code an F spec after a P spec, then that file is local to the subprocedure.


By default, a local F spec file will automatically be opened every time the procedure is called and it will be automatically closed every time the procedure ends (i.e., returns to its caller) regardless of the value of the LR indicator.


An important detail to note is that the LR indicator has no effect. LR is part of the RPG cycle and the RPG cycle doesn’t apply to subprocedures. In a way, it seems that subprocedures are behaving as if LR is always on, but it really has nothing to do with the cycle. Instead it has to do with automatic vs. static storage. Just like local D-spec-defined data in subprocedures, local files use automatic storage and therefore, the file’s storage disappears when the procedure returns to its caller.


Of course, it may have occurred to you that there could be a potential negative performance impact in opening and closing the files on every call to the subprocedure. Is there something we can do to alleviate that?


Yes. Just as you can code the STATIC keyword on D-spec-defined data to keep it around between calls, you can use the STATIC keyword on your local files as well. That means the file will remain open at its current position and state between calls to the procedure.


So when will the local STATIC file be closed? Only when explicitly closed via a CLOSE operation or when the activation group ends. As we said earlier, the status of LR has no impact on closing a local file in a procedure, whether it uses static or automatic storage. This catches many RPGers by surprise when they find files remaining open long after the part of the application using them is finished.


<Previous   Next>