Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I'm trying to add to a Tk pop-up menu a list of options using an array.

sub add_edit_popup { my ($mw, $obj) = @_; my $menu = $mw->Menu(-tearoff=>0, -menuitems=>[ [command=>'Start Term Info', -command=>[\&get_term_info_from_col_1 +_partial, $obj,]], '', [qw/command Copy/, -command=>['clipboardCopy', $obj,]], [qw/command Paste/, -command=>['clipboardPaste', $obj]], '', [command=>'Select All', -command=>[ sub { $_[0]->selectionRange(0, 'end'); }, $obj, ]], [command=>'Unselect All', -command=>[ sub { $_[0]->selectionClear; }, $obj, ]], '', [ Cascade => 'Insert symbol', -tearoff => 0, -menuitems => [ foreach my $symbol (@list_symbols) { [command => "$symbol",-command => sub{inser +t_symbol($symbol)}]; } ], ], ]); $obj->menu($menu); $obj->bind('<3>', ['PostPopupMenu', Ev('X'), Ev('Y'), ]); return $obj; }

I'm encountering problem with the foreach loop, which works fine in a normal menu widget. Waht is wrong with it?

Replies are listed 'Best First'.
Re: tk popup menu foreach
by Loops (Curate) on Aug 04, 2013 at 18:14 UTC

    Hi there,

    Am sorry that I don't understand how to make that foreach loop work, but this does:

    -menuitems => [ map { [ command => "$_", -command => sub { insert_symbol($_) } +] } @list_symbols ],