use strict; use warnings; use Tk; my @files = ("File1.txt", "File2.txt", "File3.txt"); #in real life, this might not be fixed my @buttons; my $main = MainWindow->new(); my $load = $main->Button(-text => "Load", -command => \&load_buttons)->pack(); my $unload = $main->Button(-text => "Unload", -command => \&unload_buttons)->pack(); MainLoop; sub load_buttons { for my $file (@files) { push @buttons, $main->Button(-text => "Open $file", -command => sub{`notepad $file`})->pack(); } } sub unload_buttons { while (@buttons) { my $button = pop @buttons; $button->destroy(); } }