in reply to Inotify2 + AnyEvent

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

Replies are listed 'Best First'.
Re^2: Inotify2 + AnyEvent (naked code ref)
by tye (Sage) on Oct 05, 2011 at 16:08 UTC

    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

        I'm not going to install Tk at the moment. But, \&callback does not call callback() (it just provides a code ref):

        $ perl -le 'sub callback { die "called( @_ )!" } print \&callback' CODE(0x219b4c0) $ perl -le 'sub callback { die "called( @_ )!" } print \&callback("ar +g")' called( arg )! at -e line 1. $

        The Tk::Button documentation (see also Tk::callbacks) implies that -command => $codeRef will not immediately call the referenced sub.

        There certainly could be other reasons for that usage to cause "pre-mature exit and crash". For example, -command => \&callback, time, would likely "crash".

        I see plenty of evidence to suggest that it will not cause callback() to be called at button-creation time and no details suggesting that you have enough evidence to support that one part of your claim.

        - tye