Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by sanPerl (Friar)
on Nov 02, 2009 at 13:30 UTC ( #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

Comment on When I am using Ctrl-C and coming out of Perl Program
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
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? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-12-28 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls