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; }