Another way is to capture the errors with a error handler, do what ever you need to them (send it to your log file, print them, die or continue executing the script).
This eliminates the need of " or die() "
after every method. Documentation about the HandleError method can be found here.
For starters take this simple example:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $DB_name = 'database';
my $DB_user = 'root';
my $DB_pwd = '';
my $dsn = 'dbi:mysql:avm:localhost:3306';
my ($sth, $id, $name);
my $dbh = DBI->connect($dsn,$DB_user,$DB_pwd, { PrintError => 0, ShowErrorStatement => 1, HandleError => \&dbi_error_handler,} );
$sth = $dbh->prepare("SELECT * FROM tblmanufacturer");
$sth->execute();
while ( ($id,$name) = $sth->fetchrow_array() )
{
print "$id\t\t $name \n";
}
$sth->finish();
$dbh->disconnect();
sub dbi_error_handler
{
my( $message, $handle, $first_value ) = @_;
# print to your log file, call your own logger etc ...
# here it will die() to be similar to "or die()" method, but the line number is incorect
die($message);
# if you return false it will check/execute RaiseError and PrintError
return 1;
}
P.S. There is no reason to encapsulate yout string variables in quotes here :($dsn,"$DB_user","$DB_pwd");
, don't do it, for more info about that, read this.