ikegami, I liked your approach (ejecting a CD to cause a read error). So, here are my results which are somewhat different than yours: The results, run on WinXP cygwin using ActiveState Perl for Windows v5.8.7:
$ /opt/perl/bin/perl -v This is perl, v5.8.7 built for cygwin-thread-multi-64int ... $ /opt/perl/bin/perl ttt sysread msg 1: rc=1 Eject the media then press Enter msg 2: cnt=99800000 rc=undef: 0: $ /opt/perl/bin/perl ttt read msg 1: rc=1 Eject the media then press Enter msg 2: cnt=99806953 rc=undef: 9: Bad file descriptor Close failed: 9: Bad file descriptor $ /opt/perl/bin/perl ttt readline msg 1: len=5 Eject the media then press Enter msg 2: cnt=99809957 rc=undef: 9: Bad file descriptor
Here are the results when I set $initial_read to 0:
$ /opt/perl/bin/perl tttt sysread Eject the media then press Enter msg 2: cnt=0 rc=undef: 0: $ /opt/perl/bin/perl tttt read Eject the media then press Enter msg 2: cnt=0 rc=undef: 9: Bad file descriptor Close failed: 9: Bad file descriptor $ /opt/perl/bin/perl tttt readline Eject the media then press Enter msg 2: cnt=0 rc=undef: 9: Bad file descriptor
Here's my test code:
use strict; use warnings; my $initial_read = 1; $| = 1; my $arg = $ARGV[0]; die "usage: $0 [read|readline|sysread]\n" unless @ARGV == 1 && defined $arg && $arg =~ /^(?:read|readline|sysread)\z/; my $file_name; $file_name = "d:/sourceforge-4_3-hf5-dl3.zip"; my ($fh, $buf, $rc, $bangn, $bangs, $prt); open $fh, '<', $file_name or do { $bangn=$!+0; $bangs=$!; die "Unable to open file $file_name: $bangn: $bangs\n"; }; if ($initial_read) { undef $!; if ($arg eq "read") { $rc = read $fh, $buf, 1; } elsif ($arg eq "sysread") { $rc = sysread $fh, $buf, 1; } else { $rc = readline $fh; } $bangn=$!+0; $bangs=$!; $prt = $arg eq "readline" ? "len=@{[length $rc]}" : "rc=$rc" if defined $rc; print "msg 1: ", defined $rc ? "$prt\n" : "rc=undef: $bangn: $bangs\n"; } print("Eject the media then press Enter"); <STDIN>; my $cnt = 0; undef $!; if ($arg eq "read") { while ($rc = read $fh, $buf, 100000) { $cnt += $rc; } } elsif ($arg eq "sysread") { while ($rc = sysread $fh, $buf, 100000) { $cnt += $rc; } } else { while (defined($rc = readline $fh)) { $cnt += length $rc; } } $bangn=$!+0; $bangs=$!; $prt = $arg eq "readline" ? "len=@{[length $rc]}" : "cnt=$cnt rc=$rc" if defined $rc; print "msg 2: ", defined $rc ? "$prt\n" : "cnt=$cnt rc=undef: $bangn: $bangs\n"; close $fh or do { $bangn=$!+0; $bangs=$!; die "Close failed: $bangn: $bangs\n"; };

In reply to Re^2: Best way to handle readline errors? by jrw
in thread Best way to handle readline errors? by jrw

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.