I'm working on a module which traps warning and fatal signals in the BEGIN block:

  $SIG{__WARN__} = \&foo;
  $SIG{__DIE__}  = \&bar;

All this works fine (well, I gotta work on trapping dies in `eval' but that's a separate issue). What doesn't work is when I use another module that also traps these, such as CGI::Carp.

If I `use' my module before CGI::Carp, my traps are lost. Likewise if I use mine after CGI::Carp, I kill its traps.

I've tried something like the following:

  if ($SIG{__WARN__}) {
    my $previous = $SIG{__WARN__};
    $SIG{__WARN__} = sub {
        # do my stuff
        goto &$previous;
    };
  }
  else
  # blah blah

but this doesn't work! It repeatedly calls my handler over and over, as if $previous contained a reference to my code (I've debugged it and found $previous only contains something when a module such as CGI::Carp has trapped the signal).

So what am I doing wrong here?


In reply to How can two modules latch onto $SIN{__WARN__} or $SIG{__DIE__}? by rrwo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.