use strict; use warnings; use IO::Select; my $timeout = 20; my $read_set = new IO::Select(); my $pid; my $starttime = time(); my $pid = open my $fh, "-|", $command or die $!; $read_set->add($fh); while (time() - starttime < $timeout) { my ($rh_set) = IO::Select->select($read_set, undef, undef, 1); if (defined $rh_set) { # note: use sysread() instead of <> to be really safe if ($_ = <$fh>) { push @array, $_; print "+"; } else { # eof last; } } else { # nothing received for a second print "."; } } kill 9, $pid; close $fh; if (time() - starttime >= $timeout) { print "Timed out\n"; }