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.


In reply to Perl 6 and Scoping by talexb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.