Working on the basis of $! is "russian-roulette" with you guaranteed to lose everytime, it's value can be anything when the expression it is associated with succeeds.
$ perl -le 'print $c++ for 0..0xFFFF' > test; ls -l test
-rwxr-xr-x 1 user user 382106 2006-11-14 15:04 test
$ perl -e 'open F, "test"; print "$! => $_" while <F>'
Bad file descriptor => 0
Bad file descriptor => 1
Bad file descriptor => 2
...
Infact, undefining $! has no effect and
perlvar does say "the value of $! is meaningful only immediately after a failure". Like $^E, its useful for one thing only - describing the error that occured. Programming around it would definitely be considered "bad practice".
Just use defined or eval to test the success of readline().
#!/usr/bin/perl -Wl
use strict;
use warnings;
$|++;
open F, "<", $ARGV[0];
while (1) {
last if eof(F);
if (defined chomp( local $_ = readline (F) )) {
print
} else {
warn "hmerrm, readline failed : $!";
last;
}
}
# or
while (1) {
last if eof(F);
chomp( local $_ = eval "readline (F)" );
unless ($@) {
print;
} else {
warn "whoops, readline failed : $@";
last;
}
}
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.