in reply to Re: weak-reference set internally
in thread weak-reference set internally

within $bar->a_method, i need to check a property of the parent, ie, in Foo.

Hence the reason for getting the relevant Foo object, checking it, all good so far, but then want to also keep it for future use in case i need the parent again.

Replies are listed 'Best First'.
Re^3: weak-reference set internally
by Anonymous Monk on Sep 30, 2010 at 09:42 UTC
    but then want to also keep it for future use in case i need the parent again.

    So don't use weak references?

      true, but in other cases i may well have created the Foo object outside of the creation of a Bar, and ALSO store it internally within Bar (for ease of use within Bar's methods), hence the reason for the weak-ref.

      This is the issue...

        You either need the parent to still be around. Then you need a strong reference. Or it is OK if the parent goes away. Then you need a weak reference.

        If you have a strongly referenced relation between parent and child, you will need to call a special method (like, ->free()) to release the references you hold explicitly. As Perls memory management is based on reference counting, there is no other, implicit way to find out that a set of memory objects is not referenced from anywhere on the outside.

        If you want a bit more sanity in managing such object forests, you can keep a reference to a "master" object for each tree, whose main purpose is to call ->free() on the topmost parent object of that tree.

        I can't quite envision a scenario where you need this, but either you want Foo available or you don't. Pick one.