in reply to Re: Best way to handle readline errors?
in thread Best way to handle readline errors?

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"; };

Replies are listed 'Best First'.
Re^3: Best way to handle readline errors?
by ikegami (Patriarch) on Nov 12, 2006 at 23:06 UTC

    All three versions read 99Mb after the disk was ejected, indicating that WinXP must be caching the CD file

    It did that to me when I used Alcohol to mount and unmount a virtual CD instead of ejecting a real CD. Apparently, Alcohol doesn't fully unmount an image with an open file handle.

    When I switched to using a real CD, WinXP would prompt me to reinsert the disk when all cached data had been read. When I Cancel that dialog, Perl returns an error.

Re^3: Best way to handle readline errors?
by jrw (Monk) on Nov 13, 2006 at 21:24 UTC
    So, can anyone help with my original question? I'm sure I'm not the only perl person who is concerned with thorough error handling in code where error handling is important (such as code run by root).

    The original question was: what is the Perl Best Practice idiom for handling readline errors?

    Being a new poster to PerlMonks, but not a new perl user, is there anything I can do or any way I can help to get this question answered? Anything I can do to get more attention focused on this question?