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 = '';
vec($bit_in, fileno($f), 1) = 1;
select($r_bit = $bit_in, undef, undef, tick_len);
# if select said there was data...
# connection lost if sysread returns undef
return unless $f->sysread($in, 1);
# otherwise, it's good data
$str .= $in;
last if $in eq "\n";