That are some unfortunate ways to say it.

The purpose of weaken is to assist Perl's garbage collection mechanism. This mechanism returns memory to the pool when there are no more references to it. It is not possible to demonstrate this with just the classes: Let me add some main code. Warning: This code runs forever!

package main; while (1) { my $dir = Practice::Dir->new; my $file = Practice::File->new; $dir->add_file($file); $file->add_dir($dir); }

If you comment out your call to weaken and run the code, then you'll see the memory consumption increase slowly, but steadily.

Explanation: At the end of each iteration, the variables $dir and $file go out of scope. The object behind $dir will not be freed, because it is referenced to by the $file object - and vice versa. If you weaken the dir attribute of the file, then the object behind $dir can be freed, and since this is now gone, the object behind $file can also be freed.


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? by haj
in thread Should I use weaken on an object attribute containing a reference to an object which contains reference back to original object? by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.