I's probably worth noting that your code as-is is halfway between "has-a" and "is-a". I think you do want "is-a", as the other posters have noted. If it were the case that you might at a later date want to delegate method calls to a different base class, you'd do something like this:
package BasicX::AAA1; sub new { my($class, $delegate) = @_; my $self = {}; bless $self, $class; $self->delegate($delegate || BasicX->new()); return $self; } sub delegate { my $self, $new_delegate_instance) = @_; $self->{_delegate} = $new_delegate_instance if defined $new_delega +te_instance; return $self->delegate; } sub delegated_method { my $self = shift; if $self->delegate->can('delegated_method') { $self->delegate->delegated_method(); } else { # handle it yourself } }
So now the instance has a instance of the delegate, and decides to pass control off to it as appropriate. If you have a standard subclass "is-a" relationship; the subclassed methods always have to resolve to the parent class. If you have a delegated "has-a" relationship, then any given instance of the class can be set up to have a delegate to handle methods selectively - and that delegate can be any class you want, as long as it can execute the delegated method. Also note that you can, with this setup dynamically change the delegate after the "subclass" is instantiated.

In reply to Re: Multiple instances of the same base class by pemungkah
in thread Multiple instances of the same base class by jck000

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.