Perl/Tk: recursive "after" method issue
holli
created: 2006-02-02 16:22:23
Monks,

I have just bought Mastering Perl/Tk and play with some examples. While doing that I stumbled over the "after" method and wrote the following code:
use Tk;

my $mw = MainWindow->new();

my $level = 0;

my $id = $mw->after (1, \&_after);

sub _after
{
    if ( $level < 5 )
    {
        $level++;
        my $id = $mw->after (1, \&_after);
    }
    else
    {
        $level--;
    }

    print "$level";
}


MainLoop;

1;
The ouput of this in my system (Win32, Perl 5.8) is "123454" and not "123454321" as I expect. Also the program seems to be still busy. There is an hourglass cursor.

I'm seeking for enlightment why the program doesn't behave as I expect it to.


holli, /regexed monk/
Re: Perl/Tk: recursive "after" method issue
created: 2006-02-02 16:38:50
The reason this is happenening is that after isn't recursive. What calling after does is tell the Tk MainLoop, once it next receives control, to run the specified subroutine after the specified amount of time. That is to say, when your _after sub is called the second time, the first iteration has already completed, having printed the value "1", and so on.
Re: Perl/Tk: recursive "after" method issue
created: 2006-02-02 16:41:41
The after method only executes once. So you count up to 5, each time reiniting the timer. The 6th time, you decrease level, but don't reinit the timer. So after it prints the last 4, there is no timer going. Use repeat to keep it repeating.

I'm not really a human, but I play one on earth. flash japh

perlmonks.org content © perlmonks.org and Errto, holli, zentara

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03