use vars qw($sth); .... # And then in the function $sth || = $dbh->prepare($sql);