in reply to Class DBI and reference counting

If you're using a modern enough Perl (5.6.1 or later, I believe), use Scalar::Util's weaken() function to create weak references in your cache. The documentation for the function shows a similar application.

Replies are listed 'Best First'.
Re^2: Class DBI and reference counting
by Nomis52 (Friar) on Jan 10, 2006 at 23:43 UTC
    I acutally tried weaken before posting:
    sub flagged2 { my $self =shift ; unless(defined($self->{f})) { my @bs = $self->bs( flag => 1 ) ; $self->{f} = weaken(shift @bs) ; } return $self->{f} ; }
    The problem is on the first call to the function it works as expected, but the second time it's called the object has already gone out of scope and been destroyed resulting in an undefined value returned.