# a subroutine returns a ref to a database handle... my $pdb = &openDatabase(); # the ref to the db handle is passed to a subroutine as an # optional hash element... &doSomething(DB=>$pdb,FOO=>'bar'); # the subroutine assigns the ref to a scalar if it was # passed, or directly obtains one of its own if it wasn't, # does its thing, then closes the db handle if # it opened it... sub doSomething { my(%param)=@_; # we assign the ref to $rdb if it's defined, or open the # database for the first time if it's not my ($rdb) = ((defined($param{'DB'})) && ($param{'DB'})) ? $param{'DB'} : &openDatabase(); # the subroutine does something useful... $$rdb->disconnect if(!(defined($param{'DB'}))); return('done'); }