Sorry, let me DOS that for you, looks like Control-C won't break out of the sleep the same way on windows.
C:\PERL\bin>perl -le"$SIG{INT}=sub{print 'cleanup';exit 0}; while(1){s
+leep 1}; END{print 'goodbye'}"
# here I hit Control-C
cleanup
goodbye
Update
Now we are a bit more than a one liner we may as well write a proper script. As tirwhan points out it is dangerous to do much in the signal handler. Best just to set a flag that tells your loop to exit.
#!/usr/bin/perl
use strict;
use warnings;
my $killed=0;
$SIG{INT}=sub{$killed++};
while (1) {
# this may take some time
sleep 1;
last if $killed;
}
if ($killed) {
print "cleaning up after getting a sig int\n"
}else{
print "finished infinite loop, halting problem next\n"
}
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
|