in reply to Local Scope Variables

One thing that I would like to add is the fact that it's generally a bad idea to allow functions or subs to absorb variables of greater scope by osmosis. If you want test2() to have access to a variable created in test1(), pass it to test2() by reference. If you want test2() to just receive the value, pass by value (instead of reference). But if you can at all avoid it, don't pass implicitly by assuming globals will be there to leak into functions.

There are exceptions, and one exception I can think of is static variables... this for example:

{ my $static = 10; sub test1 { print $static++, "\n"; } } test1(); test1(); __OUTPUT__ 10 11

This is a special situation where you actually want to access variables from outside of the sub's internal scope.


Dave