Here are three examples of different ways to invoke a callback from a Tk button. The first is the simplest and "cleanest", but it uses the most memory. The second is simple and more memory efficient, but it is ugly. The third takes advantage of Tk's OO system and uses the least amount of memory, but it is inflexible if improperly designed.

use strict; use Tk; use Tk::FooButton; my $MW = new MainWindow; # 1. callback using anonymous sub (also known as a # "closure") -- the arguments are embedded in # the closure. my $a_obj = new Foo (1); my $a = $MW->Button(-text => 'A', -command => sub { $a_obj->callback("pushed 'A'") } ) ->pack; # 2. callback using sub reference (looked up using # Perl's object-oriented "can" method) -- the # arguments are passed through the array reference. my $b_obj = new Foo (2); my $b = $MW->Button(-text => 'B', -command => [ $b_obj->can('callback'), $b_obj, "pushed 'B'" ] ) ->pack; # 3. callback using a custom (derived) Tk::Button -- # the arguments are stored as properties on the # button itself. my $c_obj = new Foo (3); my $c = $MW->FooButton(-text => 'C', -object => $c_obj, -message => "pushed 'C'") ->pack; MainLoop; # --------------------------------------------------------- package Foo; use strict; sub new { my ($class, $i) = @_; my $self = { -id => $i }; return bless($self, $class) } sub callback { my ($self, $message) = @_; print "$message using object #$self->{-id}\n"; } # --------------------------------------------------------- # SAVE THIS CODE AS "Tk/FooButton.pm" package Tk::FooButton; use strict; use base qw(Tk::Derived Tk::Button); Construct Tk::Widget 'FooButton'; sub Populate { my ($self, $args) = @_; $self->SUPER::Populate($args); $self->ConfigSpecs(-object => [ "PASSIVE", undef, undef, undef ], -message => [ "PASSIVE", undef, undef, undef ]) } sub invoke { my ($self) = @_; my $object = $self->cget(-object); my $message = $self->cget(-message); $object->callback($message) if ($object) } 1;


In reply to Three techniques for Tk button callbacks by blssu
in thread code ref by pg

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, details, 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, summary, 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.