http://www.perlmonks.org?node_id=460911

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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.