sub button { my ($w, $bg, $text, $pcmd, $ival) = @_; (defined $ival) and print "Value of \$i is $ival\n"; my $b = $w->Button(-bg => $bg, -text => $text); $b->configure(-width => 16, -command => $pcmd); $b->pack; return $b; } # ... and later, when calling button() ... push @$pbuttons, button($frame, 'skyblue', $dup, $psub, $i); # The above correctly displays each value of $i