13.10. Accessing Overridden MethodsProblemYour constructor method overrides the constructor of a parent class. You want your constructor to call the parent class's constructor. SolutionLearn about the special class, SUPER.
sub meth {
my $self = shift;
$self->SUPER::
DiscussionIn languages like C++ where constructors don't actually allocate memory but just initialize the object, all base class constructors are automatically called for you. In languages like Java and Perl, you have to call them yourself.
To call a method in a particular class, the notation $self-> Simple users of the class should probably limit themselves to the first one. The second is possible, but not suggested. The last must only be called from within the overridden method. An overriding constructor should call its SUPER's constructor to allocate and bless the object, limiting itself to instantiating any data fields needed. It makes sense here to separate the object allocation code from the object initialization code. We'll name it with a leading underscore, a convention indicating a nominally private method. Think of it as a "Do Not Disturb" sign. sub new { my $classname = shift; # What class are we constructing? my $self = $classname->SUPER::new(@_); $self->_init(@_); return $self; # And give it back } sub _init { my $self = shift; $self->{START} = time(); # init data fields $self->{AGE} = 0; $self->{EXTRA} = { @_ }; # anything extra }
Both $obj = Widget->new( haircolor => red, freckles => 121 ); Whether you store these user parameters in their own extra hash or not is up to you.
Note that SUPER only works on the first overridden method. If your my $self = bless {}, $class; for my $class (@ISA) { my $meth = $class . "::_init"; $self->$meth(@_) if $class->can("_init"); }
This fragile code assumes that all superclasses initialize their objects with See AlsoThe discussion on the SUPER class in perltoot (1) and perlobj (1), and in the section on "Method Invocation" in Chapter 5 of Programming Perl Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|