Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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!


(the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1155393]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2023-09-24 10:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found