Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

How do we capture CTRL^C

by Anonymous Monk
on Mar 04, 2005 at 09:14 UTC ( #436492=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Is there a way in perl as to capture the ctrl^c when a program is running Regards

Replies are listed 'Best First'.
Re: How do we capture CTRL^C
by Zaxo (Archbishop) on Mar 04, 2005 at 09:25 UTC

    $SIG{'INT'} = sub {print "Caught One!\n"};

    See perlvar and perlipc for more info.

    After Compline,

        Be careful when doing what - using sig handlers or hitting CTRL-C? The problem that you seem to be referencing has nothing to do with signal handling and everything to do with unsafe signals.

        The basic problem is that prior to 5.8.0, Perl would process a received signal immediately even if it was in the middle of an operation. This could make lead to some undesireable behavior when signals were received (intentionally such as hitting CTRL-C or otherwise). The problem with the alternative (safe signals) is that it may be this long running operation you want to abort midstream. With 5.8.1 and above, you can set an environment variable to get back the old functionality (which some find to be desireable) called unsafe-signals.

        Understanding is the key since the word "unsafe" leads to certain connotations that may not be applicable to a given circumstance. It is the reason why the N (nuclear) in NMRI has been almost universally dropped - people freak out when they hear it.

        Cheers - L~R

      Now try to press ctrl-C twice in a row.
      $SIG{'INT'} = sub {print "Caught One!\n"}; while(1) { sleep 1; }

      On ActivePerl 5.6.1/Win32, the program is exited the second time you press ctrl-C. Apparently the signal handler gets cleared... setting the signal handler again makes it work again. Of course, there will still be a short crack where it's still vulnerable.

      On ActivePerl 5.8.3, it keeps working.

Re: How do we capture CTRL^C
by manav (Scribe) on Mar 04, 2005 at 09:48 UTC
    One thing to note is that on some systems, signal(3) function is broken.(Perl uses this for its signal-handling mechanism, and perldoc says that on some systems, it behaves in <q>the old unreliable SysV way. </q>

    In this case, you need to re-register the signal inside the handler itself.

    I bring this to your notice coz some time ago, I was much troubled by this behaviour(dont remember, probably ActivePerl.....) and found this in perldoc after much brain-racking.....

    Because Perl's signal mechanism is currently based on the signal(3) function from the C library, you may sometimes be so misfortunate as to run on systems where that function is "broken", that is, it behaves in the old unreliable SysV way rather than the newer, more reasonable BSD and POSIX fashion. So you'll see defensive people writing signal handlers like this:
    sub REAPER { $waitedpid = wait; # loathe sysV: it makes us not only reinstate # the handler, but place it after the wait $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \&REAPER; # now do something that forks...

Re: How do we capture CTRL^C
by Anonymous Monk on Mar 04, 2005 at 09:42 UTC
    Well, it's just a keystroke, so you get it by doing a getc, or a (sys)read. Of course, you might be using a terminal that gets to ^C first - but then your problem is "how to disable the terminal from intercepting ^C". In that case (for Unix terminals), stty is your friend.

    Now, in case your real question is "how do I intercept SIGINT", see the answer elsewhere in this thread. But note that that answer isn't about capturing ^C - merely its effect on some terminals.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://436492]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2018-06-22 07:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (122 votes). Check out past polls.