From: "Dr.Ruud" <[EMAIL PROTECTED]>
> "Jenda Krynicky" schreef:
> > {
> >   my $static;
> >   sub foo {
> >     $static++;
> >     ...
> >   }
> > }
> 
> There (the first declared version of) the variable $static is part of
> the environment of foo(). Don't mistake that for staticness.

Maybe I don't know what does "staticness" mean then. I though a 
static variable is one that is private to a function, but keeps the 
value between the function's invocations. How do you define 
staticness?

> In Perl 5.8.8 you can enforce $static to be static, like this:
> 
> {
>   0 and my $static;
>   sub foo {
>     $static++;
>     ...
>   }
> }
> 
> That ugly my() can only occur once, ut it still makes the variable
> lexical.
> There is just no better way to set up a real static variable in Perl
> 5.8.8.
> 
> 
> Check out the differences between the following two "academic" examples:
> 
> $ perl -le'
>    for (7..9)
>    {
>      my $static = $_;  # declared and initialised 3 times
> 
>      sub foo {
>        $static++;  # uses the first of the declared $static's
>        print "  foo:$static";
>      }
>      foo() for 0..1;
>      print "for:$static";
>    }
> '

With -w you get a "Variable $static will not stay shared" warning. 
And rightly so. You are doing something you are not supposed to do.

A named subroutine inside another subroutine or a loop is a red flag. 
Something that (unless found in an obfuscation) suggests that the 
author of the code misunderstood something. It's yet another "please 
don't do this".

$ perl -le'
   for (7..9)
   {
     my $static = $_;  # declared and initialised 3 times

     my $foo = sub {
       $static++;  # uses the first of the declared $static's
       print "  foo:$static";
     };
     $foo->() for 0..1;
     print "for:$static";
   }
'

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to