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
In reply to Re: sub classing best practice
by polettix
in thread sub classing best practice
by cLive ;-)
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |