Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 7.16 How do I create a static variable?

Thread Tools

FAQ 7.16 How do I create a static variable?

PerlFAQ Server
Posts: n/a
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 .


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

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.

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
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 7.16 How do I create a static variable? PerlFAQ Server Perl Misc 0 04-14-2011 04:00 PM
FAQ or not FAQ? =?ISO-8859-15?Q?Juli=E1n?= Albo C++ 28 01-15-2007 04:33 AM
FAQ - How do I direct someone to this FAQ? FAQ server Javascript 1 08-04-2006 10:13 PM
[de] Update of FAQ in German/FAQ auf Deutsch ueberarbeitet Josef 'Jupp' Schugt Ruby 0 09-22-2003 08:56 PM