| Previous | Next
SignalsPerl supports many facilities for Interprocess Communication (IPC), including signals and sockets. Regardless of which IPC form you use with Perl, you'll almost always tangle with signals. Signals can be generated from any source, including key sequences on your keyboard, an event on your system, or from a failed program. For quite some time, Perl has used a simple signal handling module, in which For signals to be useful, they must be trapped and dealt with. However, you shouldn't try to do too much when you trap a signal. Generally, you should only generate a warning and deal with the condition that caused the error. In a way, Perl's
If you need to know which signals Perl is aware of, do a
Here's a simple snippet that lets you know if someone sent you a
Often, the type of signal you encounter in your Perl program will dictate how you should handle it. For example, you should assign
Unix allows you to kill processes with negative process IDs by sending a signal zero. If you have a program that starts itself and several children, use
Naturally, you don't have to do anything fancy with signal handlers. You can simply die if the given
Keep in mind that it's not all fun and games with Perl and signals. If you don't know how your system's C library and its signals implementation behave, or if you haven't read the instructions before firing your BB gun, you'll shoot your eye out. We guarantee it! As of Perl 5.8, you can probably be a little more confident in Perl's ability to handle even a bad system-specific signals implementation. In the old days, when men were men and eyeless men were everywhere, a bad signals implementation at both a system and Perl level, or a signal cropping up at the wrong time, could corrupt Perl's internal state. Thankfully, Perl 5.8 and later will postpone signal handling until it's safe to proceed. This means that while the signals interface doesn't change, even if your program catches a signal at a specific place, Perl 5.8 and later will finish whatever they are doing when the signal is encountered. Finally, signals aren't limited to coping with the behaviors of processes. You can also trap alarm signals for Perl functions and system calls alike if your Perl code lives in an
Note that you may have to wait for the ping command itself to timeout. While |