People are likely to get mixed results here. I assume it's because of this line in 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."
Why is this applicable?
Let's simplify your construct a bit. my @O = sub {map pop, @_}->(@F) is very similar to my @O = map pop @F, @F, and that is quite similar to:
my @O
foreach (@F) {
push @O, pop @F;
}
And the same rule applies, if you fiddle with the number of elements in the array Perl may get confused, or in other words "undefined behavior". The issue is that in both cases (map, or foreach), the list's size is evaluated up front before entering the loop, and that count probably remains fixed even if the size of the container you are iterating over shrinks. Then as you iterate, $_ is aliased to an element in the array. If the array shrinks, what does $_ alias to?
Your code never looks at $_, instead just relying on the fact that before entering the loop Perl decides how many times to iterate. The fact that it works for some Perl versions is somewhat good luck, and luck is not reliable.
|