I was reading the excerpts of Damian Conway's OO Perl book that are on his website (while waiting for my paycheck to clear so I can order the book itself). Among lots of other useful things, he describes a technique for data encapsulation using flyweight objects:
package Foo; { my @foos; sub new { push @foos,{ name => $_[1] }; my $obj = $#foos; return bless \$obj, $_[0]; } sub getname { return $foos[${$_[0]}]->{name}; } }
His example was more useful than the "foo" class above, but this shows the basic idea. The actual objects are held in a dataspace lexically scoped to the package, and is inaccessible outside the package. Outside the package, all the rest of the world gets is a blessed handle to the data locked away in the package, and can't see anything to directly manipulate.

There does seem to be some limitations to this approach (such as difficulty doing inheritance, etc), but the one major question I have is with GC.

The actual objects are referred to by the array @foos, and only the array @foos. When the blessed handle goes out of scope, the object won't be cleaned up, because it is still being referred to by @foos. Only when the program exits will the object itself be cleaned. This could be a problem.

The obvious solution would be to add something like:

sub DESTROY { $foos[${$_[0]}] = undef; }
to the Foo class. Then, when the handle goes out of scope, Foo::DESTROY gets called, and all is well.

What I'm concerned about is cases where the handle gets copied before it goes out of scope:

sub usesfoo { my ($name,$population) = @_; my $foo = new Foo($name); my %ret = { name => $name, pop => $population, foo => $foo }; return \%ret; }
At the end of usesfoo, $foo falls out of scope. Is Perl smart enough to not call DESTROY $foo at that point, or will the copy of $foo saved in the return value be rendered useless?

Damian Conway's example does not include a destructor, so I can't tell by his example -- his looks to me like it leaks memory.


In reply to Flyweight Objects and garbage collection by BlaisePascal

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.