{ package Common; my $dbh = 0; sub getDbh { if ($dbh == 0) { # do all the stuff you said above to connect ... } return $dbh; } sub END { # print "disconnecting .."; $dbh->disconnect(); } } # other stuff .... 1;