in reply to Re: Inotify2 + AnyEvent
in thread Inotify2 + AnyEvent

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        

Replies are listed 'Best First'.
Re^3: Inotify2 + AnyEvent (naked code ref)
by zentara (Cardinal) on Oct 05, 2011 at 20:31 UTC
    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        

        I'm not going to install Tk at the moment.

        Ok, thanks for explaining. It's the () that causes premature exiting.

        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->geometry("400x200"); my $button = $mw->Button(-text => "Sub test exit", -command => \&callback(), # exits prematurely # -command => \&callback, # works ok )->pack(); MainLoop; sub callback{ print "exiting\n"; Tk::exit; } __END__

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