in reply to Tk bind mouse down question

Why it doesn't work is explained nicely by dave_the_m in a related post Binding TK events.
\&mysub creates a reference to the sub mysub;
\&mysub(...)
calls mysub, then creates a reference to the returned value
and by davidj in the same post
"When you want to pass arguments to a callbakc, specify an array reference, with the callback code reference as the first element and the callback arguments as the subsequent array elements" In other words: instead of
$input->bind('<Return>', [\&mysub($arg)]);
you need to have
$input->bind('<Return>', [\&mysub, $arg]);
Thus... what happens is instead of binding my_test to 'can', you are executing 'my_test' and binding the result to 'can'. This happens I guess in the main_loop.

UPDATE: Fixed format of quoted text

Replies are listed 'Best First'.
Re: Re: Tk bind mouse down question
by Plankton (Vicar) on Jun 01, 2004 at 15:58 UTC
    Okay so I changed my code like so ...
    #$can->bind( \$can, '<Button-1>', \&my_test( $root ) ); $can->bind( '<Button>', [ \&my_test, $root ] );
    ... now I get this error message ...
    $ ./bind_test.pl at c:/Perl/site/lib/Tk.pm line 228.
    Did I misunderstand something?
    Thanks

    Plankton: 1% Evil, 99% Hot Gas.
      Try this:
      $top->bind($can, '<Button-1>', sub{my_test(root)});