sub watch_variable { my( $mainwindow, $scalarref, $coderef ) = @_; $mainwindow->Entry( -textvariable => $scalarref, -validate => 'all', -validatecommand => sub { $coderef->( $_[0] ); 1 }, ); } #### use Tk; use strict; use warnings; my $mw = new MainWindow; my $var; watch_variable( $mw, \$var, sub { warn "val = $_[0]\n" } ); $mw->Button( -text => "Incr", -command => sub { $var++ } )->pack; $mw->Button( -text => "Set 5", -command => sub { $var = 5 } )->pack; $mw->Button( -text => "Exit", -command => sub { exit } )->pack; MainLoop; sub watch_variable { my( $mainwindow, $scalarref, $coderef ) = @_; $mainwindow->Entry( -textvariable => $scalarref, -validate => 'all', -validatecommand => sub { $coderef->( $_[0] ); 1 }, ); }