Greetings Monks, I'm using Class::DBI with two classes, A and B with A having many Bs. The database is set up like so:
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

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;
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:
sub flagged { my $self =shift ; my @bs = $self->bs( flag => 1 ) ; return shift @bs ; }
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 flagged2 { my $self =shift ; unless(defined($self->{f})) { my @bs = $self->bs( flag => 1 ) ; $self->{f} = shift @bs ; } return $self->{f} ; }
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.

Is there a way to ensure the objects get destroyed or alternatively a better way to do this ?

Cheers, Simon N

In reply to Class DBI and reference counting by Nomis52

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.