retaining original class hierarchy

Eric Wilhelm scratchcomputing at
Sun Aug 6 16:46:56 BST 2006

# from Dirk Koopman
# on Sunday 06 August 2006 07:13 am:

>sub new {
>    my $class = shift;
>    my $obj = _get(shift);   # returns a Foo::TypeB
>    my $objclass = ref $obj;
>    our @{$class}::ISA = $objclass;
> ...
>package Bar; use base 'Foo'; my $obj = Foo::new('main');

Why set @main::ISA?  Even with "Foo->new('main')", you would be setting 
@Foo::ISA, which means your factory class is breaking the tree for any 
previously created (and still existing) objects at runtime -- which is 
going to hurt.  If this is a compile-time factory, that might be a 
valid approach (but could be simpler), but that's not what it sounds 

A factory class should only be returning an object of some type which 
could operate standalone.  e.g. until you can "my $obj = 
Foo::TypeB->new(); $obj->do_stuff" (such as in a test), building a 
factory will be a pain.

>  In other words return, dynamically, objects with their own 'private'
>  @ISA setting. 

If (iff) you mean "every instance is in its own class with a slightly 
different inheritance tree", you'll need to generate the class (use an 
un-parseable name like 'my $classname = "LotsaObjects::--$id"') and set 
@ISA in it.  (Actually, all you need to do is make-up a name, bless the 
object as such, and set an @ISA.)

But, you probably just want to create 2-3 real classes and have your 
factory return an instance of one.

Speak softly and carry a big carrot.

More information about the mailing list