in reply to Re^2: how i capture a script error to a file ?
in thread how i capture a script error to a file ?
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.
|
|---|