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

if I delete the Dir, the Files can no longer reference the Dir

It's not only a question of deletion. The reference to Dir within File will disappear as soon as the Dir object gets out of scope, which is a Bad Thing™

use strict; use warnings; use Data::Dump; my $file = Practice::File->new('foo'); { my $dir = Practice::Dir->new('bar'); $file->add_dir($dir); } dd $file; __DATA__ bless({ dir => undef, name => "foo" }, "Practice::File")

Greetings,
-jo

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
  • Comment on Re: 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^2: Should I use weaken on an object attribute containing a reference to an object which contains reference back to original object?
by LanX (Saint) on Jan 21, 2024 at 23:06 UTC
    That's one of the reasons why I wrote that it depends on design decisions

    For instance, a common concept in filesystems is that deleting a directory is only possible if it's empty. => rmdir

    Another that deleting a directory also deletes the containing files. => rm -r

    In both cases all "File" objects having weak references to the "Dir" should have been removed.

    If that's done automatically in a File:: DESTROY method or only allowed to happen after a $dir->rm call are other design decisions.

    The problem here is that the OP's question is to vague to be answered.

    If this is all hypothetical and intended for Practice:: instead for a real simulation of a file system, I'd say the topic is too ambiguous to discuss weaken

    Especially as I can't even tell that I know all subtle differences between different file systems. Hence there is no "natural" model to copy.

    Update

    At least, I don't know what a "directory getting deleted after going out of scope" would translate to in "normal practice" ...(?)

    Personally I wouldn't allow it to happen.

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