"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.
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";
}
'
foo:8
foo:9
for:9
foo:10
foo:11
for:8 (would be undef without the initialisation)
foo:12
foo:13
for:9 (would be undef without the initialisation)
$ perl -le'
for (7..9)
{
0 and my $static = $_; # declared *once*,
# *never* initialised
sub foo {
$static++;
print " foo:$static";
}
foo() for 0..1;
print "for:$static";
}
'
foo:1
foo:2
for:2
foo:3
foo:4
for:4
foo:5
foo:6
for:6
--
Affijn, Ruud
"Gewoon is een tijger."
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/