Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^3: How foreach loops decide what to iterate through

by jasonk (Parson)
on Feb 15, 2009 at 18:33 UTC ( #743951=note: print w/replies, xml ) Need Help??

in reply to Re^2: How foreach loops decide what to iterate through
in thread How foreach loops decide what to iterate through

Essentially what you are doing is something like this:

my @a = (1); for ( my $i = 0 ; $i <= $#a ; $i++ ) { push( @a, 1 ); }

So the loop isn't being restarted or re-evaluated, but because you are extending the thing it's iterating over, it never gets to the end.
We're not surrounded, we're in a target-rich environment!

Replies are listed 'Best First'.
Re^4: How foreach loops decide what to iterate through
by jvector (Friar) on Feb 15, 2009 at 22:38 UTC
    The insight moment for me in this thread was getting when it is that an anonymous array is created and used (and therefore the loop is unaffected by modifications to the original array during its execution) as opposed to the original array itself being used during the loop.

    Note to self: check copy of PBP to see if this is mentioned.

    Find "This signature" for less on Ebay

      an anonymous array is created and used

      An anonymous list, not an anonymous array. (Well, all lists are anonymous.) No array is created.

      Arguments and return values of functions and subs are placed on the stack as a list of scalars.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://743951]
[hippo]: Thanks for your input which has soothed my sanity (a little)
[Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now $x = 0.00 will be true instead of false as it were before
[Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning
[Corion]: We sell sanity in small packages ;)
[Eily]: oh, boolean context uses the "" overload if no bool overload has been defined
[Eily]: (I wondered if overloading bool was actually necessary)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2017-07-27 13:46 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (414 votes). Check out past polls.