in reply to Using a single DBI handle in a script and a module

I handled this once by having the module create and tear down the db handle, via BEGIN and END clauses, and when I needed the use the handle directly in my main script, I simply used something like this:

package My::Module; my $db; BEGIN { $db = DBI->connect($blah, $blah, $blah) or die "...\n"; } END { $db and $db->disconnect; } sub db { $db; }

Which meant that I could grab the handle whenever I needed to do some ad-hoc querying, and have the module take care of the fiddly bits, as well as all the standard db stuff it was supposed to do anyway, all nicely encapsulated. You can actually do what appears to be rather weird...

my $ss = My::Module::db->prepare($sql);

I.e., nary a scalar in sight, but it does what you expect, without having to explicitly save the $db reference in a scalar. It's not exactly walking into some-one's living room without being invited, but it's close to leaning on the window sill and watching the TV set.

I'm not sure I'd do this in a codebase with multiple people, but it worked well enough for me.

--
g r i n d e r