Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Scoping of my with foreach loop

by BrowserUk (Patriarch)
on Mar 07, 2005 at 19:29 UTC ( [id://437293]=note: print w/replies, xml ) Need Help??


in reply to Scoping of my with foreach loop

It doesn't make a lot of sense to use a loop variable that was previously declared, because the one that gets used within the loop is (temporarally), an entirely different variable, despite that it has the same name:

my $i = -23; for $i ( 1 .. 10 ) { print $i; last if $i == 5; } print $i; 1 2 3 4 5 -23

Doing so tends to make look as if $i will retain the last value used in the loop, but as you can see, it doesn't.

It caught me out a couple of times and is something that I hope will not persist into Perl6.

In fact, I think I would change it in perl 5.10. Any code that got bitten by the change in behaviour would be better changed anyway.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 21:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found