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.


In reply to Re: Perl/Tk parameters by stephen
in thread Perl/Tk parameters by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.