Yeah, it's not at all uncommon of a pattern. It also happens to be quite easily done in perl. Remember that an object is just a reference blessed into a package (or, more to the point, blessed into the name of a package).

Something along these lines is a common approach:

sub new { my ($class, @args) = @_; my $destination_class = figure_destination_class(@args); # or whate +ver my $self = bless {}, $destination_class; $self->initialize(@args); return $self; }
One important note is that you'll want to move any actual initialization logic that might have been part of new out to another method (I called initialize), so that a subclass would have the ability to overload that logic, after the subclass is determined, and the object is blessed into that package.

There are some potential gotchas, of course. Mostly to do with the questions of what the potential sub-classes are, and if/whether you automatically load their code on the fly. For example, you could extend the above to something along the lines of this:

sub new { my ($class, @args) = @_; my $destination_class = figure_destination_class(@args); # or whate +ver # verify that the $destination_class is a valid package name # and a proper sub-namespace of $class, and detaint if ($destination_class =~ /^(\Q$class\E(?:::[a-zA-Z_]\w*)+)$/)) { eval "require $1" or die $@; } else { die "'$destination_class' is an invalid class name for sub-class +es of $class\n"; } my $self = bless {}, $destination_class; $self->initialize(@args); return $self; }
Which does some checking of the package name before loading it (generally people refer to this act as "detainting", as it has the side effect of allowing a taint-check-enabled script to still do this ok... if you construct it properly).
------------ :Wq Not an editor command: Wq

In reply to Re: OOP - redefining a class by etcshadow
in thread OOP - redefining a class by nmerriweather

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.