in reply to OO question

Others have already answered your question better about the specific domain of DBI objects and caching but I was also struck that you'd just reinvented the interface for Class::Singleton.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: OO question
by Joost (Canon) on Mar 18, 2007 at 02:11 UTC

      Right. It adds nothing in this particular case but for other similar problems when Apache *isn't* involved, it's nice to know the perl module that implements that interface.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^2: OO question
by whereiskurt (Friar) on Mar 19, 2007 at 02:32 UTC

    Just a little deeper than the CPAN search there is an exact Class:Singleton DBI implementation (scroll a page or two down):

    Class:Singleton DBI Example :)

    package MyApp::Database; use vars qw( $ERROR ); use base qw( Class::Singleton ); use DBI; $ERROR = ''; # this only gets called the first time instance() is called sub _new_instance { my $class = shift; my $self = bless { }, $class; my $db = shift || "myappdb"; my $host = shift || "localhost"; unless (defined ($self->{ DB } = DBI->connect("DBI:mSQL:$db:$host"))) { $ERROR = "Cannot connect to database: $DBI::errstr\n"; # return failure; return undef; } # any other initialisation... # return sucess $self; }

    The above example might be used as follows:

    use MyApp::Database; # first use - database gets initialised my $database = MyApp::Database->instance(); die $MyApp::Database::ERROR unless defined $database;