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

Leigh Sharpe lsharpe at pacificwireless.com.au
Thu Jul 26 21:50:09 PDT 2007


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

Isn't $dbh already out of scope once the continue block is called?

Leigh Sharpe
Network Systems Engineer
Pacific Wireless
Ph +61 3 9584 8966
Mob 0408 009 502
Helpdesk 1300 300 616
email lsharpe at pacificwireless.com.au
web www.pacificwireless.com.au

-----Original Message-----
From: Gautam Gopalakrishnan [mailto:thatha at gmail.com] 
Sent: Friday, 27 July 2007 2:27 PM
To: melbourne-pm
Subject: Re: [Melbourne-pm] DBI connect()'s, scope and disconnects


> 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 ...
Melbourne-pm mailing list
Melbourne-pm at pm.org

More information about the Melbourne-pm mailing list