pacakge MyProject::DBH; use DBI; my $DBH; # lexical scope outside the constructor # makes it available to all calls to new(). sub get_dbh { if ref($DBH) eq 'DBI::db') { # dbh already created, so return it return $DBH; } else { # connect to the DB and return the handle # (assume $user, $pass and $dsn come from somewhere) $DBH = DBI->connect($dsn, $user, $pass); return $DBH; } }