# -2 strokes:
echo www.perlmonks.org.split.reduce.code.check |perl -lF'(\.)' -E'say
+reverse@F'
# Compare to:
echo www.perlmonks.org.split.reduce.code.check |perl -lnE'say reverse
+split/(\.)/'
-F'pattern' sets -a auto-split mode with the pattern specified. The auto-split goes into @F. The -F field also sets -n automatically. So the original version and this version work essentially the same way, just relying on different ways to specify the split.
| [reply] [d/l] [select] |
$ echo www.perlmonks.org.split.reduce.code.check | perl -lF'(\.)' -E's
+ay sub{map pop,@_}->(@F)'
$
... and I can't think why at the moment. Perhaps I'm misunderstanding what you have done :-/
| [reply] [d/l] |
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.
| [reply] [d/l] [select] |
$ echo www.perlmonks.org.split.reduce.code.check | \
perl -lF'(\.)' -E'say sub{map pop,@_}->(@F)'
check.code.reduce.split.org.perlmonks.www
| [reply] [d/l] |