[ don't top post. don't quote sigs. read and follow the posting
guidelines already ]
I have a lot of buttons and each button have different function call
(once clicked).
I created a generic sub routine to create the button and grab inputs
from method call.
Now I want to pass in a "sub routine" so this button will execute the
right method call once a button is click.
You still haven't explained what exactly is giving you a problem and why
you are using prototypes.
I was assuming you understood what function arguments are etc in my
original response but it looks like that's where the problem lies.
So, is this (not tested) really what you are asking for?
sub add_button {
my ($text, $row, $col, $cmd) = @_;
$main->Button(
-text => $text,
-command => $cmd,
)->grid(
-row => $row,
-column => $col,
);
}
What??? I'm not sure you understood his pigeon Engrish. His example code clearly
portray's his intended question. He's looking for a single *pass-through* function
handler for all the buttons he creates in a particular group of buttons.
That is the \&function_pass handler. In the add_button function he is instantiating
a new button. Each call is assigning the same button handler for every new button.
In the add_button function, he is trying to add a parameter to be passed to a common
handler. In this case its a reference to a function. But it could have been anything,
like a pseudo button ID.
When instantiating a Button, the -command parameter expects either a code ref or an
array ref. The latter must have a code ref as the first parameter, then any number of
user, pre-defined arguments that are passed to that function. This emulate's
a sophisticated event driven OS, gui or otherwise, and is just an emulation.
Sometimes develpers want to 'pre-filter' events instead of asigning unique handlers
to each events. Sometimes there is a range of events that need action en-mass like
disabling or enabling or other factored out chores.
This is quite common in event driven OS applications.
-command => $code_ref or [$code_ref, $arg1, $arg2, ...],
And it is quite common to have a single handler for many buttons, needing only the
button ID (pseudo in this case).
I think you are looking at the trees instead of the forest.
There are numerous uses for a common handler in event driven app's.
Even primitive Tk apps.
------------------------
sub function_pass
{
my ($func, $id) = @_;
# do common activities
# ...
# call this specific handler
my $result = $func->();
# do specific activities
# if ($id == 2 && !$result)
# { .. disable a group of controls .. }
}
sub add_button
{
my ($text, $row, $col, $handler, $control_id) = @_;
# instatiate button, set grid
$main->Button (
-text => $text,
-command => [\&function_pass, $handler, $control_id]
)->grid(
-row => $row,
-column => $col,
);
}
sub test_a
{
}
sub test_b
{
}
add_button('test a', 1,0, \&test_a, 1);
add_button('test b', 1,1, \&test_b, 2);