You can create your own class derived from Tk::Label and override the configure method.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
{
package Tk::MyLabel;
use Tk::widgets('Label');
@Tk::MyLabel::ISA = 'Tk::Label';
Tk::Widget->Construct('MyLabel');
sub configure {
my ($cw, %args) = @_;
if (my $bg = $args{-background}
|| $args{-bg}) {
print "changing background to: $bg\n";
}
$cw->SUPER::configure(%args);
}
}
my (@color, $color) = qw(blue green);
my $mw = tkinit;
$mw->packPropagate(0);
my $lab = $mw->MyLabel(
-text => "foo",
-background => $color[0],
)->pack;
my $b = $mw->Button(
-text => 'Do it!',
-command => sub {
$color = $color ? 0 : 1;
$lab->configure(-background => $color[$color]);
}
)->pack;
$mw->bind('<Control-q>', sub { Tk::exit(0) });
MainLoop;