Skip to content

Purging Name Pollution in APL+Win

Vector 16:1, page 120

programming techniques development practice

Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 120

Variables in APL are global by default, but can be localised. {It may be preferable if it were the other way round.}

Imagine a Windows front end managing a legacy back end. Legacy systems tend to be badly written, and badly documented.

How does the front end clear the debris left by the legacy system?

It is usually not practical to track all the variables created in the legacy system. However, it is easy to identify the one function which starts the legacy system - it is the one called by the front end.

  1. Insert the word ProcPurge as the first statement on the first executable line in this function
  2. Add a line with the word Purge at the end, ensuring that this function exits via the last line.
  3. Localise Purge

What are ProcPurge and Purge?

    ∇ ProcPurge
[1]   ⍝ Ajay Askoolum
[2]   0 0⍴⎕def ('∇Purge',(,';',⎕nl 2)~' '),⎕tcnl,'[1]0 0⍴⎕ex ⎕nl 2∇'
    ∇

ProcPurge creates a function Purge which localises all variables in the workspace prior to calling the legacy system. Running the function Purge as the last thing before returning to the front end wipes out all variables created by the legacy system.

If the front end relies on the variables created by the legacy system, initialise the required variables prior to calling the legacy system. These variables will be modified but will not be deleted.

By the same token, the legacy system can modify any variable that existed before it started.

How does the front prevent the legacy system from modifying its variables?

Modify the ProcPurge function as shown below:

    ∇ ProcPurge
[1]   ⍝ Ajay Askoolum
[2]   ⍎⎕def ('∇Purge',(,';',⎕nl 2)~' '),⎕tcnl,'[1]0 0⍴⎕ex ⎕nl 2 ⋄ Legacy ∇'
    ∇

Note the second statement on the first line of Purge, Legacy. This is the function which starts the legacy system.

Thus, ProcPurge starts the legacy system, protecting all variables in the workspace. Be sure to run Purge when returning back to the front end.

Note

The above method will work for functions too (⎕nl 3) and for both variables and functions at the same time. Take care not to localise the function stated in ProcPurge.

The catch? The length of the names of variables or functions (or both) separated by ; and including the string ProcPurge must not exceed the maximum length of 32768 bytes.

Try it!