note
bart
<blockquote><i>The question then is is this violation of the general prohibition safe or was I lucky</i></blockquote>
Now I am confused. What do you expect,
<ol><li>Perl loops over the <em>original</em> array, whatever you do to the array in the meantime
<li>Perl loops over the modified array
</ol>
Frankly, I have no idea what to expect, you can make a case for both behaviors. In such a case, I'd say: don't do it. What will happen in this case <em>might change over time</em>, possibly because someone decided to fix perl's "erratic behavior".
<p>What I think is safe, is this:
<ol><li>Loop over the original array: make a temporary copy.
<c>
foreach(my @temp = @arr) {
push @arr, 'd' if $_ eq 'a';
push @arr, 'e' if $_ eq 'b';
push @arr, 'f' if $_ eq 'c';
print $_;
}
</c>
<li>if you want to loop over the modified array, use the loop index:
<c>
for(my $i = 0; $i < @arr; $i++) {
local $_ = $arr[$i];
push @arr, 'd' if $_ eq 'a';
push @arr, 'e' if $_ eq 'b';
push @arr, 'f' if $_ eq 'c';
print $_;
}
</c>
</ol>
p.s. I was expecting this to work:
<c>
foreach(() = @arr) {
print;
}
</c>
but it doesn't loop even once. I think that's a bit weird, since
<c>
$x = () = @arr;
</c>
actually sets $x to the size of the array.
1023497
1023497