Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Disable writing of eval'd errors to CGI log

by Itatsumaki (Friar)
on Nov 27, 2003 at 06:11 UTC ( #310483=perlquestion: print w/replies, xml ) Need Help??

Itatsumaki has asked for the wisdom of the Perl Monks concerning the following question:

Howdy monks,

This might be standard question: if so, please point me at the documentation, because I just can't figure out what to call my problem.

In a variety of CGI scripts I use the eval { } if ($@) { } construct to catch errors from DBI or BioPerl modules. Interestingly, though, any errors that are caught still get written to my cgi.log filling it up. I'd like to turn that off, if at all possible. Is there a standard way of doing this? What exactly am I looking to turn off? It isn't STDOUT, so is it writing to STDERR I want to disable?

-Tats

Replies are listed 'Best First'.
Re: Disable writing of eval'd errors to CGI log
by Zed_Lopez (Chaplain) on Nov 27, 2003 at 06:24 UTC

    I suspect you've left PrintError => 1 in DBI (which is its default value). This means DBI is warning, not dying and the eval block doesn't trap warnings -- they'll go to whatever your $SIG{__WARN__} handler is (which is probably the default and is printing to STDERR.) Set RaiseError => 1, PrintError => 0.

    I don't know anything about BioPerl but expect it's also warnings.

      Thanks: this did indeed fix the DBI-based warnings I was getting.

Re: Disable writing of eval'd errors to CGI log
by davido (Cardinal) on Nov 27, 2003 at 07:30 UTC
    The problem probably is that eval is great for trapping errors and allowing you to deal with them nicely, but it doesn't do anything for trapping warnings.

    For that you have to set up a $SIG{__WARN__} handler.

    See warn for a brief discussion on how to trap warnings. Also, look at perlvar for info on setting %SIG entries, and the Carp module's POD for a more detailed discussion on error and warning handling.

    What you're thinking are errors might, in fact, just be warnings.


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein

      Thanks, this was necessary for most of the BioPerl warnings.

Re: Disable writing of eval'd errors to CGI log
by bart (Canon) on Nov 27, 2003 at 09:19 UTC
    What code is responsible for writing to the log? My guess is that it uses a construct based on $SIG{__DIE__}. If so, and the handler does make responsible use of the $^S variable to distinguish eval from a normal die, this is what happens. This is what "responsible" looks like:
    $SIG{__DIE__} = sub { return if $^S; # inside eval # write to log: ... };

    Either get a properly updated module containing the error handler/log writer, fix the module if it's one of your own, or temporarily clear $SIG{__DIE__} for the eval, like this:

    { local $SIG{__DIE__}; eval { # your code here ... }; } if($@) { # oops! ... }
Re: Disable writing of eval'd errors to CGI log
by ambrus (Abbot) on Nov 27, 2003 at 10:58 UTC

    You surely mean eval { }; if ($@) { }, don't you? The semicolon is imporant there.

      No, bart put his eval, whose block was correctly terminated with a semicolon, within another block so he could localize $SIG{__DIE__}. That block shouldn't get a semicolon and doesn't have one.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://310483]
Approved by Roger
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-06-02 16:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (19 votes). Check out past polls.

    Notices?