$ /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
####
$ /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
####
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");
;
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";
};