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

Re^2: why need my in a foreach loop?

by ikegami (Pope)
on Nov 28, 2010 at 08:22 UTC ( #874085=note: print w/ replies, xml ) Need Help??


in reply to Re: why need my in a foreach loop?
in thread why need my in a foreach loop?

The OP points out that there's no real difference between for $lex (...) and for my $lex (...). The "my" is just useless noise. As such, the declaration could be implicit.

It's reasonable, but it's not true because foreach loops don't just work with lexicals.


Comment on Re^2: why need my in a foreach loop?
Select or Download Code
Re^3: why need my in a foreach loop?
by szabgab (Priest) on Nov 28, 2010 at 18:37 UTC
    The foreach loop always works with an alias. IMHO the variable is never connected to anything outside the for loop so the values never "leak" out of the loop. If I am mistaken, please give an example when that variable is connected to something else as well.
      I've already showed when it makes a difference.
        Oh, apparently I did not read your example in detail. Interesting example. Now I understand how the "my" makes a difference there. Thanks.

        However I'd be interested to know in what case would you really want to write such code?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-10-31 13:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (217 votes), past polls