As pointed out, on the left operand of the or you have scalar context. List context propagates from the "for"
onto the right operand of or. Now the catch: .. is two
completely different operators in list vs. scalar context.
In scalar context, it is the flipflop operator, and the constants 1 and 10 are implicitly compared to $. (the current readline input number.) In list context, it generates a list from the specified range. So what your
code produces is something like:
my $flipflopcount;
print for do {
my $flipflop;
if (!$flipflopcount && $. == 1) {
$flipflop = ++$flipflopcount;
}
if ($flipflopcount && $. == 10) {
$flipflop = ++$flipflopcount . "E0";
undef $flipflopcount;
}
if ($flipflop) {
$flipflop;
} else {
(1.11);
}
}
Try this:
perl -wne'print for (1..10 or 1..11)' foo
where foo is a file with 12 or so lines. You will get "12345678910E0" as the output for the first 10 lines and "1234567891011" as the output for each line thereafter.
To the monk confused by the difference between scalar(1..10) and scalar 1..10, that's a precedence thing. The latter is equivalent to scalar(1)..10 so the scalar() is not forcing .. to have scalar context.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.