in reply to SOLVED: Trying to DESTROY() a (closure-wrapped) object

You never used the word "closure", which makes me think you may not actually get what is happening. In most cases, a lexical is freed (and the reference count decremented if the lexical is a hard ref) at the end of a function call:

sub mock { my $self = shift; # ref count of the object +1 # code here } # $self goes out of scope, ref count of the object -1
But since your new One::foo sub is created inside that lexical scope, it prevents $self from being freed (because the created sub "closes" over it, which means it is a closure that keeps the lexical alive for its own use).
sub mock { my $self = shift; # ref count ++ return sub { $self->{Thing}; }; # $self is still held by the sub, so it is not freed }
So this is the reference that you want to weaken, just add weaken $self; just before *One::foo = sub { $self->{x} = 'x'; return "baz\n"; }; and you will get the expected result.

Replies are listed 'Best First'.
Re^2: Trying to DESTROY() an object
by stevieb (Canon) on Dec 08, 2015 at 16:55 UTC

    I'm sorry that I neglected to explain this better. I should have mentioned the closure... I did and do understand what's happening, I've just never been in a position to where I needed to reduce a refcount manually before :)

    I appreciate all the great feedback. I could have sworn I had tried weakening $self, but apparently not because that does the trick and I get the results I'm after.

    Cheers!

    -stevieb

Re^2: Trying to DESTROY() an object
by kennethk (Abbot) on Dec 08, 2015 at 23:50 UTC
    Note Re^3: Trying to DESTROY() an object for a bug in your proposed solution.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.