my $dbh; sub dbConnect { return if defined $dbh; # exit if we are already connected. my $dsn = "DBI:mysql:database:localhost"; my $db_user_name = 'username'; my $db_password = 'password'; $dbh = DBI->connect ($dsn, $db_user_name, $db_password, {RaiseError => 1}) || die("cannot connect to DB: ".DBI::errstr."\n",$dbh); } sub foo { # Can still see $dbh in here dbConnect(); my $sth = $dbh->prepare(...); }