My Perl script launches an external program (which takes one command line parameter) and processes its output. I originally did this:
my @result = `prog arg`;
However, it turns out that the program does not work correctly and hangs unpredictably in rare cases. How can I kill a program if it did not exit after a certain time? The script should work on both Windows and Linux, and I understand that alarms and plugs do not work (or at all) on Windows.
I found a module called IPC :: Run , but I cannot figure out how to use it correctly from its documentation. :-( I tried this:
use strict;
use warnings;
use IPC::Run qw(run timeout);
my $in;
my $out;
my $err;
my @result;
my @cmd = qw(prog arg);
run \@cmd, \$in, \$out, \$err, timeout (10) or die "@cmd: $?";
push @result, $_ while (<$out>);
close $out;
print @result;
, 60 , stdout . , 60 ( 10 , -) :
IPC::Run: timeout on timer
, Proc:: Reliable. , , . , ! :
use strict;
use warnings;
use Proc::Reliable;
my $proc = Proc::Reliable->new ();
$proc->maxtime (10);
my $out = $proc->run ("prog arg");
print "$out\n";
10 . . 5 . , 10- -, , stdout $out. ! script .
, ? ( .) .