_brian_d_foy wrote in perl.documentation :
>=head2 How do I create a static variable?
>
> (contributed by brian d foy)
>
> Perl doesn't have "static" variables, which can only be accessed from
> the function in which they are declared. You can get the same effect
> with lexical variables, though.
>
> You can fake a static variable by using a lexical variable which
> goes of scope. In this example, you define the subroutine
> C<counter>, and it uses the lexical variable C<$count>. Since you wrap
> this in a BEGIN block, C<$count> is defined at compile-time, but also
> goes out of scope at the end of the BEGIN block. The subroutine

Well, I don't understand why the BEGIN block is necessary here.
Maybe add that you want C<$count = 0> be executed at compile-time, if
that's what you meant.

> C<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 C<$count> is private to
> C<counter>.
>
>    BEGIN {
>       my $count = 0;
>       sub counter { $count++ }
>       }

Reply via email to