Thanks all.

using eval helped me.

08.04.2012 09:43, Shlomi Fish пишет:
Hi Vyacheslav,

On Sun, 08 Apr 2012 06:12:53 +0000
Vyacheslav<agapov.sl...@gmail.com>  wrote:

Hello all.
My english bad and i have a problem.

I am connected to databases in a cycle foreach and the script die, if
one of database is not available.

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBD::mysql;

foreach $db (&hostdb("$project")) {

my $server = "$db";
my $dbname = "information_schema";
my $port = "3306";
my $dsn = "dbi:mysql:$dbname:$server:$port";
my $user = "user";
my $pass = "pass";

my $dbh = DBI->connect($dsn, $user, $pass) or die "Can't connect to the
DB: $DBI::errstr\n";
Your problem is that you are invoking die "..." which throws an exception. See:

https://www.socialtext.net/perl5/exception_handling

Since this exception is not caught (using eval { ... }) it terminates the
entire program. So what you should do instead is handle it gracefully (say
using "next"):

my $dbh = DBI->connect($dsn, $user, $pass);

if (!$dbh)
{
        next DB_HOSTS_LOOP; # And label the loop appropriately.
}

Regards,

        Shlomi Fish


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to