Enclose DBI operation inside eval -----------------------------------------
#!/usr/bin/perl use strict; use warnings; use DBI; use DBD::mysql; foreach $db (&hostdb("$project")) { eval { 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"; my $query = "SHOW DATABASES"; my $sth = $dbh->prepare($query); $sth->execute; my @dbs; while ( my $data = $sth->fetchrow_arrayref ) { push @dbs, $data->[0]; } $dbh->disconnect; }; if ($@) { print "host $db - not ok"; } else { print "host $db - ok"; } <img src="http://www.gnome.org/friends/banners/associate.png" alt="Become a Friend of GNOME" border="0" /> ________________________________ From: Vyacheslav <agapov.sl...@gmail.com> To: beginners@perl.org Sent: Sunday, April 8, 2012 11:42 AM Subject: foreach and next 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"; my $query = "SHOW DATABASES"; my $sth = $dbh->prepare($query); $sth->execute; my @dbs; while ( my $data = $sth->fetchrow_arrayref ) { push @dbs, $data->[0]; } print "host $db - ok; $dbh->disconnect; My result host db1 - ok host db2 - ok host db3 - ok DBI connect('information_schema:db4:3306','user',...) failed: Can't connect to MySQL server on 'db4' (111) at ./dbcheck.pl line 53 and script die How I can pass an error that the cycle has continued execute? I need host db1 - ok host db2 - ok host db3 - ok ... host db10 - ok Can I use next operator in this situation? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/