First of all, don't use numbered variables. Use an array. Then you can use map to generate the anonymous array elements. (Oh, I renamed a few of your variables / functions to something more self-documenting.)
#!/usr/bin/winperl use warnings; use strict; use Tk; my @menu_items = splice @{[get_menu_items()]}, 0, 3; @menu_items = ('None') unless @menu_items; my $mw = MainWindow->new(); $mw->Label(-text => 'Right-click for menu...')->pack(); my $m = $mw->Menu(-tearoff => 0,font => "{arial} 12 {bold}",-menuitems + => [ map { [Button => $_, -command => sub { replace($_) }] } @menu_items ]); $mw->bind("<Button-3>" => sub { $m->Popup(-popover => "cursor",popanch +or => 'nw') }); $mw->focus(); MainLoop; sub get_menu_items { return qw(); # return qw(Foo Bar Baz); # return qw(Foo Bar Baz Quux Zod Wibble); }
The code to populate @menu_items is a little obfuscated, but not terribly. You could break it up into an if-else. *shrug*
bbfu
Black flowers blossom
Fearless on my breath
In reply to Re: Tk Menu question.
by bbfu
in thread Tk Menu question.
by perl_seeker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |