You will need to configure a signal handler.
package Test; sub new { bless {} } sub DESTROY { print "in DESTROY\n" } package main; my $terminate = 0; $SIG{INT} = \&sigint; sub sigint { $terminate = 1; } my $t = new Test; while (1) { last if $terminate; sleep 10; }
Something like that. Then in your main loop just mark $terminate , and if it sets the program to exit normally.
What happens is that cntl-c interrupts sleep mode, the signal handler is called setting $terminate , sleep mode returns immediately, it moves up, tests $terminate and exits gracefully.
Robert S. Barnes
source share