[Melbourne-pm] DBI connect()'s, scope and disconnects

Gautam Gopalakrishnan thatha at gmail.com
Thu Jul 26 21:26:52 PDT 2007


> foreach(@an_array)
> {
>     my $dbh=DBI->connect($dsn);
>     # Do some other stuff here.
>     last if (some_condition == true);
>     $dbh->disconnect();
> }
> Question is, if some_condition is true, the loop ends and $dbh goes out of
> scope. Does disconnect() get called on $dbh automatically?

It does (as per the documentation) but safer way would be to put the
disconnect inside a continue block.

foreach (@array) {
   #connect, and do stuff
continue {
   $dbh->disconnect() (if defined $dbh)

Though opening and closing the db handle outside the loop is the
better way to do things ...

More information about the Melbourne-pm mailing list