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

Hi,
I would like to know how to pass arguments to code reference.
I am invoking the code reference asscoiated with Tk Button Command and in that, I would like to pass arguments which should be picked by subroutine pointed by reference.
So,
Tk Code
command => \&About('program');
Sub code
sub About { my $name = shift; if($name eq 'program'){ } }
Above doesn't work..
If I remove program from Tk-Code it works..
Thanks, Artist

Replies are listed 'Best First'.
(Ovid) Re: Tk, Perl Code Reference
by Ovid (Cardinal) on Sep 28, 2001 at 00:49 UTC

    Here's a simple example:

    use strict; use warnings; use Tk; my $top = MainWindow->new; # use a frame widget as a container for menu buttons my $menu_bar = $top->Frame->pack( side => 'top' ); # Search menu button my $search_mb = $menu_bar->Menubutton( text => 'Search', relief => 'raised', borderwidth => 2 )->pack( side => 'left', padx => 2 ); # Find menu buttons $search_mb->command( -label => 'Find', accelerator => 'Meta+F', underline => 0, command => [\&whoami, 'Find'] ); $search_mb->command( -label => 'Find Again', accelerator => 'Meta+A', underline => 0, command => [\&whoami, 'Find Again' ] ); MainLoop(); sub whoami { my $var = shift; print $var; }

    Whenever you click on a menu option, the associated variable will be printed in the console window. This should give you some ideas.

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

(ichimunki) Re: Tk, Perl Code Reference
by ichimunki (Priest) on Sep 28, 2001 at 00:38 UTC
    You could cheat (I'll call it that in case someone has a better method).
    $object->widget( ... , command => sub{ About( 'program' ); }, ... );