elsewhere I learned that putting a BEGIN block in my script
#!/usr/bin/perl -w BEGIN { $0 =~ /^(.*)\\([^\\]*)\.pl$/; our ($path, $script_name) = ($1, $2); open(STDERR, ">>$path/$script_name.err") or die "invisible error"; warn "$0 started ".localtime().$/; }

prints out to the file ?.err any errors or warnings that the script migh encounter during compile time.

My question is -- is there a way to implement a corresponding FINISH block (as opposed to __END__) which will trap any errors in case the script comes to a dead halt?

Now, I can use the die statement whenever something goes wrong, but that can't really trap things like Control-breaks or control-c etc. How do I trap SIGTERM so that the reason for SIGTERM is written out to ?.err as well?

Update: I should mention -- I need to do this on Windows.

Considered by dragonchild - Retitle to "How do I trap signals on Win32"
Unconsidered by castaway - Keep/Edit/Delete: 8/19/0 - Its fine as is, no putting words in the OPs mouth


In reply to Implementing the counterpart of BEGIN by punkish

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.