Hello Monks,

I am attempting to create a class hierarchy which, when new is called for the base class, the first parameter will allow a run time decision as to which derived class is required and instantiate that. Kind of like how DBI works. I would prefer to not specify all the classes required. Something like this:

use Astro::Observation; my $obs = Astro::Observation->new( "simple", ... ); my $obc = Astro::Observation->new( "complex", ... );
rather than this:
use Astro::Observation::Simple; use Astro::Observation::Complex; my $obs = Astro::Observation::Simple->new( ... ); my $obc = Astro::Observation::Complex->new( ... );
I don't have a clue how to go about doing this. I have Conway's book, but cannot figure out from there.

Can anyone suggest somewhere to look, or a pure perl module which uses this? I looked at DBI, but couldn't figure out how they accomplished this.

As you might be able to infer, this is my first attempt at using objects in Perl, and I'm barely treading water...

Thanks,
Rob


In reply to Runtime instantiation decisions by LogicalChaos

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.