@arr_ =('host1','host2'); foreach $host (@arr_){ @Mydsn =("dbi:mysql:$MYSQL_DATABASE:$host:$MYSQL_PORT","$MYSQL_USER","$MYSQL_PWD"); my $my_connection = DBI->connect(@Mydsn, { RaiseError => 0, PrintError => 1 } ) or die("Fail to connect Database connection"); ### Here how can i skip if mysql is not able to connect with breaking execution of script }