Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Perl 6 and Scoping

by talexb (Chancellor)
on Jan 27, 2002 at 07:45 UTC ( [id://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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://141875]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found