http://www.perlmonks.org?node_id=1155365

gandolf989 has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a script that will run on Redhat 6.5 with Perl 5.10.1 and an Oracle 11.2.0.3 client. I need to connect to a few dozen Oracle databases using 10g, 11g or 12c. Unfortunately, there are two possible passwords that could get used, and I don't have a list of which password to try. Hence, I want to try one password and if that does not work try the other. Otherwise, I will just add it to an array that will print out at the end and I can look at those databases and possibly change the password. I am using the system to connect to every database.

How should I do the error handling to deal with not being able to connect to a database? Also when I get an error and try to exit, the script doesn't exit, but restarts from the beginning. How can I change that behavior?

Thanks in advance!
sub connect_to_db { my $tns_name = shift; my $logger = shift; my $run_as_user = shift; my $system_password = shift; my $system_password2 = shift; my $dbh; my $correct_password; # try connecting with one password then the other, if both passwords +fail mark it as failed try { $dbh = DBI->connect( "DBI:Oracle:".$tns_name, $run_as_user, $sys +tem_password, { PrintError => 1, RaiseError => 1, AutoCommit => 1 } ); $correct_password = $system_password; } catch { print "wrong password, try another\n"; }; if ( undef $dbh ) { try { $logger->info( 'password two |'.$system_password2.'|'); $dbh = DBI->connect("DBI:Oracle:".$tns_name, $run_as_user, $ +system_password2, { PrintError => 1, RaiseError => 1, AutoCommit => 1 } ); $correct_password = $system_password2; } catch { print "wrong password, add it to the connection issues list\n +"; }; } if ( undef $dbh ) { push @db_connect_issues, "error:".$tns_name." -- ".$DBI::errstr; } else { $users_and_passwords{ 'INSTALLER'}{$run_as_user } = $correct_pas +sword; print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> The right password is + $correct_password <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; } return $dbh; }