I see your point now. For some reason I thought that undef value would always evalutes to false in conditional statements in perl. However, when I thought more about it and given the fact that $! is a 'synonym' for C's errno, I see no guaranty that it would be reset to some specific value if no system error occurs and may indeed contain phantom residule value that could evaluate to true. Basically, without looking through perl's code there is no way to say which statement is true.
Here is a modified version of the read_msg sub...
sub read_msg {
my ($sock, $bcount, $buf, $msg) = (shift, 0, '', '');
my $bytes_read;
# Use sysread for stream-oriented sockets
READ_MSG:
while ($bytes_read = sysread($sock, $buf, 1024)) {
# Handle partial read
$bcount += $bytes_read;
$msg .= $buf;
}
# sysread returns undef on system error, check $!
if (not defined $bytes_read) {
# Just in case, save $! value
my $error = $!;
# To be safe here...
redo READ_MSG if $error =~ /Resource temporarily unavailable/;
# All other errors trigger unconditional return with error
return ($msg, $bcount, $error);
}
# Return success
return ($msg, $bcount, '');
}
|