in reply to sub classing best practice
it's probably sufficient to call:my $subobj = $obj->subclass->new();
Anyway, could you please elaborate a bit regarding the actual usage scenario? It seems that this factory method stiffs the base class, putting a requirement for the base class to "know" about the derived class and making it difficult to use proper inheritance. This would lead to something like:my $subobj = $obj->subclass(); # or, better, my $subobj = $obj->subclass_clone();
which leads us to:my $subobj = $obj->clone_into('Class::SubClass'); # ... sub clone_into { my ($self, $subclass) = @_; return $subclass->new(%$self); }
or, probably more cleanly, to:my $subobj = Class::SubClass->new(%$obj);
my $subobj = Class::SubClass->clone_from($obj); # ... sub clone_from { my ($package, $base) = @_; return $package->new(%$base); }
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sub classing best practice
by cLive ;-) (Prior) on Dec 11, 2006 at 15:57 UTC | |
by jbert (Priest) on Dec 11, 2006 at 16:12 UTC | |
by Joost (Canon) on Dec 11, 2006 at 16:10 UTC | |
by polettix (Vicar) on Dec 11, 2006 at 16:20 UTC |