retaining original class hierarchy

Dirk Koopman djk at
Sun Aug 6 15:13:27 BST 2006

I have a factory class called 'Foo'. It has several subclasses like:
'Foo::TypeA', 'Foo::TypeB' and so on. Each of these subclasses has a
different version of 'method'.

There a stash of objects of these different subclassed type such that a
function Foo::get('main') will return an object of, say, 'Foo::TypeB' -
BUT - you don't know what that object's class will be.

I now want to have something functionally like this: 

package Foo;

our %stash;

sub new
    my $pkg = shift;
    my $class = ref $pkg || $pkg;
    my $obj = _get(shift);   # returns a Foo::TypeB
    my $objclass = ref $obj;
    no strict;
    our @{$class}::ISA = $objclass;
    return bless $obj, $class;

sub _get
   return $stash{shift};

and I want to then have something like:-

package Bar;

use base 'Foo';

my $obj = Foo::new('main');

and have the inheritance work so that it looks at 'Foo::TypeB' (or
whatever Foo::new has set @ISA to for that object). In other words
return, dynamically, objects with their own 'private' @ISA setting.

I don't want to know which subclass of Foo that am dealing with in the
program code itself (at least at this point) because it may well change.


More information about the mailing list