Perl for Newbies - Part 3 - Modules and ObjectsObjects in PerlMaking Your Own ObjectsObject InheritanceCalling Overridden Methods of Base Classes

4.3.3.1. Calling Overridden Methods of Base Classes

It is possible to explicitly call the method of a base class even if it was overridden by another method in the derived class. To do that use the SUPER:: prefix before the method name.

Here is a rewrite of the Bar class, this time with assign_name retaining its name.

package Bar2;

use strict;
use warnings;

use vars qw(@ISA);

use Foo;

@ISA=qw(Foo);

sub assign_name
{
    my $self = shift;

    my $name = shift;

    # Call the method of the base class
    my $ret = $self->SUPER::assign_name($name);
    if (! $ret)
    {
        $self->{'num_times'}++;
    }

    return $ret;
}

sub get_num_times_assigned
{
    my $self = shift;

    return
        (exists($self->{'num_times'}) ?
            $self->{'num_times'} :
            0
        );
}

1;

Written by Shlomi Fish