[pm-h] When to use "weaken"?

G. Wade Johnson gwadej at anomaly.org
Fri Jan 24 07:20:50 PST 2014


On Fri, 24 Jan 2014 06:44:41 -0800 (PST)
"Michael R. Davis" <mrdvt92 at yahoo.com> wrote:

> >>
> >> If I'm understanding you, that's the definition of a circular
> >> reference.
> >>
> >>     parent -> child -> parent
> >
> 
> Can't locate object method "cfg" via package "XXX::Profiles::Profile"
> at /usr/lib/perl5/vendor_perl/5.10.0/XXX/Profiles/Profile.pm line
> 112. So, weaken must not work like I think.  The child has lost the
> link to the parent.  I guess the child to parent link is more
> important than the parent to child relationship.  Maybe I need to
> weaken the parent ref.  Maybe as Todd suggested I need to re-think
> the architecture. For now I'm going to exit after a few loops and
> restart on cron with Sys::RunAlone or something like that. Maybe I
> need to use a DESTROY block and undef($self->{"parent"}) in it.
> Anyone know the gotchas on DESTROY? Thanks, Mike

I'm not sure, but your initialize function raises a few red flags for
me. It would depend on how you use it, but it appears to replace
everything in the $self hash. Are you sure that's what you want? What
does your new method look like?

From what I could see, you have a parent setter method as well as
initialize. This is probably a bad idea. I prefer to keep as much of my
internal data initialize-only or only handled by object methods. Adding
setter-type methods seems to spread responsibility around and increase
the possibility of stomping on class-invariants.

I've used the DESTROY/undef approach before without problems. I know a
few people have reported difficulties with DESTROY blocks, but I've
never seen them myself.

Getting back to Todd's advice, why do you need the parent link in the
child?

I'm not saying that it is wrong (I've written quite a bit of code that
does things like this.), but it is a potential source of memory issues.
So, it seems reasonable to make certain that it is needed.

Sorry for the blast of questions. This seems like an interesting
problem and I'd like to help you get it fixed.

G. Wade
-- 
In theory, there is no difference between theory and practice. But, in
practice, there is.                   -- Jan L.A. van de Snepscheut


More information about the Houston mailing list