# declare a db handle # get a lock on the table # call a subroutine from the module that does something to the db # unlock the table #### use dbmodule; my $dbh; BEGIN {$dbh = DBModule_Start} END {DBModule_Finish} # then do various stuff including $sth = $dbh->prepare("BLAH BLAH") die $dbh->errstr; $sth->execute or die $dbh->errstr; DBModule_Sub1; #### my $module_dbh; sub DBModule_Start { unless ($module_dbh) { $module_dbh = DBI->connect("DBI:mysql:database=db_name", "user_name", "password"); } return $module_dbh; } sub DBModule_Sub1 { $module_dbh->do{'BLAH BLAH BLAH'} or die $module_dbh->errstr; } sub DBModule_Finish { $module_dbh->disconnect; }