sub read_msg { my ($sock, $bcount, $buf, $msg) = (shift, 0, '', ''); # Use sysread for stream-oriented sockets READ_MSG: while (my $bytes_read = sysread($sock, $buf, 1024)) { # Handle partial read $bcount += $bytes_read; $msg .= $buf; } # Just in case redo READ_MSG if $! =~ /Resource temporarily unavailable/; # All other errors trigger unconditional return with error return ($msg, $bcount, $!) if ($!); # Return success return ($msg, $bcount, ''); }