my $button = $mw->Button(-text => $label);
####
$button->configure(-command => [ \&show_button_label, $button ]);
-or-
$button->configure(-command => sub { show_button_label($button) });
####
#!/usr/bin/perl
# Libraries
use strict;
use warnings;
use Tk;
# User-defined
my @button_labels = (qw(
Red Blue Green White Yellow Orange
));
# Main program
my $mw = new MainWindow();
foreach my $label (@button_labels) {
# Note labels are colors, so they can be used for the background
my $button = $mw->Button(-text => $label, -bg => $label);
$button->configure(-command => [ \&show_button_label, $button ]);
$button->pack(-side => "left");
}
MainLoop();
# Subroutines
sub show_button_label {
my ($button) = @_;
my $label = $button->cget(-text);
print "Label for button '$button' is '$label'\n";
}