The actual problem is when the last line of the file contains only a zero and no trailing newline. And that case is even a problem for the latest version of Perl.

Update: chipmunk points out that Perl5.005 patched this. I never thought to search for such a patch as it seemed like "too much magic" to me. :)

I don't think I like this patch since it doesn't extend to very similar situations like the second while below:

use IO::Handle; while(my $line= <DATA>) { my $n= chomp($line); print "$n($line)\n"; } seek(DATA,0,0); print $/; while(my $line= DATA->getline()) { my $n= chomp($line); print "$n($line)\n"; } __DATA__ A line 0
If you save the above file and remove all trailing newlines from the last line (Use Notepad under Windows. Under Unix, delete the last line then use something like 'echo -c "0" >>getline.pl', depending on your shell.), then the last line output by the first loop will be "0(0)" while the last line output by the second loop will be "1(A line)". (If you get output of "1(0)", then you didn't remove all trailing newlines successfully.)

I liked the old idiomatic while(<STDIN>) and I liked it even more when it was improved to use defined. But I don't see where we should draw the line for this type of patch. I'd rather have drawn it back where it was since I think this change is likely to encourage mistakes in similar code that doesn't trigger this patch. Now that it has shifted this far, I think it needs to shift even further. |:

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Reading a pipe by tye
in thread Reading a pipe by mpolo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.