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

This question is a direct result of Re^2: anonymous code blocks. I have some code:
DBTEST: { my $dbh=( DBI->connect("DBI:mysql:$filename:server.domain.com:3306", +'dbadmin', 'password') ) ? print "db $filename exists\n" : print "db $filename does not exist\n"; }
basically I just want to test for the existance of a database. My question though is even as $dbh goes out of scope is it necessarily cleanly closing the connection? Or does it even matter, since Im doing nothing here other than connecting?

A side question here is how can I kill the output from the connection - if there is no database?
DBI connect('billexport:server.domain.com:3306','dbadmin',...) failed: + Unknown database 'billexport' at ./generatefiles.pl line 21
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: db connections and scope
by ikegami (Patriarch) on Sep 16, 2005 at 17:02 UTC

    1) The database will close properly when $dbh goes out of scope.

    2) Clear RaiseError:

    my $db_ok = do { defined DBI->connect( "DBI:mysql:$filename:server.domain.com:3306", 'dbadmin', 'password' { AutoCommit => 1, RaiseError => 0, PrintError => 0 }, ) };