in reply to Re: Perl action on cancellation
in thread Perl action on cancellation

It is the Control-C as a DOS command that I am thinking about.

Replies are listed 'Best First'.
Re^3: Perl action on cancellation
by Random_Walk (Prior) on Feb 08, 2005 at 12:22 UTC

    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!