title: Perl attempt to read line from filehandle without blocking
body: <p>I am writing a simple utility that tests the round trip time to a few hosts and picks the one that's fastest on average. However, doing this serially instead of in parallel takes a while, so I am spawning one ping process per host and reading from them. For my use case the <code>scalar &lt;$fh&gt;</code> operation which blocks until the line is read is good enough, since I can't avoid waiting until the slowest host times out. But if I want to get just the fastest <em>k</em> hosts and don't care about the rest this is a bad approach. Is there a way to attempt to read a complete line with a certain time limit, and fail without consuming any bytes if the time limit passes?</p>
  
  <pre><code>use strict;
  use warnings FATAL =&gt; 'all';
  
  # generate command to get average 
  # round trip length to hostname.
  # we know in advance that each command only
  # emits one line
  sub command {
      my $host = shift;
      qq(ping -w 1 -c 5 '$host' |) . 
      q(tail -n 1 |) .
      q(sed 's/^.*= //;s/ ms//' |) .
      q(awk -F '/' 'END {print $2}');
  }
  
  # launch each ping in its own process and create
  # a filehandle that reads from it
  sub filehandles {
      my @handles;
      for my $command (@_) {
          open my $fh, '-|', $command or
          die "cannot open file ($!) associated with $command";
      push @handles, $fh;
      }
      @handles;
  }
  
  my @commands = map command($_), qw[google.com cnn.com wikipedia.org];
  my @fhs = filehandles(@commands);
  
  # read line from each of the ping processes sequentially
  for my $fh (@fhs) {
      my $line = scalar &lt;$fh&gt;;
      chomp $line;
      printf "%s\n", $line || '+INF';
  }
  </code></pre>