jck000 has asked for the wisdom of the Perl Monks concerning the following question:

How can I subclass 2 separate instances of 2 modules that inherit and use the same base. Similar to a factory class but separate instances for each call. I'm looking at using this approach for a module in mod_perl, fcgi, cgi and pl environments.

I've written out a basic outline of what I'd like to do below.

Thank you in advance for your thoughts, suggestions and help.
Jack

package BasicX; sub new { ... } sub someMethod1 { ... } sub someMethod2 { ... } 1; # --------------------------- package BasicX::AAA1; use BasicX; sub new { my $class = shift; my $args = shift; my $newclass = BasicX->new( { DEBUG => $args->{DEBUG}, SETTTING1 => $args->{SETTING1}, } ); return $newclass; } 1; # --------------------------- package BasicX::BBB1; use BasicX; sub new { my $class = shift; my $args = shift; my $newclass = BasicX->new( { DEBUG => $args->{DEBUG}, SETTTING1 => $args->{SETTING1}, } ); return $newclass; } 1; # ------------------------------- script.pl ---------- #!/usr/bin/perl use BasicX::AAA1; use BasicX::BBB1; my $ObjAAA = BasicX::AAA1->new( { DEBUG => 1, SETTING1 => "this is it" + } ); my $ObjBBB = BasicX::BBB1->new( { SETTING1 => "separate value" } ); $ObjAAA->someMethod(); ...

Replies are listed 'Best First'.
Re: Multiple instances of the same base class
by Riales (Hermit) on Feb 10, 2012 at 22:33 UTC

    Usually, whenever you call a package's new method, it should already be returning a brand new instance of itself, so I would think what you have is already mostly correct.

    Some other things I noticed though...

    I think instead of:

    use BasicX;
    You actually want:
    use base BasicX;

    This is because use base establishes a ISA relationship for between your current class and the specified base class(es).

    Beyond that, change this section:

    my $newclass = BasicX->new(...);
    To something like this:
    my $newclass = $class->SUPER::new(...);
    That should work properly.
Re: Multiple instances of the same base class
by kennethk (Abbot) on Feb 10, 2012 at 22:34 UTC
    At the level of object-orientation that you are using, inheritance is defined using the @ISA array. For details, see Inheritance in perltoot. I might rewrite one of your classes as
    package BasicX::AAA1; use BasicX; our @ISA = qw(BasicX); sub new { my $class = shift; my $args = shift; my $newclass = $class->SUPER::new( { DEBUG => $args->{DEBUG}, SETTTING1 => $args->{SETTING1}, } ); return $newclass; } 1;
    Note the use of the SUPER pseudoclass -- see Overridden Methods in the same document. If your parent class is incautious about its blessing, your object will be in the wrong class, but that would have already been true with your old code.
Re: Multiple instances of the same base class
by pemungkah (Priest) on Feb 12, 2012 at 06:19 UTC
    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.