I make a distinction between connections to outside processes (like databases and files) and internal things (like variables and the like).
The thing that bit me regarding this (because I always used to work with implicit closes) was prepare_cached(). The following snippet, within a function that's called repeatedly, will not work as expected:
sub oft-used_func
{
....
if ($do_db_call)
{
my $sth = $dbh->prepare_cached($sql) || die;
$sth->execute(@bind_vars) || die;
while ($sth->fetch)
{
....
}
# Use implicit close here
# $sth->finish;
}
....
}
The second time you do that DB statement, using prepare_cached() and not having called finish(), you will have a DBI error. Thus, I explcitly close all external things, but implicitly close all internal things.
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. |