I wouldn't do it that way. If your just using packages (and not OO), then look into DBI's connect_cached (hopefully your DBD supports it) and let each package/sub make it's own connection. Otherwise, I would pass the dbh to the constructors (and/or methods).
Hue::Foo::do_something( dbh => $dbh, data => $data );
update: Oops ... forgot to say this is an area where an OO approach would work best.
package Hue::SQL; sub new { return dbi->connect blah blah; } package Hue::Foo { sub new { my $class = shift; my %args = @_; my $self = {}; bless $self, $class; $self->{dbh} = $args{dbh}; $self; } sub do_something { my $self = shift; my %args = @_; $self->{dbh}->do( "sql query using $args{data}" ); } package main; use strict; use warnings; use Hue::SQL; use Hue::Foo; my $dbh = Hue::SQL->new(); my $foo = Hue::Foo->new( dbh => $dbh ); $foo->do_something( data => $data );
In reply to Re: Sharing DBI handler between several modules
by derby
in thread Sharing DBI handler between several modules
by Hue-Bond
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |