in reply to Re: OO question
in thread OO question
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;
|
|---|