Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Interrupting a loop

by choroba (Cardinal)
on Jul 28, 2024 at 08:34 UTC ( [id://11160787]=note: print w/replies, xml ) Need Help??


in reply to Interrupting a loop

Under Tk, Ctrl+C no longer sends the INT signal. You can still bind <Control-c> to something, though.

Also, Tk already incorporates a loop: the MainLoop. Looping over short lists is probably OK, but introducing loops that might take a long time to iterate or have a high number of iterations is wrong, you should use Tk->repeat or Tk->after instead, see Tk::after.

#!/usr/bin/perl use warnings; use strict; use Tk; sub periodically { warn "Testing the file existence...\n"; } my $mw = MainWindow->new(-title => 'Test'); my $repeat = $mw->repeat(1000, \&periodically); $mw->bind('<Control-c>', sub { $mw->afterCancel($repeat) }); MainLoop();

Update: Added the example.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Interrupting a loop
by etj (Curate) on Jul 28, 2024 at 13:43 UTC
    To emphasise a point in choroba's excellent thought: when you want to juggle multiple events (a signal/button-click AND elapsed time), you need to stay in the idiom of the relevant event loop. In Tk, that's why you'd want to do the time-delay with Tk::after, not with sleep, so the event loop can quietly also watch for the button-click etc and react; sleep just suspends the whole process until the OS sends it a SIGALRM.
Re^2: Interrupting a loop
by cavac (Parson) on Jul 29, 2024 at 05:09 UTC
Re^2: Interrupting a loop
by merrymonk (Hermit) on Jul 28, 2024 at 13:42 UTC
    Thank you. I had not come across Tk->repeat etc before.
    So I now have my interruptible Perl Tk app working now.
      The reason I wanted CNTR-C to work was that the Buttons on my Tk GUI would not respond when the loop was processing.
      I now have found that using the Tk repeat for the loop means that I can use the Buttons.
      So the solution was even better than I first thought.

      You should be using <br/>, not </br>. TIA. (I fixed your root post already.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11160787]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-09-13 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.