|We don't bite newbies here... much|
work around for static vars and singleton constructors not inheriting properly?by Anonymous Monk
|on May 27, 2005 at 00:06 UTC||Need Help??|
Anonymous Monk has asked for the
wisdom of the Perl Monks concerning the following question:
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:
where base has a Class::MethodMaker setup like this:
And I when write code like this:
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.