Argel has asked for the wisdom of the Perl Monks concerning the following question:

I was looking at the Non-buffered read from program thread and thinking that using an eval block and alarm would work. However, when the alarm triggers any output from the command is lost. Is there a way to catch the output regardless? I assume this has something to do with signal handling?

eval.pl:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @return; eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm 5; @return = `./test.pl`; alarm 0; }; if( $@ ) { # CASE: Command timed out die unless $@ eq "alarm\n"; # propagate unexpected errors } else { # CASE: Did *not* timeout } print Data::Dumper->Dump([\@return],['return']);
test.pl
#!/usr/bin/perl print "Foo\n"; sleep 2; print "Bar\n"; sleep 3; print "Foobar\n";
output
$return = [];

-- Argel

Replies are listed 'Best First'.
Re: Saving output when alarm goes off?
by BUU (Prior) on Dec 30, 2005 at 22:47 UTC
    Yes, see open or IPC::Open2. Backticks only return output when the entire program has finished executing.