#!/usr/bin/perl use Tk; use strict; use warnings; my $mw = tkinit; my @buttons; for my $i(0..5){ push @buttons, $mw->Button(-text => $i, -command => sub{ print "pressed $i\n"; # or use the widget: print "pressed ", $buttons[$i]->cget('-text'), "\n"; })->pack; } MainLoop();