END block ignored while run_cmd() is executing

Fulko Hew fulko.hew at gmail.com
Mon Apr 9 17:14:07 CEST 2012

My next test after moving from using backticks to run_cmd()
for executing and capturing external data has uncovered
another side effect that I don't know how to get around.

"Perl END blocks are not run if run_cmd(stuff) is executing."

In the following code, I try to capture CTL C to perform
some clean-up, and I also want my module's END block to
get executed.  Now that I've moved to using run_cmd(),
it seems that while run_cmd() is executing... if I
(for example) send it a INT signal, the INT signal is
caught, but the END block doesn't get executed.  If I
wait for the run_cmd() to finish and then press CTL C,
the END block _is_ executed.

Comments or suggestions are welcome.


------------- code example starts -------------


use strict;
use warnings;
use Coro;
use AnyEvent;
use AnyEvent::Util;

END { print "INSIDE END BLOCK\n"; }

sub make_catcher {
    my $s = shift;
    return sub { signal_catcher($s); };

sub signal_catcher {
    my ($name) = @_;

    print "\ncatcher called with '$name'\n";
    exit if $name eq 'INT';

sub get_cmd_data {
    my ($cmd) = @_;

    my $buffer = '';
    my $ps_cv = run_cmd $cmd, ">" => sub {$buffer .= $_[0] if (scalar @_);};
    return \$buffer;

sub obtainer {

    print "END not accessible during run_cmd()... wait 5 seconds till its
    my $bufRef = get_cmd_data("sleep 5");
    print "run_cmd() done, END is now reachable on CTL C\n";

my $mysig =  AnyEvent->signal (signal => 'INT', cb => make_catcher('INT') );
my $obtainer = AnyEvent->timer(after => 1, interval => 15, cb =>

------------- code example ends -------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20120409/5ad79d3c/attachment.html>

More information about the anyevent mailing list