in reply to Perl/Tk parameters

Actually, the problem has nothing to do with Tk per se. What you're trying to do is pass Tk a reference to a subroutine to call later on. Unfortunately, you're trying to pass arguments at the same time, which means that Perl thinks you're telling it to call the subroutine immediately at runtime and pass to Tk a reference to what the subroutine returns. So if I did this:

use strict; sub vert { my ($arg) = @_; print "Arg is $arg\n"; return 'text'; } # Correct way of creating subroutine reference my $sub_ref = \| # Will print "Arg is foo" &$sub_ref('foo'); # Incorrect attempt at creating a subroutine reference # Will print "Arg is bar" as a side effect my $not_sub_ref = \&vert('bar'); # Will die horribly, since $not_sub_ref is a reference # to the string 'text' (the return value of &vert), # not a reference to a subroutine &$not_sub_ref();

To get a reference to a subroutine that will call queryDB with the right parameters when you click the button, do this:

#untested $action->command(-label=>"Sort by Number",-command=> sub { &queryDB("i +tem_number") }); $action->command(-label=>"Sort by Sales",-command=>sub { &queryDB("tot +_sales") });

When you say sub { ... } without naming the subroutine, perl creates an anonymous subroutine and returns the reference. See perlman:perlref and perlman:perlsub for more information.


Update: Added additional explanaton.

Replies are listed 'Best First'.
Re: Re: Perl/Tk parameters
by Anonymous Monk on Apr 15, 2002 at 17:55 UTC
    Absolutely brilliant! Thank you both very much for the kind explanations. I don't use references often, so this stuff never occurred to me. This problem was driving me nuts all last week. Thanks again!