From perlsub:
If declared at the outermost scope (the file scope), then lexicals work somewhat like C’s file statics. They are available to all func‐ tions in that same file declared below them, but are inaccessible from outside that file. This strategy is sometimes used in modules to create private variables that the whole module can see.
So
is similar (from a scope perspective) to:my $var; sub foo { $var++; }
sub foo { my $var; { $var++; } }
Trying to avoid this *unintended* global issue is why you see some perl code with subroutines called main or with all the subs defined at the beginning of a file and the driver portion at the end:
orsub foo { ... } sub bar { ... } my $var = 0; foo(); bar();
I neither condemn nor condone these approaches - just pointing out why some devs do what they do; although the reverse definition always seemed too pascally to me.main(); sub main { my $var = 0; foo(); bar(); } sub foo { ... } sub bar { ... }
In reply to Re: Using an "outer" lexical in a sub?
by derby
in thread Using an "outer" lexical in a sub?
by cornballer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |