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

Jacinta Richardson jarich at perltraining.com.au
Sat Jul 28 00:09:00 PDT 2007

Leigh Sharpe wrote:

> Question is, if some_condition is true, the loop ends and $dbh goes out
> of scope. Does disconnect() get called on $dbh automatically? Or do I
> have a database connection still, even though I can't actually use it?

disconnect gets called automatically when the handle goes out of scope.  This
happens because the object is destroyed.

> On a related note, does disconnect() get called automatically when a
> program ends? Even if not explicitly called?

Yes.  The same as above, whenever the database handle goes out of scope
(possibly at the end of your program) its DESTROY method is called and this
calls disconnect for you if the handle is active.

On another note though, are you connecting to the same database for each array
item?  If so, then your program will be faster if you connect first, do the
foreach and then disconnect.

All the best,


   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |

More information about the Melbourne-pm mailing list