#!/usr/bin/perl my @b; for my $i ( 1 .. 5 ) { $b[$i] = $top->Button( -text=>$i, -command=> &mybuttons($i), ^^^^^^^^^^^^^^^^ WRONG # Stephen O. Lidie writes: # -command expects a *code reference*, that shall, at some future time, # execute. You've called for an immediate execution, which in theory could # return a code ref, but doesn't here. # Ya'all all are skrting around a closure, which may be created in various # ways, all detailed in you know what. # Try: -command => sub {&mybuttons($i)} #or maybe: -command => [\&mybuttons, $i] #### #!/usr/bin/perl -w use Tk; my $xx=99; sub a{ my $x=$_[0]; my $wm= new MainWindow; my $l=$wm->Entry(-textvariable => \$x)->pack(); $wm->Button(-text => "Apply", # -command => [ \&onApply, "$x"])->pack(); -command => [ \&onApply, \$x])->pack(); } # Here, you specify an anonymous list as the value of the -command option, # which is a legal callback syntax. The problem is that this list will be # created when the button is created, and the first element in the list is # the reference to onApply(), but the second element is the *value* of $x # at the time the list is created, which is the initial value of $x. The # variable $x itself has nothing to do with it anymore, so changing it # will not change this anonymous list. # What you can do is pass a reference to $x instead: # -command => [\&onApply, \$x], # and change your onApply() sub to reflect that. sub onApply{ # my $z=$_[0]; my $z = ${$_[0]}; print "XX=$z\n"; }; &a($xx); MainLoop();