in reply to subroutines and namespace
The point about statically scoped (my) variables is that what they refer to can be completely determined by their point of definition. So if I say
then it is possible, just by examining the point of definition of quux, to determine what gets modified where.my $foo=17; sub quux { my $bar=29; $foo = baz($foo,++$bar) }
What you're after is global variables. You want some variable defined in one place in your program to get modified in an entirely different place. Why is this bad?
Well, consider this code:
Note how every time some other $foo gets modified. Just by examining the new definition of quux you get no clues as to what gets modified. Instead, you have to examine the point where quux gets called, then work your way up the call stacks to surrounding blocks. You examine each block to see if local $foo sets up a new copy, or find $foo at the top level. And that's the copy of $foo that gets modified.use vars qw/$foo/; sub quux { my $bar=29; $foo = baz($foo,++$bar) } # ...code passes... $foo = 11; print quux($foo), "\n"; # ...more code... { local $foo = 39; print quux($foo), "\n"; }
Variable lookup with global variables is akin to spaghetti. You can eat it, but there are healthier alternatives...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re2: subroutines and namespace
by dragonchild (Archbishop) on Nov 02, 2001 at 19:49 UTC |