Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How do we capture CTRL^C

by Zaxo (Archbishop)
on Mar 04, 2005 at 09:25 UTC ( [id://436494]=note: print w/replies, xml ) Need Help??


in reply to How do we capture CTRL^C

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

See perlvar and perlipc for more info.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: How do we capture CTRL^C
by tirwhan (Abbot) on Mar 04, 2005 at 09:51 UTC
      tirwhan,
      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

        Hmm, not wanting to get into a semantic squabble here, but unless I'm mistaken (which is always possible) it is the signal handler, not the signal itself which was unsafe. Or more precisely the way perl used to process signals wich allowed for "unsafe" handling.

        I was referring to Zaxo's signal handler, which printed out a string. As the linked node mentions, it is possible to cause a core dump when doing stdio in a signal handler during another stdio operation. So I figured a word of warning would be appropriate, since the original poster seemed to want to intercept the SIGINT and carry on regardless. Sorry for not explaining that in more detail earlier.

Re^2: How do we capture CTRL^C
by bart (Canon) on Mar 05, 2005 at 01:38 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://436494]
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: (2)
As of 2024-04-20 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found