Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: how i capture a script error to a file ?

by gabrielsousa (Sexton)
on Apr 27, 2017 at 13:09 UTC ( #1189036=note: print w/replies, xml ) Need Help??

in reply to Re: how i capture a script error to a file ?
in thread how i capture a script error to a file ?

i want to capture my script compilation erros ( or other errors ) to a log file
  • Comment on Re^2: how i capture a script error to a file ?

Replies are listed 'Best First'.
Re^3: how i capture a script error to a file ?
by shmem (Chancellor) on Apr 27, 2017 at 13:20 UTC
    want to capture my script compilation erros ( or other errors ) to a log file

    To capture compilation errors, redirect the STDERR of the invoking shell to the file as in my previous post:

    $ perl 2>log.err

    This also captures runtime errors thrown out STDERR. If you want to only capture fatal runtime errors, you might handle these through a DIE handler. However, this handler isn't setup if the compilation fails in the first place.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      ok, but i do it on script ? without using > stderr on bash

        ok, but i do it on script ?

        What environment does "on script" connote? The program in question is invoked by "some something", and that "some something"'s STDERR must be redirected to the error log. The notation I gave is for bash (and sh, korn shell, zsh and others). Check the documentation of that "some something" about how to redirect its STDERR.

        If that "some something" is another instance of perl, see open about how to redirect STDERR. You might want to localize that redirection.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^3: how i capture a script error to a file ?
by haukex (Bishop) on Apr 27, 2017 at 14:23 UTC
    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189036]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2021-09-28 17:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found