in reply to Problem creating a singleton wrapper around DBI
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.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problem creating a singleton wrapper around DBI
by danmcb (Monk) on Aug 23, 2005 at 11:40 UTC |