in reply to Best way to handle readline errors?
I did some digging (on Windows XP, using perl 5.8.8). I didn't come up with a solution, but I thought I'd post what I found out.
I can get <$fh> to return undef by ejecting the media on which resides the file being read. close reports no error. $! is meaningless (It's "Bad file descriptor"/9 when the read is successful and when the read is not successful.)
I cannot get <$fh> (with $/ = \100) to return when the media on which resides the file being read has been ejected. It just hangs. Reinserting the media doesn't help.
I can get sysread to return undef by ejecting the media on which resides the file being read. close reports no error.
I can get read to return undef by ejecting the media on which resides the file being read. close reports "Bad file descriptor".
In all cases, I can confirm an IO error occured, since the OS pops a dialog asking to reinsert the disk once all read buffers have been emptied.
Test programs:
For <$fh>:
use strict; use warnings; my $file_name = 'G:\\I386\\DRIVER.CAB'; # 50MB file. open(my $fh, '<', $file_name) or die("Unable to open file \"$file_name\": $!\n"); # Read 100 bytes at a time. #binmode($fh); #$/ = \100; my $line; undef $!; $line = <$fh>; my $ok_defined = defined($!); my $ok_str_val = "$!"; my $ok_num_val = 0+$!; print("Eject the media then press Enter."); <STDIN>; undef $!; for (;;) { undef $!; last if not defined ($line = <$fh>); warn(length($line), "\n"); } my $bad_defined = defined($!); my $bad_str_val = "$!"; my $bad_num_val = 0+$!; close($fh) or warn("Unable to read file \"$file_name\": $!\n"); print("On success, "); if ($ok_defined) { print("\$! = \"$ok_str_val\"/$ok_num_val\n"); } else { print("\$! is not defined\n"); } print("On failure, "); if ($bad_defined) { print("\$! = \"$bad_str_val\"/$bad_num_val\n"); } else { print("\$! is not defined\n"); }
For sysread and read:
use strict; use warnings; my $file_name = 'G:\\I386\\DRIVER.CAB'; # 50MB file. open(my $fh, '<', $file_name) or die("Unable to open file \"$file_name\": $!\n"); my $buf; my $rv; $rv = sysread($fh, $buf='', 1); print("On success, "); if (defined($rv)) { print("\$rv = $rv\n"); } else { print("\$rv is not defined\n"); } print("Eject the media then press Enter."); <STDIN>; 1 while $rv = sysread($fh, $buf='', 1); print("On failure, "); if (defined($rv)) { print("\$rv = $rv\n"); } else { print("\$rv is not defined\n"); } close($fh) or warn("Unable to read file \"$file_name\": $!\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Best way to handle readline errors?
by jrw (Monk) on Nov 11, 2006 at 21:59 UTC | |
by ikegami (Patriarch) on Nov 12, 2006 at 23:06 UTC | |
by jrw (Monk) on Nov 13, 2006 at 21:24 UTC | |
|
Re^2: Best way to handle readline errors?
by jrw (Monk) on Nov 11, 2006 at 14:49 UTC |