I could be wrong, but the memory problem doesn't seem to be unfixable.

The packages the OP is creating would be small (because all the methods are inherited). Also you could manually get rid of the package on destruction using something akin to this:
package FOO; sub asdf{shift ; reverse @_}; package main; # need an object so that functions get bound at runtime my $o = bless {}, 'FOO'; print $o->asdf(1..10); # ok undef %FOO::; print $o->asdf(1..10); # function cannot be found
If you were extremely memory concious, you could put each instance's fields in the new package (why use another hashref?)

If you wanted to be realy sneaky, you could have the object as a globref to it's own package and the decrement the refcount on the glob by one. When the instance goes out of scope, the glob's refcount drops to 0 and cleans itself up.

At least thats the way I understand packages. Please enlighten me if I'm wrong.

Udate: The code presented above doesn't work. Here's why: every time perl sees the name *Foo:: or %Foo:: in the source, it increments the ref count of it (similar in concept to closing over a value, but for globals). Therefore undef *Foo:: inadvertently creates another reference to the value it is trying to destruct.

Here's the code I used to come to that conclusion.
use strict; use Inline 'C'; $\ = "\n"; my $x; BEGIN { my $n = 100; $x = eval 'sub {' . ('%Foo::;' x $n) . '}'; } print refcount(*Foo::); # prints $n + 2 __END__ __C__ int refcount(SV* x) { return SvREFCNT(x); }

In reply to Re^2: Overloading different instances differently. by plobsing
in thread Overloading different instances differently. by kyle

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.