Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: error handling with dbi

by jeffa (Bishop)
on Feb 16, 2016 at 20:22 UTC ( #1155393=note: print w/replies, xml ) Need Help??


in reply to error handling with dbi

There are many, many ways to accomplish what you wish. But you also have to ask yourself if it really worth your time to write a script for this task when you should have reliable configuration tools. Having said that, here is a small script that demonstrates using DBI's HandleError hook. The code will stop after take the first successful connection, which may not be quite what you want:

use strict; use warnings; use DBI; use Data::Dumper; sub dbi_error { warn "failed with @_\n" } my @attempts = ( [qw( wrong wrong )], # this would be a wrong user/pass [qw( correct pass )], # this would be a correct user/pass [qw( wrong wrong )], # and another wrong one ); my $dbh; for (@attempts) { $dbh ||= DBI->connect( qw(DBI:mysql:information_schema:localhost), @$_, { HandleError => sub { dbi_error( @$_ ) } }, ); } print Dumper $dbh->selectall_arrayref('select * from TABLES', {Slice=> +{}});
But as you can see, DBI does indeed allow you to control what happens when an error occurs. You do not necessarily need to wrap the call in a try-catch block. Hope this helps!

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1155393]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2017-11-21 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (295 votes). Check out past polls.

    Notices?