Hi Perl monks. I have a large program with many different packages. Some of the packages use voluntary file locks, that are normally cleaned out in the END block. But, when a SIGINT is sent, the END block doesn't get seen. So I had the idea to "chain" signal handlers on SIGINT:

# Use a closure to add a new behavior to the # chain of behavior for the interupt handler { my $OldINTHandler = $SIG{'INT'}; defined $OldINTHandler or $OldINTHandler = sub { exit(2); }; sub NewIntHandler { unlink(@LockFileArray); &{$OldINTHandler}; } # Install the new handler $SIG{'INT'} = \&NewIntHandler; }


The idea here is to preserve any existing behavior but still unlink my file locks in the case of a SIGINT. I think that me implementation is solid can be used throughout. However, signal handlers can have unexpected behavior -- and I would really like to hear what might go wrong with this approach. (Assuming the chaining method is used throughout - ie, $SIG{'INT'} isn't stupidly set to a non-chaining handler.)

One point of concern -- I couldn't find a code reference to the default handler, so I had to put an 'exit' in explicitly. Maybe you know a better way?

BTW - I know there are many more signals.. at most I would catch SIGTERM too, so 'kill' would work in a nice way.

Thanks for your help!

In reply to Chaining Signal Handlers by Anonymous Monk

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.