Hello Monks,

I am stumped yet again and need to call upon your infinite knowledge. I have the below sub routine but some of the databases it pulls are not valid so when the main body executes it stops/exits.

What I would like to do is keep the script going even if the connection fails - IE have it move onto the next element in the arary (database).

Right now (as you can see) I tried to put the connect into an if statement in the sub_rortine but it dosen't seem to work. This is new to me so not sure if I should just have something in the body or in the subroutine (like i have below)

#!/usr/bin/perl use strict; use warnings; use DBD::mysql; ###################### #### CONFIG VARS ##### ###################### my $output_file = 'Algo_usage.txt'; ###################### #### PRIVATE VARS #### ###################### my $db_user = 'xxxxx'; my $db_pass = 'xxxxx'; my $ignored_text = "'sech05h','gbts01t','smhb07e','QAITtb01','qait33', +'qa'"; ###################### #### MAIN BODY ####### ###################### select(STDOUT); $| = 1; open OUTPUT, ">$output_file"; select(STDOUT); $| = 1; my @book_db = get_book_databases(); foreach my $bookdb (@book_db) { print "Processing on: $bookdb\n"; my $dbh = DBI->connect("DBI:mysql:database=$bookdb:host=xxxxx",$db +_user,$db_pass,{RaiseError=>1})|| die "$DBI::errstr\n"; my $sql = "SELECT COUNT(ID) FROM Items WHERE Content REGEXP '<valu +e name=\"algorithm\">var'"; my $sth = $dbh->prepare ( $sql ); $sth->execute(); while (my @row = $sth->fetchrow_array()) { if ($row[0] > 0) { my $count = $row[0] ? $row[0] : 0; #Get Book Information { my $dbh1 = DBI->connect("DBI:mysql:database=xxxx:host +=xxxx",$db_user,$db_pass,{RaiseError=>1})|| die "$DBI::errstr\n"; my $sql = "SELECT Name, Abbr, Discipline FROM BookList + WHERE SUBSTRING(DatabaseURL,19) = '$bookdb'"; my $sth = $dbh1->prepare ( $sql ); $sth->execute(); while (my @row = $sth->fetchrow_array()) { print OUTPUT "$row[2]\t$row[0]\t$row[1]\t$count\n" +; } $sth->finish(); $dbh1->disconnect(); } } } $sth->finish(); $dbh->disconnect(); } close OUTPUT; ###################### #### PRIVATE SUBS #### ###################### sub get_book_databases { my @book_database_urls; my $dbh = DBI->connect("DBI:mysql:database=xxxx:host=xxxxx",$db_us +er,$db_pass,{RaiseError=>1})|| die "$DBI::errstr\n"; my $sql = "SELECT DISTINCT SUBSTRING(bl.DatabaseURL,19) FROM BookL +ist AS bl WHERE BookClass='SQL' AND Product IN ('BCTest','TestBank',' +Forms','Homework','PrintedTestBank') AND Enabled='1' AND bl.Abbr NOT +IN ($ignored_text)"; my $sth = $dbh->prepare ( $sql ); $sth->execute(); while (my @row = $sth->fetchrow_array()) { print "Testing $row[0] => "; if (my $dbh1 = DBI->connect("DBI:mysql:database=$row[0]:host=x +xxxx",$db_user,$db_pass,{RaiseError=>1})) { print "SUCCESS\n"; push (@book_database_urls, $row[0]); } else { print "FAILED\n" } } $sth->finish(); $dbh->disconnect(); return @book_database_urls; }

In reply to How do I keep script live and not abort on fail to connect by kdmurphy001

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.