FAQ 7.16 How do I create a static variable?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .


7.16: How do I create a static variable?

    (contributed by brian d foy)

    In Perl 5.10, declare the variable with "state". The "state" declaration
    creates the lexical variable that persists between calls to the

            sub counter { state $count = 1; $counter++ }

    You can fake a static variable by using a lexical variable which goes
    out of scope. In this example, you define the subroutine "counter", and
    it uses the lexical variable $count. Since you wrap this in a BEGIN
    block, $count is defined at compile-time, but also goes out of scope at
    the end of the BEGIN block. The BEGIN block also ensures that the
    subroutine and the value it uses is defined at compile-time so the
    subroutine is ready to use just like any other subroutine, and you can
    put this code in the same place as other subroutines in the program text
    (i.e. at the end of the code, typically). The subroutine "counter" still
    has a reference to the data, and is the only way you can access the
    value (and each time you do, you increment the value). The data in chunk
    of memory defined by $count is private to "counter".

            BEGIN {
                    my $count = 1;
                    sub counter { $count++ }

            my $start = counter();

            .... # code that calls counter();

            my $end = counter();

    In the previous example, you created a function-private variable because
    only one function remembered its reference. You could define multiple
    functions while the variable is in scope, and each function can share
    the "private" variable. It's not really "static" because you can access
    it outside the function while the lexical variable is in scope, and even
    create references to it. In this example, "increment_count" and
    "return_count" share the variable. One function adds to the value and
    the other simply returns the value. They can both access $count, and
    since it has gone out of scope, there is no other way to access it.

            BEGIN {
                    my $count = 1;
                    sub increment_count { $count++ }
                    sub return_count    { $count }

    To declare a file-private variable, you still use a lexical variable. A
    file is also a scope, so a lexical variable defined in the file cannot
    be seen from any other file.

    See "Persistent Private Variables" in perlsub for more information. The
    discussion of closures in perlref may help you even though we did not
    use anonymous subroutines in this answer. See "Persistent Private
    Variables" in perlsub for details.


The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in

Re: FAQ 7.16 How do I create a static variable?

Quoted text here. Click to load it
Why don't you go live in another dimension, call it the new shit!
Otherwise you can't, even if its just an acronym.

Why waste peoples' time with bullshit?

Faq workers suck as usual, determined via trial and error !
Anybody says there is ONE Faq worker is full of shit !!
If one, he/she sucks beyond compare and if many, they suck exponetially !!


Re: FAQ 7.16 How do I create a static variable?

sln@netherlands.com wrote:

Quoted text here. Click to load it

Why don't you knock it off already?  There's no reason to get so excited
and upset about it and use profanity.  If you bothered to notice, Brian
replied to your complaint about another FAQ article the other day and
stated he had updated the FAQ with the information you posted.  These
are FAQs that were compiled from a lot of people's input and
contributions over the years, and all you do is freak out randomly in
response to them being posted as a means to offer help to new users
that could benefit from them.  You are clearly welcome to contribute
and they will accept valid contributions and modify the FAQ.  So,
exactly what is the big problem you have here about them?  I'm also not
responding to your other post, where you (as usual) replied to yourself
with even more abusive and profane nonsense.
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Site Timeline