in reply to Sharing DBI handler between several modules
In the past I have used a 'Singleton Class' to do a similar sort of thing, something like this:
(This is cut straight from working code so is missing part). Then each place the DB connection is needed they can call UsageDB->new() and use handle() to get the database handle.sub new { my ( $class ) = @_; + if (! defined $UsageDB::_instance ) { $UsageDB::_instance = $class->_init(); } + return $UsageDB::_instance; } + sub _init { my ( $class ) = @_; + my $self = bless {}, $class; + # get configuration for connection $self->{_handle} = DBI->connect($dsn,$user,$pass, {PrintError => 1, RaiseError => 1} +); return $self; } + =item handle + Return the connected DBI handle to the usage DB + + =cut + sub handle { my ( $self ) = @_; + return $self->{_handle}; }
The advantage of doing this is that the modules that use this don't have to know the implementation details at all and you don't have to interfere with any other packages namespace.
/J\
|
|---|