The select() function will tell you when there is data waiting to be read on a socket. If the connection is lost, select() will lie and say there is something to be read. When you try to read the line, sysread will return undef.
my $bit_in = '';
my $r_bit;
vec($bit_in, fileno($f), 1) = 1;
while(1) {
select($r_bit = $bit_in, undef, undef, tick_len);
# if select said there was data...
if(vec($r_bit,fileno($f),1)) {
my $in;
# connection lost if sysread returns undef
return unless $f->sysread($in, 1);
# otherwise, it's good data
$str .= $in;
last if $in eq "\n";
}
}