Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Perl/Tk parameters

by stephen (Priest)
on Apr 15, 2002 at 17:06 UTC ( [id://159268]=note: print w/replies, xml ) Need Help??


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.

stephen

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://159268]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found