Thanks - I discussed the problem with a colleague, he suggested using a wrapper class, I thought some autoload magic could make it transparent - as ever, someone on CPAN already has a solution :-)

As I understand it, Object::Destroyer will by default call the DESTROY method on the object it guards - this may close resources, but won't cause other references to the object to become undefined, which is what I was trying to achieve. Other packages had a file scoped reference to the object in question, which would become stale, if my understanding is correct.

#!/usr/bin/perl -w use strict; our $other; my $main = foo->new(); print "undef object: ";<STDIN>; undef $main; print "exit: ";<STDIN>; package foo; use Object::Destroyer; sub new { my $package = shift; my $self = {}; bless $self => $package; $::other = $self; my $ret = Object::Destroyer->new($self); return $ret; } sub DESTROY { my $self =shift; print "DESTROYing $self\n"; #undef $self # doesn't help }

outputs:

undef object: DESTROYing foo=HASH(0x817f9d0) exit: DESTROYing foo=HASH(0x817f9d0)

I had a look at the packages with file scoped references, and realised that I was just creating problems for myself - if I made objects call the constructor in question from a lexical/function scope, pretty much all of my problems go away - lexical scoping takes care of everything.


In reply to Re^2: return weak reference by flipper
in thread return weak reference by flipper

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.