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

Is there a way to redirect STDERR (from DBI) into an array so you can display the errors after the program is all done?

Currently my app that uses DBI is displaying all the errors right to the screen. *sigh*

Replies are listed 'Best First'.
Re: STDERR
by lemming (Priest) on Dec 20, 2000 at 01:09 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: STDERR
by PsychoSpunk (Hermit) on Dec 20, 2000 at 02:49 UTC
    Technically speaking, everyone may have attacked this wrong. Remember that the DBI puts the last error produced into $DBI::errstr aka $dbh->errstr. Most likely, at the DBI->connect jettero has RaiseError => 1 which is going to be the real culprit.

    I preach using RaiseError until the code goes to production. Why? Because I don't want my scripts to produce any output until it is clean. Thus, the result will oft be located in the apache error-log.

    jettero doesn't want this to happen, so first he should set RaiseError => 0. (Btw, I don't use PrintError since RaiseError does the same with a warn.) Now, after each DBI prepare and do, a good little coder will test for success, and finding there is none means it's time to put the $DBI::errstr into whatever container you're using to hold all errors. (This is only valid if a single error is not going to kill the rest of the script) Then, lo you may print the container of errors to STDOUT along with everything else.

    While jettero's answer does handle properly, it's also what signalled that he is using RaiseError. I'm advocating this method since some errors may need to be handled differently (ie - stop execution of the script) than others. His own answer to his question is probably valid in the sense that he may not have this requirement.

    ALL HAIL BRAK!!!

Re: STDERR
by footpad (Abbot) on Dec 20, 2000 at 01:53 UTC
    jettero,

    Update: Added clarification after seeing jettero's response. Left original post for context.

    Would something like this help?

    $errfile = qw( stderr.log ); open STDERR, ">$errfile"; die "And in that sleep of death, who knows what dreams may come...";

    Clarification:

    Sorry about the mixup. Here's something crude that seems to work on my machine. YMMV

    #!\usr\bin\perl my @errors = (); my $ctr = 0; for ( $ctr = 0; $ctr < 10; $ctr += 1 ) { eval { if ( $ctr == 5 ) { die "Number 5 is not alive."; } }; if ( $@ ) { push ( @errors, $@ ); } } $ctr = @errors; if ( $ctr != 0 ) { for ( @errors ) { print "$_\n"; } } else { print "No errors found."; }

    As I said, it's a bit tortured, but the idea is to wrap the DBI stuff into an eval block, pushing the problems onto @errors. Later, you can dump them to the screen, to a file, or whatever.

    Sorry for the confusion...

    --f
      Acutally, no, since (as you recall) I want to store the lines of STDERR into an array, not a file.
Re: STDERR (the answer)
by jettero (Monsignor) on Dec 20, 2000 at 02:31 UTC
    I found the answer on my own. This is more like what I was looking for. Tieing it seems like killing a fly with a sledgehammer.
    $SIG{__WARN__} = sub { push @errors, @_; };