Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
then in other file i can just do :package MyDB; use strict; use warnings; my $DBH; sub new { return bless {}, shift; } sub dbh { if ( $DBH ) { # return $DBH print "found existing ",$DBH; } else { $DBH = 'my handler'; print "init dbh"; } } 1;
i am running the code under plain CGI. i made the $DBH as class variable in order to cache the same $dbh if i need to use it somewhere else in the same program. dbh caching works but i sort of reaching the solution by trying/guessing but don't know exactly why.use MyDB; { MyDB->new->dbh } # init dbh { MyDB->new->dbh } # got the cached dbh
can someone please explain to me?
by the way, is it a good practice for caching dbh this way? will it still work under mod_perl without using Apache::DBI?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: OO question
by kyle (Abbot) on Mar 18, 2007 at 00:35 UTC | |
by Anonymous Monk on Mar 18, 2007 at 02:02 UTC | |
by ikegami (Patriarch) on Mar 18, 2007 at 02:11 UTC | |
|
Re: OO question
by rhesa (Vicar) on Mar 18, 2007 at 00:32 UTC | |
|
Re: OO question
by Joost (Canon) on Mar 18, 2007 at 02:20 UTC | |
by rhesa (Vicar) on Mar 18, 2007 at 03:25 UTC | |
|
Re: OO question
by diotalevi (Canon) on Mar 18, 2007 at 00:37 UTC | |
by Joost (Canon) on Mar 18, 2007 at 02:11 UTC | |
by diotalevi (Canon) on Mar 18, 2007 at 03:30 UTC | |
by whereiskurt (Friar) on Mar 19, 2007 at 02:32 UTC |