O'Reilly logo

Perl Cookbook by Nathan Torkington, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required