and here is a much cleaner solution with IO::Select and a timeout:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
use Time::HiRes qw[ time ];
use POSIX ":sys_wait_h";
use IO::Select;
# if command ended, do not keep it because that makes it zombie
# this or reap it using waitpid below
#$SIG{CHLD} = 'IGNORE';
my $timeout = 3;
my $st=time;
my $Command = qq(echo 'Hallo, sleeping for 12 seconds ...'; sleep 12);
my $COMMANDFH;
my $pid = open ($COMMANDFH, '-|', "$Command");
my $select = IO::Select->new();
$select->add($COMMANDFH);
print "ran command with pid=$pid\n";
my $iters = 0;
WH:
while (++$iters) {
last WH if waitpid $pid, WNOHANG;
my @ready = $select->can_read($timeout);
print "no output for the last $timeout seconds ...\n" unless scala
+r @ready;
foreach(@ready){
last WH if waitpid $pid, WNOHANG;
print "command got: ".<$_>."\n";
}
sleep 1;
}
print "command has ended after ".(time-$st)." seconds.\n";
bw, bliako