compilation erros

Note this from perlvar:

__DIE__/__WARN__ handlers are very special in one respect: they may be called to report (probable) errors found by the parser. In such a case the parser may be in inconsistent state, so any attempt to evaluate Perl code from such a handler will probably result in a segfault. This means that warnings or errors that result from parsing Perl should be used with extreme caution...

Using $SIG{__DIE__} to catch runtime errors is fine, though, as well as from inside eval, despite what the above link currently says - the following is a text from the upcoming 5.26 (reference):

The $SIG{__DIE__} hook is called even inside an eval(). It was never intended to happen this way, but an implementation glitch made this possible. This used to be deprecated, as it allowed strange action at a distance like rewriting a pending exception in $@. Plans to rectify this have been scrapped, as users found that rewriting a pending exception is actually a useful feature, and not a bug. Perl never issued a deprecation warning for this; the deprecation was by documentation policy only. But this deprecation has been lifted in Perl 5.26.

If you want to reliably log compilation errors, redirect STDERR from outside of the script, like shmem said. Once the Perl script is up and running, you can use eval as a try/catch mechanism (eval { code here ...; 1 } or do { handle error }, or e.g. Try::Tiny), or use a $SIG{__DIE__} handler, however, note that the latter is only called when the program is already dying, so the only thing I'd do in that handler is try to do something with the error message, like rewrite it or attempt to log it to a file - keeping in mind that this may fail, which is why I said before to redirect STDERR from outside of the script.

Oops, accidentally posted this node before finishing it. Okay, done working on it. No, really, now I am.


In reply to Re^3: how i capture a script error to a file ? by haukex
in thread how i capture a script error to a file ? by gabrielsousa

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.