On Feb 1, 2004, at 1:10 AM, R. Joseph Newton wrote:

To answer your immediate
question, there is a way to call functions of the parent, but with another
step in indeirection:
$child_class->SUPER->new.


You should rarely need this though. Any attributes [ie hash members] of the
parent class that are relevant in your child object can simply be addressed
thorugh the constructor of the child class.

and


Well, that worked, but note that the following constructor for the MyChild
object worked just as well:



sub new { my $class = shift; my $name = shift; my $age = shift; my $self = {}; bless $self, $class; $self->{name} = $name; $self->{age} = $age; return $self; }

and the name and age attributes set through the child constructor were just
as available to the bemoan_age accessor provided by the base class. I'm not
sure I see any benefit to calling SUPER:: methods except in cases where a
parent method is overshadowed but still needed.

I think SUPER is a little more important than you give it credit for. I would say it's very common to use in a child's constructor.


I find your suggestion of duplicating the parent's construction scary OO thinking and I hope we're assuming you wrote both objects, at the very least. Still, one day you're going to change one and forget the other... Aren't we always telling people not to "reinvent the wheel" and to "use modules"? It's really the same issue, I think. I would much rather see people using SUPER than rewriting code.

SUPER is also quite handy when you're overriding a method, but just want to extend its functionality. Make the call then do the rest. Simple, flexible, powerful.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to