On Thu, Nov 13, 2003 at 09:16:59PM -0000, Rob Dixon wrote: > Steve Grazzini wrote: >> This is a documented optimization w/r/t foreach() loops, but the same >> thing applies to the foreach() modifier. > > What the code is optimised to is a touch OT, but I've never seen this > documented Steve. Can you direct me?
It's in perlop under "Range Operators". In the current implementation, no temporary array is created when the range operator is used as the expression in "foreach" loops, but older versions of Perl might burn a lot of memory when you write something like this: for (1 .. 1_000_000) { # code } It looks (in the Changelog) like "older versions" means $] < 5.004_069. > What do you think is happening under the hood for something like > > print for (0..0, 1, 2..5, func(6)..func(99)) I think the whole list would have to be generated. The optimization only applies to the case where there's just one range in place of the LIST. It's kind of tricky to demonstrate this, though. Running with -Ds would show it. And there are some bugs with constant ranges: % perl -e 'for (1,2) { print $_++ for 1..2,3..4; print "\n" }' 1234 2345 When you just have one range, it gets expanded "lazily" and produces the correct output. % perl -e 'for (1,2) { print $_++ for 1..4; print "\n" }' 1234 1234 -- Steve -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]