in reply to Re: Should I use weaken on an object attribute containing a reference to an object which contains reference back to original object?
in thread Should I use weaken on an object attribute containing a reference to an object which contains reference back to original object?

> The correct solution could be neither.

As I already said, this is a design problem of this "file system simulation" , and not weaken's

I have trouble imagining a file system where dirs and files are deleted implicitly after "falling out of scope".

Personally I would make deletion explicit with ->remove methods, and keep strong refs of the objects only in an %instance hash of the class, all other internal refs must be weak. Like this the refcount can not become 0 unless explicitly wanted.

Now if there are still other external refs when ->remove is called, this must be resolved.

Either they are all weak, because the constructor returned them weak and they'll become automatically undef after distruction.

Or they are strong and the refcount is checked by ->remove and throws an error "can't remove, still in use"

Bottom line: This scenario is too complicated to discuss weaken

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

  • Comment on Re^2: Should I use weaken on an object attribute containing a reference to an object which contains reference back to original object?
  • Download Code

Replies are listed 'Best First'.
Re^3: Should I use weaken on an object attribute containing a reference to an object which contains reference back to original object?
by hippo (Archbishop) on Jan 23, 2024 at 14:48 UTC
    I have trouble imagining a file system where dirs and files are deleted implicitly after "falling out of scope".

    I've re-read the OP. It doesn't seem to mention anything about actually operating on a filesystem. Rather it's just an example of collections of related objects or references.


    🦛

      The OP wants a general advice for weaken with an ambiguous OO design.

      My point in 3 posts already is that it's not possible to answer a fuzzy question, because different readers will have different use cases in mind.

      That's a bad ground to discuss weaken thoroughly. Bad question, useless answer.

      A conclusion like

      > > > But there's also a problem with using weaken.

      looks problematic to me.

      FWIW: The only general advice I found in the perldocs on weaken is to use it on the shorter lived ref.

      Update

      Creating a decent discussion/tutorial for weaken would be nice.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

Re^3: Should I use weaken on an object attribute containing a reference to an object which contains reference back to original object?
by ikegami (Patriarch) on Jan 23, 2024 at 00:51 UTC

    What the hell are you talking about? I didn't say anything about automatic file deletion. The question being asked is if the OP should be using weaken or not in a data structure that mirrors a file system.

    haj pointed out that you get memory leaks if you comment out weaken. And I pointed out that you can end up with premature deallocation if you don't. The point I was making is that just adding weaken is not a solution, or at least not one without downsides.

    Perl's reference-counting GC makes bidirectional data structures very hard to implement.

      > What the hell are you talking about

      That the OP's premise is unrealistic.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        You didn't reply to the OP.