Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-24 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found