perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
I have two snippets of code and I was wondering why they produced different results (P is like printf w/auto LF).
> perl -E ' use P; for ($_=3;$_-- >0;) { P "_=%s", $_ }' _=2 _=1 _=0
> perl -E ' use P; P "_=%s", $_ for $_=3;$_-- >0; ' _=3
Note, I didn't say "for my $i...". That would make sense. But the behavior seems like that's what it did. However, a similar construct the reversed "if", doesn't act this way:
> perl -E ' use P; $_=1; P "_=$_" if --$_; > P "got to end w/_=%s", $_; > ' got to end w/_=0 #alternatively: > perl -E ' use P; $_=12; P "_=$_" if --$_; P "got to end w/_=%s", $_; ' _=11 got to end w/_=11
This is in perl 5.16.1. Could someone explain why these look inconsistent to me? (i.e. "$_" value in the conditional statement isn't consistent with it's value in the test or outside the loop.
Thanks!Curious one.
---
FWIW -- the postfix 'while' seems to be consistent, giving the same results in regular vs. postfix form;
Back to
Seekers of Perl Wisdom