Re: FW: Re: sigint, memoize
[email protected] (_brian_d_foy) Tue, 04 May 2004 09:42:05 -0400
| Newsgroups | perl.perlfaq.workers |
|---|---|
| Message-ID | <040520040942059558%[email protected]> |
In article <020520042154490968%[email protected]>, _brian_d_foy <[email protected]> wrote: > In article <[email protected]>, Robert Spier > <[email protected]> wrote: > > > MJD sent this message a few months ago. I meant to try and write > > something up, but after three months, it's fallen off the bottom of my > > todo list. > > let me take a whack at it sometime this week. cvs diff -u -d perlfaq8.pod Index: perlfaq8.pod =================================================================== RCS file: /cvs/public/perlfaq/perlfaq8.pod,v retrieving revision 1.20 diff -u -d -r1.20 perlfaq8.pod --- perlfaq8.pod 7 Feb 2004 04:29:50 -0000 1.20 +++ perlfaq8.pod 4 May 2004 13:41:05 -0000 @@ -413,31 +413,30 @@ Signals are documented in L<perlipc/"Signals"> and the section on ``Signals'' in the Camel. -Be warned that very few C libraries are re-entrant. Therefore, if you -attempt to print() in a handler that got invoked during another stdio -operation your internal structures will likely be in an -inconsistent state, and your program will dump core. You can -sometimes avoid this by using syswrite() instead of print(). - -Unless you're exceedingly careful, the only safe things to do inside a -signal handler are (1) set a variable and (2) exit. In the first case, -you should only set a variable in such a way that malloc() is not -called (eg, by setting a variable that already has a value). +You can set the values of the %SIG hash to be the functions you want +to handle the signal. After perl catches the signal, it looks in %SIG +for a key with the same name as the signal, then calls the subroutine +value for that key. -For example: + # as an anonymous subroutine + + $SIG{INT} = sub { syswrite(STDERR, "ouch\n", 5 ) }; + + # or a reference to a function + + $SIG{INT} = \&ouch; + + # or the name of the function as a string + + $SIG{INT} = "ouch"; - $Interrupted = 0; # to ensure it has a value - $SIG{INT} = sub { - $Interrupted++; - syswrite(STDERR, "ouch\n", 5); - } +Perl versions before 5.8 had in its C source code signal handlers which +would catch the signal and possibly run a Perl function that you had set +in %SIG. This violated the rules of signal handling at that level +causing perl to dump core. Since version 5.8.0, perl looks at %SIG +*after* the signal has been caught, rather than while it is being caught. +Previous versions of this answer were incorrect. -However, because syscalls restart by default, you'll find that if -you're in a "slow" call, such as <FH>, read(), connect(), or -wait(), that the only way to terminate them is by "longjumping" out; -that is, by raising an exception. See the time-out handler for a -blocking flock() in L<perlipc/"Signals"> or the section on ``Signals'' -in the Camel book. =head2 How do I modify the shadow password file on a Unix system? -- brian d foy, [email protected]