I agree: I use a similar style. If I expect to only have a single database handle for the program, I'll keep it global. On the other hand, if I expect to have lots of handles running around, I move all the connection code into sub-routines (e.g. one sub per transaction), like this:
InsertRecord($val1, $val2);
my @array = RetrieveValues($val2, $val3);
sub InsertRecords {
my $dbh = DBI->connect(xxxx);
# stuff
}
sub RetrieveValues {
my $dbh = DBI->connect(xxxx);
# stuff
}
This is undoubtably inefficient, but it is very clear, and is the first step before I move the subs off into their own module (e.g. StandardTransactions.pm). YMMV |