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

Actually, the code I posted is already sending the label to the replace subroutine (that's why it's sub { replace($_) } and not \&replace).

Just start out your replace sub like this:

sub replace { my $label = shift; ... }

bbfu
Black flowers blossom
Fearless on my breath

Replies are listed 'Best First'.
Re: Re3: Tk Menu question.
by perl_seeker (Scribe) on Oct 08, 2003 at 06:33 UTC
    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.
    :)

      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.
        :)