I believe you'll encounter that error everytime that $dbh leaves scope..
What I normally do to get around this is check the "environment" first, after I've verified my program has all the options and info needed to run, I'll open up a global $DBH, and then I'll do an end block like:
END {
$DBH->disconnect;
# whatever else you want to clean up neatly
}
which I guess is a cheap hack, but works..
-brad..