Kenneth Graves wrote:
> Let's assume our friendly fibonnacci example. Assume further that
> I create a perltk program with 6 buttons, each of which changes its
> label to the return value from fib() when clicked. The user now
> pounds on the buttons in no particular order.
> Does it matter if each button's callback had a separate anonymous
> subroutine, as opposed to all six using a reference to the same
> callback routine?
I can't recall the code for the fibonnacci example, but given:
sub next_index { my $i=0; print "$i\n"; yield; while(1){print $i++;
yield;} }
if you use this as the callback to your Tk button, it does matter
how you use it. each behaviour could be useful, depending on your need.
if you say
$mw-Button(command=>
sub next_index { my $i=0; print "$i\n"; yield; while(1){print $i++;
yield;} }
);
for each button, then each button will get its own anon sub,
so each button gets its own incrementer.
( the <Buttonx> indicates that you pressed button labeled x )
<Button1> 1
<Button1> 2
<Button1> 3
<Button2> 1
<Button1> 4
<Button2> 2
if you say:
$mw->Button(command=>\&next_index);
then all the buttons use the same incrementer.
<Button1> 1
<Button1> 2
<Button1> 3
<Button2> 4
<Button1> 5
<Button2> 6
> (If that isn't bad enough, what about threads? What about Naomi?)
using coroutines with threads will have all the same "gotchas"
as using normal subroutines with threads.
i.e. race conditions can occur as before.
Greg London
All information is on an "as-is" basis and
comes with no guarantee of its accuracy or relevancy.