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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why/how are these different?
by Athanasius (Archbishop) on Jan 31, 2013 at 03:22 UTC | |
by perl-diddler (Chaplain) on Jan 31, 2013 at 04:40 UTC | |
by Athanasius (Archbishop) on Jan 31, 2013 at 06:46 UTC | |
by perl-diddler (Chaplain) on Jan 31, 2013 at 19:41 UTC | |
by AnomalousMonk (Archbishop) on Jan 31, 2013 at 08:51 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |