Thanks to @zdim! You need to install a signal handler for the BREAK
signal. The following works:
use strict;
use warnings;
use feature qw(say);
use Data::Dumper;
use IPC::Run qw(run timeout);
my $timeout = 3;
my $cmd = ['perl', '-E', "sleep 4; say 'stdout_text'; say STDERR 'stderr_text'"];
my $in;
my $out;
my $err;
my $result;
{
local $SIG{BREAK} = sub { die "Got timeout signal" };
eval {
$result = run $cmd, $in, $out, $err, timeout($timeout );
};
}
if ( $@ ) {
say "Timed out: $@";
}
else {
print Dumper({ out => $out, err => $err});
}
Output:
> perl p.pl
Timed out: Got timeout signal at p.pl line 14.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…