Hi,

I writing an object oriented framework in Perl. After thorougly digesting Conway's Object Oriented Perl, I decided to build my classes around Class::MethodMaker .

However I am having a problem with static variables and also with singleton constructors. If I make three classes, base, d1, and d2 such that:

d1 ISA base d2 ISA base

where base has a Class::MethodMaker setup like this:

use Class::MethodMaker [ new => -singleton=>'new' ];

And I when write code like this:

my $a = d1->new() my $b = d2->new()

I end up with $a and $b pointing to the same instance of d1!

This makes sense; the singleton constructor in base is allowing only one instance of base to ever exists. But what I want is to have only one instance of d1, AND one instance of d2. I want the singleton constructor to inherit "properly" (as I see it).

Obviously I can make singleton constructors in the derived classes; however I am trying to keep the code in the derived classes as simple as possible, so the users of the framework don't have be burdened with any more than nessecary.

SO THE QUESTION: Is there a proper way to make the singleton constructor inherit like I want it to?

Also I have the same problem with -static variables created with Class::MethodMaker. I assume it's the exact same problem since singleton constructors are built around static (class) variables.

I have my own workaround now; I make my own singleton constructor which keeps a static reference to a hash where the keys are derived class names and the variables are existing instances of those classes.

That works pretty well but I'd like to find a way to do it more cleanly if possible.

Thanks in advance.


In reply to work around for static vars and singleton constructors not inheriting properly? by Anonymous Monk

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.