in reply to Re: Odd behaviour with $. and <DATA>
in thread Odd behaviour with $. and <DATA>

OK -- so the difference is how foreach and while operate differently. Thanks -- that explains it all.

But (since I'm not allowed to use the phrase beg the question) I wonder why that is .. foreach suggests that, for each element in the list, it's going to do something. It doesn't suggest that it's going to slurp up the entire list at once.

Update Sorry, disregard this .. you've explained that one works in list context and the other works in scalar context .. my question is reduced to 'Why do they work in different contexts' and I guess the answer is, 'They do -- period'.

Update2 This is properly explained on p.34 (the first paragraph) of the Camel (3rd edition) -- if and while work in scalar context, and foreach works in list context.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re^3: Odd behaviour with $. and <DATA>
by ikegami (Patriarch) on Jan 16, 2006 at 21:22 UTC

    Before foreach can iterate over the list, it needs to be built. It works the same way for function. A list must be built before it can be used. In this case, building the list means reading the entire file.

    foreach (x..y) is an exception to this. That list is evaluated lazily.

Re^3: Odd behaviour with $. and <DATA>
by blazar (Canon) on Jan 17, 2006 at 08:54 UTC
    But (since I'm not allowed to use the phrase beg the question) I wonder why that is .. foreach suggests that, for each element in the list, it's going to do something. It doesn't suggest that it's going to slurp up the entire list at once.

    Indeed: but before iterationg over the list, the latter has to be created, which should answer your question. Said, this, the list has to be created unless we have "lazy evaluation", which we curretly have not, with the exception of a pair of special cases, whereas Perl 6 will have it by default -and you'll generally need to explicitly tell it to do differently, if you want so!- in fact for will be the default for iterating over iterators, be them regular lists, filehandles or more exotic objects.