I mentioned this at TPC5 BOF but never followed up. I haven't
considered the best fix, so I'll take the unusual step of not
posting a patch.
The bug is that FOREACH loops are not robust to modifying the FOREACH
array inside the loop. The loop count is set once at the start of the
loop (since this is copied by get_first to the Template::Iterator
object), but the values behave correctly (until the array is exhausted).
For example, this loop:
[%
x = [1..6];
cnt = 1;
FOREACH i = x;
"loop $cnt: i = $i\n";
CALL x.shift;
cnt = cnt + 1;
END;
-%]
prints:
loop 1: i = 1
loop 2: i = 3
loop 3: i = 5
loop 4: i =
loop 5: i =
loop 6: i =
when I claim it should produce just 3 loops:
loop 1: i = 1
loop 2: i = 3
loop 3: i = 5
I should note that modifying the array inside the loop isn't something
you normally do, but I came across this when I was wanting a quick way
to skip odd elements of an array. The same loop works correctly in perl
(the loop runs 3 times), despite this warning from perlsyn:
If any part of LIST is an array, `foreach' will get very
confused if you add or remove elements within the loop
body, for example with `splice'. So don't do that.
The bug could be documented in a similar way for TT, but it would be
better to devise a way for Template::Iterator to handle this case
correctly.
Craig