The problem with not using weaken has been explained.
But there's also a problem with using weaken.
sub d { my $dir = Practice::Dir->new; my $file = Practice::File->new; $dir->add_file($file); $file->add_dir($dir); return $dir; } sub f { my $dir = Practice::Dir->new; my $file = Practice::File->new; $dir->add_file($file); $file->add_dir($dir); return $file; } # This works fine. # `$dir` is a dir with one file. my $dir = d(); # XXX This fails. # `$file`'s dir is `undef` even though it wasn't before we returned. my $file = f();
The correct solution could be neither.
|
|---|