http://www.perlmonks.org?node_id=367685


in reply to Avoiding too many DB queries

The comments above suggest that you cut out the repeated database connections. This code might be a good place for you start:
sub dbi_connect { my $self = shift; return $$self{dbh} ||= DBI->connect($dbi,$u,$p,{'RaiseError' => 1}); } sub dbi_disconnect { my $self = shift; return 1 unless $$self{dbh}; my $dbh = $self->dbi_connect; $dbh->disconnect || die $dbh->errstr; $$self{dbh} = undef; return 1; }
Obviously you should only call dbi_disconnect when you are finished with the db handle. Alternatively just let it get disconnected automatically when the object goes out of scope.

--tidiness is the memory loss of environmental mnemonics