Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
"be consistent"

Inotify2 + AnyEvent

by dlal66 (Acolyte)
on Oct 04, 2011 at 16:09 UTC ( #929591=perlquestion: print w/ replies, xml ) Need Help??
dlal66 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have a perfectly working perl/Tk script that we use for network management. It runs on Centos 5.6 and perl version is 5.8.8 I installed a new PC running Centos 5.7 and perl 5.8.8. I made sure all perl modules the script needs are installed. But I keep on getting the following error on the new machine:
Unable to create sub named "" at /usr/lib/perl5/site_perl/5.8.8/i386-l +inux-thread-multi/ line 1488.
The lines in the script relevant to this are:
my $inotify = new Linux::Inotify2 or die "unable to create new inotify + object: $!"; # add watchers --watch the snmptrapd.log file $watchObject = $inotify->watch ("/var/log/snmptrapd.log", IN_MODIFY, \ +&processTrap); $inotify_w = AnyEvent->io ( fh => $inotify->fileno, poll => 'r', cb => sub { $inotify->poll ; }, ); MainLoop;
The sub processTrap is defined.If I comment the
$inotify_w= AnyEvent->io (...
line, the code does not give an error and quit. But obviously I need that line. Any wisdom is appreciated...

Comment on Inotify2 + AnyEvent
Select or Download Code
Re: Inotify2 + AnyEvent
by rcaputo (Chaplain) on Oct 04, 2011 at 16:19 UTC
Re: Inotify2 + AnyEvent
by zentara (Archbishop) on Oct 04, 2011 at 17:20 UTC
    Just a guess from knowing how finicky calling subs by reference sometimes can be. Instead of
    $watchObject = $inotify->watch ( "/var/log/snmptrapd.log", IN_MODIFY, \&processTrap);
    $watchObject = $inotify->watch ( "/var/log/snmptrapd.log", IN_MODIFY, &processTrap() ); or "/var/log/snmptrapd.log", IN_MODIFY, processTrap() ); or "/var/log/snmptrapd.log", IN_MODIFY, sub{ processTrap() } );
    anyways, experiment around with the way you call that sub. In the Ionotify2 docs, the examples show .... sub{ } notation, and I know Tk can get very fussy about the way subs are called. One tip, if you use &, include the ().

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      I'd use \&processTrap. The sub { procesTrap() } should also work, but I don't recommend using that with Tk because I find that it can leak memory (and I don't know if Inotify2 avoids that problem). The other two things you suggested don't pass in a reference to code to be called later; they just call the code right then and then pass in whatever that code returns.

      - tye        

        Thank you gentlemen for replying. I tried the suggestions provided but they did not work. So I checked and saw that the script was working on the machine that had AnyEvent version 5.34 installed. I checked the version of AnyEvent on the machine that has the issue and saw that it was running version 7.02 of AnyEvent. So I downloaded the version 5.34 of AnyEvent and after installion, my application is running fine on the new machine. So I guess the version 7.02 introduced a bug. Thanks again for your replies.
Re: Inotify2 + AnyEvent
by zentara (Archbishop) on Oct 05, 2011 at 09:44 UTC
    To amplify with a code example, look at this Tk code. When the code ref for the button callback is left hanging naked, it gets executed at creation time. I suspected that is what was happening in your Ionotify2 code, the naked coderef was been acted upon at creation time, giving the error can't find "".
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->geometry("400x200"); my $button = $mw->Button(-text => "Sub test exit", # this naked coderef is incorrect, as it causes # the callback to be executed at button creation # time, instead of later at button press -command => \&callback(time), # below is correct method, put [] around coderef # -command => [ \&callback, time ], )->pack(); MainLoop; sub callback{ my $in = shift; print "$in exiting\n"; Tk::exit; } __END__

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      No, \&callback(time) is not a "naked code ref". Yes, the similar-looking \&callback is a code ref, but adding (time) on the end changes the parsing to \(&callback(time)), that is, a call to callback(time) but ignoring prototypes and then returning reference(s) to the returned scalar value(s).

      - tye        

        I know you are smarter than me about this stuff, but even without adding (time), to the coderef
        # -command => \&callback(time), -command => \&callback,
        still executes the sub callback at button creation time, causing a pre-mature exit, and crash.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://929591]
Approved by Perlbotics
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-04-19 01:18 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (475 votes), past polls