Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

When I am using Ctrl-C and coming out of Perl Program

by sanPerl (Friar)
on Nov 02, 2009 at 13:30 UTC ( [id://804452]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
Greetings,
As the subject suggests. When I am using Ctrl-C and coming out of Perl Program, I want to run a subroutine (which will put some message in log file or do some action).
I know it is possible, I would be glad if someone guides me to right solution/link/document to read/example.
Regards,
SanPerl

Replies are listed 'Best First'.
Re: When I am using Ctrl-C and coming out of Perl Program
by Unforgiven (Hermit) on Nov 02, 2009 at 13:48 UTC
Re: When I am using Ctrl-C and coming out of Perl Program
by gmargo (Hermit) on Nov 02, 2009 at 14:26 UTC

    See also the sigtrap documentation.

    Here's some simple test code to play with:

    #!/usr/bin/perl -w # Test code to demonstrate signals. # A file is created, then removed by the signal handler. use strict; use warnings; use sigtrap; use IO::Handle; use sigtrap handler => \&TermHandler, 'normal-signals'; my $mystery = "/tmp/mystery_date.txt"; sub TermHandler { my $signal = shift; print STDERR "TermHandler: $signal signal received at ".localtime( +).".\n"; unlink($mystery) if -f $mystery; die("death"); } # Create file and sleep forever open(OUT, ">>", $mystery) || die("Cannot open $mystery: $!"); autoflush OUT 1; autoflush STDOUT 1; while (1) { print OUT "The time is now ".localtime()."\n"; print "The time is now ".localtime()."\n"; sleep(2); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found