16.18. Catching Ctrl-CProblemYou want to intercept Ctrl-C, which would otherwise kill your whole program. You'd like to ignore it or run your own function when the signal is received. Solution
Set a handler for
SIGINT. Set it to $SIG{INT} = 'IGNORE'; Or, set it to a subroutine of your own devising to respond to Ctrl-C: $SIG{INT} = \&tsktsk; sub tsktsk { $SIG{INT} = \&tsktsk; # See ``Writing A Signal Handler'' warn "\aThe long habit of living indisposeth us for dying.\n"; } DiscussionCtrl-C isn't directly affecting your program. The terminal driver processing your keystrokes recognizes the Ctrl-C combination (or whatever you've set your terminal to recognize as the interrupt character), and sends a SIGINT to every process in the foreground process group ( foreground job ) for that terminal. The foreground job normally comprises all programs started from the shell on a single command line, plus any other programs run by those programs. See the section on " "Signals " in the Introduction to this chapter for details.
The interrupt character isn't the only special character interpreted by your terminal driver. Type % stty -a
The last section, See AlsoYour system's stty (1) manpage (if you have one); Recipe 15.8 ; Recipe 16.17 Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|