in reply to Re: Passing arguments to callback routines with named references
in thread Passing arguments to callback routines with named references

OK, so in other words, there is no way to use a named reference to a subroutine to supply an argument to a callback routine? You are always forced to use an anonymous subroutine as in case 1?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

  • Comment on Re: Re: Passing arguments to callback routines with named references

Replies are listed 'Best First'.
Re: Re: Re: Passing arguments to callback routines with named references
by graff (Chancellor) on Jul 15, 2003 at 04:29 UTC
    Actually, bobn is not right in this case. Your "case 2" is very close to the mark -- the right way would be:
    my $change_mode = \&change_mode; ... # Case 2: Callback routine called with argument (corrected): # given that $change_mode is a ref to a sub $main->Button(-text => 'To Upper Case', -command => [ $change_mode, 2 ] )->pack;
    The "-command" option on Tk widgets (like the "bind" method) can accept an array ref (anonymous in this case), in which the first element of the array is the subroutine reference, and the remaining elements are args to pass to the sub.

    The "bind" case is a little different, because the sub will actually get the widget handle that invoked it as the first arg every time a "bound" event triggers the sub, whereas the "-command" option does not pass the widget handle as the first arg.

      Well, that's a style I don't see everyday, but perldoc Tk::callbacks says exactly that - use the array reference and this can be done as shown.

      --Bob Niederman, http://bob-n.com
Re: Re: Re: Passing arguments to callback routines with named references
by dank (Initiate) on Jul 15, 2003 at 04:23 UTC
    You can use closures to do it:
    # generate a closure: sub gen_change_mode { my $mode = shift; return { # same code as in change_mode }; } # then do this $main->Button(-text => 'To Upper Case', -command => gen_change_mode(2) )->pack; # or this $some_ref = gen_change_mode(3); $main->Button(-text => 'To Mixed Case', -command => $some_ref )->pack;

    so in the last case gen_change_mode(3) will return a code reference with $mode set as 3, meaning you can pass this around as a code ref, which is what you're after.

    there is an example in `perldoc perlref` which uses a lexical (my) var inside the anon sub that gets returned, so you can pass more arguments to it when you finally run it as a subroutine.

Re: Re: Re: Passing arguments to callback routines with named references
by chromatic (Archbishop) on Jul 15, 2003 at 04:15 UTC

    Yes. Otherwise, it'd be Very Hard to determine automatically whether you meant a subroutine call to execute now or at some point in the future.

Re: Re: Re: Passing arguments to callback routines with named references
by bobn (Chaplain) on Jul 15, 2003 at 02:27 UTC

    I'm pretty sure that's right. There was another node about that recently.

    --Bob Niederman, http://bob-n.com