I guess your intention is to make a single connection for use by other modules during a CGI call?
I use a piece of code like this
{ package Common; my $dbh = 0; sub getDbh { if ($dbh == 0) { # do all the stuff you said above to connect ... } return $dbh; } sub END { # print "disconnecting .."; $dbh->disconnect(); } } # other stuff .... 1;
The $dbh is now privately scoped and cannot be accessed excpet by the method calls provided. You also don't have to worry about disconnecting, it happens at the end of the script automatiocally. (Knowing Perl, it would clean up the filehandle safely when done anyhow, but it's good practice to do it yourself.)
In reply to Re: Problem creating a singleton wrapper around DBI
by danmcb
in thread Problem creating a singleton wrapper around DBI
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |