Re: Can't trap Pg warnings



Ripped out of some of my code working with DBD::Pg:

# DBI->connect() here
$SIG{'__WARN__'}=sub {
# Don't use REs (//) here, you would change some special variables!
warn @_ unless substr($_[0],0,7) eq 'NOTICE:';
};
# Rest of the application

Hope that helps.

Alexander

tlm wrote:

Consider the following short script:

use strict;
use warnings;
use DBI;

my $h = DBI->connect( 'dbi:Pg:dbname=mytestdb', 'anonymous', '', );

# close( STDERR );
# open( STDERR, '>', \my $stderr );

warn 'before';
$h->prepare( 'CREATE TEMPORARY TABLE tmp ( i serial )' )->execute();
warn 'after';
$h->disconnect;

# print ">>\n$stderr<<\n";
__END__

The output I get from it looks like this:

before at test_script.pl line 10.
NOTICE: CREATE TABLE will create implicit sequence "tmp_i_seq" for serial
column " tmp.i"
after at test_script.pl line 12.

The output looks exactly the same if I pipe the command's standard output to
/dev/null ( i.e. what we're seeing was sent to standard error).

Now, if I uncomment the commented lines, the output looks like this:



before at script/test.pl line 10.
after at script/test.pl line 12.
<<

So, even though the messages given explicitly to warn in the script were
faithfully appended to the scalar $stderr, as intended, the notice from Pg
(about the implicitly created sequence) is gone: it shows up neither on the
screen, nor in the scalar $stderr.

How can I trap such notices?

Thanks!

tlm


P.S. It is tempting to blame the closing of STDERR for this problem, but
this a mandatory step. Without it, $stderr receives no output at all.




--
Alexander Foken
mailto:alexander@xxxxxxxx http://www.foken.de/alexander/

.



Relevant Pages

  • Re: File handle to "in memory" file
    ... >>> with a scalar and which scalar it is connected with. ... closes STDERR and reopens it as handle to an "in memory" file: ... my $class = shift; ... Frank Seitz; http://www.fseitz.de/ ...
    (comp.lang.perl.misc)
  • Re: File handle to "in memory" file
    ... Frank Seitz wrote: ... >>> with a scalar and which scalar it is connected with. ... closes STDERR and reopens it as handle to an "in memory" file: ... my $class = shift; ...
    (comp.lang.perl.misc)
  • Re: help with eval
    ... > The original STDERR filehandle is special. ... > In order to redirect STDERR to a scalar, it must be closed first, ... not want STDERR to left without its specialness after a temporary ...
    (comp.lang.perl.misc)
  • Cant trap Pg warnings
    ... The output looks exactly the same if I pipe the command's standard output to ... even though the messages given explicitly to warn in the script were ... faithfully appended to the scalar $stderr, as intended, the notice from Pg ... nor in the scalar $stderr. ...
    (perl.dbi.users)
  • Re: Cant trap Pg warnings
    ... Ripped out of some of my code working with DBD::Pg: ... use DBI; ... tlm wrote: ... faithfully appended to the scalar $stderr, as intended, the notice from ...
    (perl.dbi.users)