Perl 6 and Scopingby talexb (Canon)
|on Jan 27, 2002 at 07:45 UTC||Need Help??|
There has been some discussion 1 2 about new rules for variable scoping and a recent message to the Toronto Perl Mongers asked for clarification. I responded to the initial question, and I'm posting my explanation of the scoping differences as I understand them.
In a nutshell, the old Perl 5 syntax is
but with the proposed Perl 6 syntax:
This is really enforcing the rule that a variables scope is where it was defined.
I imgine that there is an exception in Perl 5 so that in this case, $result is scoped only within the control structure rather that in the scope in which it was defined, outside the control structure.
The scoping exception that is described regarding a subroutine defition is a red herring because a sub is not part of logic flow, it's part of the program definition. I can't think of any programming language where variables defined as part of a subroutine call would continue to live after the subroutine goes out of scope.
To put it another way, variables defined by an object's instantiation are not available to the rest of the objects just because they're 'a brace away' -- those variables only exist within that object, unless there are accessor methods available for public consumption.
"Of course, you realize that this means war." -- Bugs Bunny.