everbox everbox - 1 year ago 77
Perl Question

How to suppress arbitrary warn/error messages from DBI?


# A:
$dbh->do(qq/insert into foo(cl) values('test')/);
# B:
$dbh->do(qq/insert into foo(cl) values('test')/) or warn $dbh->errstr;
# C:
eval { $dbh->do(qq/insert into foo(cl) values('test')/); };
warn "error : $@ " if $@;

All would output :

DBD::mysql::db do failed: Duplicate entry 'test' for key 'cl' at a.pl line 9.

I dont want this arbitrary warning/error message send to stderr. I'd like use
warn $dbh->errstr

perl a.pl 2>/dev/null
would suppress the error message, but I want to know how to do this in script?

Answer Source

You need to install your own error handler. E.g.

$dbh->do($statement, { HandleError => \&handle_error });

Error handlers are described in DBI POD

Another options are to:

  • Set PrintWarn attribute to false value (courtesy of Sinan's answer on some forum

  • Trap all warnings via a signal handler: $SIG{'__WARN__'} = sub {};

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download