Nomis52 has asked for the wisdom of the Perl Monks concerning the following question:
mysql> select * from test_a; +-----+ | oid | +-----+ | 1 | +-----+ mysql> select * from test_b; +-----+------+------+ | oid | aoid | flag | +-----+------+------+ | 1 | 1 | 0 | | 2 | 1 | 0 | | 3 | 1 | 1 | +-----+------+------+ 3 rows in set (0.01 sec)Where aoid references and oid in test_a .
Below is the code for the two classes
Testing2.pm
use strict; use warnings ; package Testing2; use base 'Testing'; Testing2->table('test_a'); Testing2->columns(Primary => qw/oid/ ) ; Testing2->has_many( bs => 'Testing3', 'aoid'); sub flagged { my $self =shift ; my @bs = $self->bs( flag => 1 ) ; return shift @bs ; } sub flagged2 { my $self =shift ; unless(defined($self->{f})) { my @bs = $self->bs( flag => 1 ) ; $self->{f} = shift @bs ; } return $self->{f} ; } sub DESTROY { my $self = shift; print "Destroying a " . $self->oid . "\n"; } 1;
Testing3.pm
Now I need to be able to find the particular B that corrosponds to a particular A where flag is 1. So in class A I have:use strict; use warnings ; package Testing3; use base 'Testing'; Testing3->table('test_b'); Testing3->columns(Primary => qw/oid/ ) ; Testing3->columns(Essential => qw/aoid flag/ ) ; Testing3->has_a( aoid => 'Testing2'); sub DESTROY { my $self = shift; print STDERR "Destroying b " . $self->oid . "\n"; } 1;
But in my code, flagged is called many times per execution and I'd really like to be able to cache the results of the search so it's only performed once. I tried the following:sub flagged { my $self =shift ; my @bs = $self->bs( flag => 1 ) ; return shift @bs ; }
The problem is now that the objects a and b no longer get destroyed when the execution finishes because the reference count never hits 0. This is causing problems under mod_perl.sub flagged2 { my $self =shift ; unless(defined($self->{f})) { my @bs = $self->bs( flag => 1 ) ; $self->{f} = shift @bs ; } return $self->{f} ; }
Is there a way to ensure the objects get destroyed or alternatively a better way to do this ?
Cheers, Simon N
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Class DBI and reference counting
by chromatic (Archbishop) on Jan 10, 2006 at 23:07 UTC | |
by Nomis52 (Friar) on Jan 10, 2006 at 23:43 UTC | |
|
Re: Class DBI and reference counting
by perrin (Chancellor) on Jan 10, 2006 at 23:20 UTC |