in reply to Re3: Tk Menu question.
in thread Tk Menu question.

Hi,
thanks a lot for your reply.I tried the replace sub like this:
sub replace{ my $chosen=shift; $chosen= 'none' unless defined $chosen; print "\nThe selected word:"; print "\n$chosen"; } 1;
I'm getting this as output:
The selected word: none
which means the button label is not being sent by the main code to the replace function, is that right? what is the
problem here?

Actually I thought that $_ would simply contain the elements of @menu_items sequentially,i.e Foo, Bar,Baz,
.... Now I take it that clicking on any menu button will call replace($_), where $_ is the label of the button
on which we click.

Thanks in advance.
:)

Replies are listed 'Best First'.
Re5: Tk Menu question.
by bbfu (Curate) on Oct 09, 2003 at 21:08 UTC

    Oops. Yeah, sorry about that. I was thinking that it would create a closure, but I forgot that $_ is global so that doesn't work.

    Change the map line to:

      map { [Button => $_, -command => [\&replace, $_]] } @menu_items

    and it should work just fine with that replace sub.

    bbfu
    Black flowers blossom
    Fearless on my breath

      Hi :),
      Yes that works.Thanks a lot.
      :)