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") |