sub gets { my $timeout; my($str, $c, $rin, $rout, $found, $timeleft); #be really pessimistic about how long it will take for the fist char $timeout = 120; #time in seconds $rin = ''; vec($rin, fileno(COM), 1) = 1; while (1) { ($found, $timeleft) = select($rout=$rin, undef, undef, $timeout); #look for input for up to $timeout seconds last unless $found; #give up if nothing was found $timeout = $timeout - $timeleft; $timeout ||= 15; #the $timeout must be >= 15 seconds sysread(COM, $c, 1); #read the next char if (($c ne "\0")&&($c =~ /[\w\*\-\t\:\/ ]/)) { #sanitize the data $str .= $c; #put the char at the end of the string } elsif ($c =~ /\r/) { #Note: must only match \n OR \r. Matching both does odd stuff last; } } return $str; }