in reply to How do I call an overridden method?
in thread Answer: How do I call an overridden method?
Perl has a very nice and easy way of doing this: use the SUPER pseudo-class, which tells Perl to look for the method that you're calling in any of the superclasses.
So you define your superclass--for example, we'll define the following Person class:
And then you have a class Male that inherits from Person:package Person; sub new { my $type = shift; my $class = ref $type || $type; my $self = { @_ }; bless $self, $class; $self; } sub name { shift->{NAME} }
Simple! All we had to do was call the SUPER constructor to get back a new object: the new object is blessed into our "Male" class, but it contains all of the initialized data fields from our Person class, as well.package Male; @Male::ISA = qw/Person/; sub new { my $type = shift; my $class = ref $type || $type; my $self = $class->SUPER::new(@_); $self->{GENDER} = "male"; $self; } sub gender { shift->{GENDER} }
Here we have a little test program to test it out:
and we getpackage main; my $him = new Male(NAME => "Foo Bar"); print "\$him is in the class '", ref $him, "'\n"; print $him->name, " is of the gender ", $him->gender, "\n";
Which is what we expected.$him is in the class 'Male' Foo Bar is of the gender male
For more information, take a look at perlbot and perlboot.
|
|---|