Stupid Perl Trick: Fully Qualified Method Calls Inherit

schwern on 2007-06-06T13:22:00

Let's say you have YourClass which inherits from Parent. It inherits a method called foo(). YourClass overrides foo(). For some reason (and we'll leave that open as to why) you want to skip calling YourClass::foo() and jump directly to Parent->foo(). One way would be to call Parent::foo() directly as a function simulating the method call.

Parent::foo($your_object, @args);

Trouble there is what if Parent::foo() doesn't actually exist? What if Parent inherits foo()? Function calls don't honor inheritance. Even if it happens to work now it might not work later, you've violated encapsulation.

It turns out you can make fully qualified method calls.

$your_object->Parent::foo(@args);

Furthermore, this will inherit starting from Parent so it will DWIM no matter if Parent defines foo() or its inherited.

Yes, this is documented deep in perlobj. Look for "start looking".

package GrandParent; sub foo { 23 }

package Parent; @ISA = qw(GrandParent);

package YourClass; @ISA = qw(Parent); sub foo { 42 }

print YourClass->Parent::foo();


I also show this in perlboot

merlyn on 2007-06-06T15:54:01

It's a slick trick, and useful.

NEXT and EVERY

pjf on 2007-06-07T02:58:08

For what it's worth, this sort of directed dispatch has applications when working with pseudo-classes such as NEXT and EVERY . As a practical example when we're doing object construction:

use NEXT;

sub new {
      my $class = shift;
      my $this = bless({},$class);

      # Call all my parental initalizers,
      # in the correct order, in one step.
      $this->EVERY::LAST::_init(@_);

      return $this;
}