http://www.perlmonks.org?node_id=570210

snafu has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing code that is fork()able and within that code I want to be able to catch and log all signals whether or not they kill the code (of course, not TERM). So, after trying a few options (including $SIG{__DIE__} and END{};) I came up with the following code which does what I want it to do. What I'm curious about is whether or not there is anything wrong with this pragma and if there is a better(tm) way to do it.

I did a search and found some ideas using Carp and eval()'s but felt that since this program is already 99.9% done being written, it's on a deadline, and it's working quite well that this would be an acceptable way of getting what I want.

TIA

72 ## Place this first. 73 for my $sig ( keys(%SIG) ) 74 { 75 $EXIT++; 76 outmsg(' Processing handler: '.$sig.'('.$EXIT.')',1); 77 78 $SIG{$sig} = sub 79 { 80 outmsg('Caught signal: '.$sig.'...dying +.',2,$EXIT); 81 }; 82 } 83 84 ## Then any specific handlers here...next. 85 $SIG{USR1} = sub 86 { 87 ## Make sure that the process is in a sleep state so that 88 ## we don't inadvertently interrupt a run cycle. 89 if ( $SLEEPING ) 90 { 91 outmsg(' CAUGHT USR1! -- pre-empti +ng sleep '. 92 'schedule.',10); 93 _main(); 94 } 95 }; 96

_ _ _ _ _ _ _ _ _ _
- Jim
Insert clever comment here...