Creating Persistent Private Variables

Problem

You want a variable to retain its value between calls to a subroutine but not be visible outside that routine. For instance, you’d like your function to keep track of how many times it was called.

Solution

Wrap the function in another block, and declare my variables in that block’s scope rather than the function’s:

{
    my $variable;
    sub mysub {
        # ... accessing $variable
    }
}

If the variables require initialization, make that block a BEGIN so the variable is guaranteed to be set before the main program starts running:

BEGIN {
    my $variable = 1;                       # initial value
    sub othersub {                          # ... accessing $variable
    }
}

Discussion

Unlike local variables in C or C++, Perl’s lexical variables don’t necessarily get recycled just because their scope has exited. If something more permanent is still aware of the lexical, it will stick around. In this case, mysub uses $variable, so Perl doesn’t reclaim the variable when the block around the definition of mysub ends.

Here’s how to write a counter:

{
    my $counter;
    sub next_counter { return ++$counter }
}

Each time next_counter is called, it increments and returns the $counter variable. The first time next_counter is called, $counter is undefined, so it behaves as though it were for the ++. The variable is not part of next_counter’s scope, but rather part of the block surrounding it. No code from outside can change $counter except by calling next_counter.

Generally, you should use a BEGIN for the extra scope. Otherwise, you ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.