Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Perl 6 and Scoping

by talexb (Canon)
on Jan 27, 2002 at 07:45 UTC ( #141875=perlmeditation: print w/replies, xml ) 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

if (my $result = expensive_function()) { # $result is in scope here } # $result no longer in scope
but with the proposed Perl 6 syntax:
if (my $result = expensive_function()) { # $result is in scope here } # $result is also in scope here !!
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.

--t. alex

"Of course, you realize that this means war." -- Bugs Bunny.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://141875]
Approved by root
[erix]: those clever germans

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-08-17 14:39 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (288 votes). Check out past polls.