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

Hi. I am using a sub with my Tk module similar to;

sub tick { # Run every 1000 milliseconds. if ($toggle_scan_v eq 0) {return}; # Break out of this tick l +oop # Do something $kount++; print "$kount "; $mw->after(1000, \&tick); # Suggest loop here print "No Error; we should get here often. \n"; }

I trust that whenever the "after" is executed, the print is executed, after which a graceful exit of the subroutine occurs. This provides dead time for the thread of execution. In turn other events belonging to this same thread may occur and execute (possibly turning $toggle_scan_v on and stopping this "tick" loop which is earmarked to execute 1000 mSec after the "after" method).

I trust that the subroutine is called and always subsequently returns in such a manner that there is no stack littering.?

Regards JC....

Replies are listed 'Best First'.
Re: Tk module usage of after method
by choroba (Cardinal) on Mar 30, 2025 at 16:26 UTC
    To call something repeatedly, you can also use $mw->repeat.
    my $repeat_id = $widget->repeat($ms, $callback);
    To cancel the loop, you then just call
    $widget->afterCancel($repeat_id);
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]