Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: strange scope

by Zaxo (Archbishop)
on May 22, 2007 at 23:29 UTC ( [id://616899]=note: print w/replies, xml ) Need Help??


in reply to strange scope

The scope of $x is the entire if . . . else construct. Since the condition following if is always evaluated, that is where the lone my declaration belongs.

Consider,

$ perl -Mstrict -e'if (my $x = 0) {} print $x,$/' Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $
but,
$ perl -Mstrict -e'if (my $x = 0) {} elsif (1) { print $x,$/}' 0 $
I agree that that scope is not what one expects.

Update: Two more cases,

$ perl -Mstrict -e'if ($\) {print $x,$/} elsif (my $x = 0) {} else { p +rint $x,$/}' Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $
but,
$ perl -Mstrict -e'if ($\) {} elsif (my $x = 0) {} else { print $x,$/} +' 0 $
showing that $x may be declared in an elsif condition to narrow its scope, if not needed in previous clauses.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: strange scope
by blazar (Canon) on May 23, 2007 at 09:12 UTC
    I agree that that scope is not what one expects.

    ISTR that this is a "problem" Perl 6's new scoping rules take care of.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found