in reply to Perl/Tk Menus

Your problem is that $fn isn't a lexical variable --- you'll want to use my() so the anonymous subroutine acts as a closure around each lexical $fn. See the difference between these two snippets:

my @subs; foreach $fn (1,2) { push @subs, sub{print "$fn\n"}; } $fn = 42; foreach $sub (@subs) { $sub->(); } @subs = (); foreach my $fn (1,2) { push @subs, sub{print "$fn\n"}; } $fn = 42; foreach $sub (@subs) { $sub->(); }

Replies are listed 'Best First'.
Re: Perl/Tk Menus
by Clachair (Acolyte) on Jun 09, 2001 at 09:14 UTC
    Well, that was a simple fix for a very vexing problem. Many thanks for pointing me in the right direction.