The global handle is much simpler to implement. But consider what you will have to do if you need to run subqueries on your first result set. The following example is a bit contrived, but it's easy to imagine an instance with nested procedures that can produce same problem.
use strict;
my $dbh = DBI->connect(XXXX);
runSomeQueries();
$dbh->disconnect();
sub runSomeQueries
{
my $query = "select * from table";
my $sth = $dbh->prepare($query);
if ( $sth->execute() )
while (my $data = $sth->fetchrow_hashref )
{
runSomeSubQueries(some$$hash{ItemID});
}
}
sub runSomeSubQueries
{
my $itemID = shift;
my $query = "select * from detail_table where ItemID = ?";
my $sth = $dbh->prepare($query);
$sth->execute($itemID);
}
I like to use a factory to ask for a DBI handle.
Like so.
-HTH