how to convert shell captures into anyevent style?

Fulko Hew fulko.hew at gmail.com
Mon Apr 2 21:21:47 CEST 2012


On Fri, Mar 30, 2012 at 3:31 PM, Zsbán Ambrus <ambrus at math.bme.hu> wrote:

> On 3/30/12, Fulko Hew <fulko.hew at gmail.com> wrote:
> > I tried that (I thought correctly, as per below) and I get an error
> rather
> > than my callback being called.
> >
> > Not a GLOB reference at /usr/lib/perl5/site_perl/5.8.8/AnyEvent/Util.pm
> > line 592.
>
> I cannot reproduce your error.  Look, this works fine for me.
>

... snip ...


>  If this works for you, try to interpolate between the two to find what
> breaks.  If a minimal example like this doesn't work for you, then I
> don't know what the problem could be.
>

While waiting for further discussion on this list, I created and tested my
own sample (similar to yours) and found it to work.  I then went through
the painstaking task of trying to isolate in my module, what might be
causing the 'Not a GLOB' problem.

I've narrowed it down (but still don't understand why) to 'signals'.
In my module, I record the end-user application's signals and
insert my own handlers (to automate error recovery).

When I affect $SIG{}, I get the error.
I haven't yet been able to make a 'simple' test program that causes it,
but I see another thing that doesn't work for me.

The sample code is below.  That example also shows what I my originally
philosophy of what I was trying to do (to avoid having my app 'go away
for too long' (preventing the other AnyEvent socket handling from
occurring.))

In it I've added a signal callback for __WARN__ so that cluck() is
called (so I get a better stack trace for debugging).  When installed
the AnyEvent way, cluck() never gets called.  (When I have similar code
in my 'big module', It causes that GLOB error instead.)

It seems that AnyEvent's technique for signals doesn't (didn't)
take the perl'ish signals into account? (see answer to the 'which
version' question below.)

I also seem to be having an interaction between AnyEvent's use of
CHLD and mine (perhaps I don't need it).  In general, I was using
the traditional $SIG{} handling approach for many things, and that
is what appears to cause my GLOB issues [...but only when doing the
run_cmd()].

I'll be investigating further, but I thought I'd throw these comments and
observations out to the list.


>  Try to make sure you're using the most recent version of the AnyEvent
> module, which is version 5.14 now.  Also, what version of perl are you
> using?  You might want to upgrade that too if it's too old.
>

I've currently got AnyEvent 5.28 installed (because that's what I got
with Fedora8 (...yes I know its an old Fedora, but I have my reasons.))

------------ sample code ------------
#!/usr/bin/perl

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

my $buffer;
my $obtainer;

sub collector {
    $buffer .= $_[0] if (scalar @_);
}

sub statsCollector {
    $buffer = '';
    my $collector = run_cmd "ps -ef", ">" => \&collector;   # run a command
    $collector->recv;                       # wait it to complete
    foreach (split("\n", $buffer)) {        # process its data a line at a
time in a
#       print scalar localtime(), " $_\n";  # loop so we can keep context
info local
#       Coro::cede;                         # but let other stuff run too
    }
my $x = undef;
print "$x\n" if ($x == 5);      # cause a warning for cluck to catch
}

sub user_setup { print "AgentX user_setup called\n"; }

sub clucker {
    print "clucker called\n";
    Carp::cluck(@_);
    exit 0;
}

#$SIG{CHLD} = sub { print "got CHLD\n"; };
#my $w2 = AnyEvent->signal (signal => "CHLD", cb => sub { print "SIGCHLD
happened\n"; exit 1 });

my $w1 = AnyEvent->signal (signal => "INT", cb => sub { print "SIGINT
happened\n"; exit 1 });

#$SIG{__WARN__}  = \&clucker;
my $w = AnyEvent->signal (signal => "__WARN__", cb => \&clucker);

$obtainer = AnyEvent->timer(after => 1, interval => 5, cb =>
\&statsCollector);
AnyEvent->condvar->wait;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20120402/b7ef9ab9/attachment.html>


More information about the anyevent mailing list