Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

schwern (1528)

  (email not shown publicly)
AOL IM: MichaelSchwern (Add Buddy, Send Message)

Schwern can destroy CPAN at his whim.

Journal of schwern (1528)

Wednesday June 06, 2007
08:22 AM

Stupid Perl Trick: Fully Qualified Method Calls Inherit

[ #33446 ]

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.


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();

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • It's a slick trick, and useful.
    • Randal L. Schwartz
    • Stonehenge
  • 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.

          return $this;