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

Are all signals unsafe? Or just the alarm and interrupt signals? What about warn?

I wrote a handler for warnings to give me some more info when things break. But if the warning handler makes things break as well, then obviously I don't want to use it. Is this code bad?

Update: This is under ActiveState Perl 5.6.1, on WinXP

$SIG{__WARN__} = \&mywarn; # ... sub mywarn { my $warning = shift; my $i = 0; open (WRNLOG, ">>Warnings.log") || warn "Can't open warnings log f +ile: $!"; # If open failed, output will be discarded print STDOUT "WARNING: $warning"; print WRNLOG "WARNING: $warning"; print STDOUT " Stack Trace:\n"; print WRNLOG " Stack Trace:\n"; while (caller($i)) { my (undef, $filename, $line, $sub) = caller($i); print STDOUT " $filename ($line) --- $sub\n"; print WRNLOG " $filename ($line) --- $sub\n"; $i++; } print STDOUT "\n"; print WRNLOG "\n"; }

A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Which signals are unsafe?
by BrentDax (Hermit) on Apr 01, 2004 at 23:25 UTC

    $SIG{__WARN__} isn't a real signal, so it's safe. I believe that all signals have been safe since 5.8.

    Have you looked at the Carp module? It can be used with $SIG{__WARN__} to print backtraces. Perhaps you could do something like this:

    use Carp 'cluck'; $SIG{__WARN__}=\&cluck; open(STDERR, "|tee -a Warnings.log") or die "Can't open STDERR to tee: + $!";

    It's not exactly the same as what you have; most notably, all STDERR output would go to Warnings.log, not just warnings. Fixing this is left as an exercise for the reader. ;^)

    If nothing else, Carp's longmess sub can handle the backtracing for you.

    --Brent Dax
    There is no sig.

Re: Which signals are unsafe?
by etcshadow (Priest) on Apr 01, 2004 at 23:20 UTC
    All real signals are potentially unsafe (in older perls). Warn, however, is not a real signal... it's a psuedo signal, and it's always been safe.
    ------------ :Wq Not an editor command: Wq