in reply to Re: Re: Re: DESTROY and DBI
in thread DESTROY and DBI

(Edit: I don't believe _dbh goes away before $self does, I think DBI is unloaded before the global cleanup happens; that's why I'm suggesting this END{} fix...)

Hmmm, you don't need the total encapsulation features of the FlyweightWrapper, either...

I'm betting packages get unloaded correctly. What happens if you add a hash at the package level in your class called instances, and do this:

# private hash of instances my %instances; #... sub new { # ... build up $self, bless it, do whatever $instances{$self}=1; # ... whatever else you need to do... } #... sub DESTROY { # don't double-destroy return unless $instances{$self}; #... do whatever $instances{$self}=0; } END { foreach my $instance(keys %instances) { $instance->DESTROY() if $instances{$instance}; } }
Offhand, I can't remember whether calling DESTROY directly is ok or not, but if not, you could get the same effect with a helper sub that DESTROY calls.
--
Mike

Edit: I forgot to check $instances{$self} at the top of DESTROY