#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; my %button; foreach my $num (0..9){ $button{$num} = $mw -> Button( -text => $num, -background => 'grey', -width => 5, -height => 2, # -command => $number=$number==0||$equals!=0?$input:$number.$input, -command => sub{ print "$num\n"; #do whatever here } ) -> pack(); } MainLoop;