Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please see the perldoc for oop http://perldoc.perl.org/perlobj.html As per the document: "It is important to note that SUPER refers to the superclass(es) of the current package and not to the superclass(es) of the object." Now, I am in a situation where I need the SUPER to refer to the superclass(es) of the object. So, looking a way to achieve it if possible.
  • Comment on Referring SUPER to superclass of object

Replies are listed 'Best First'.
Re: Referring SUPER to superclass of object
by tilly (Archbishop) on Jan 12, 2011 at 15:29 UTC
    The usual solution is to use NEXT. (Or to not use multiple inheritance.)

    Incidentally if you're doing interesting things with OO in Perl, you may wish to look at Moose.

Re: Referring SUPER to superclass of object
by ikegami (Patriarch) on Jan 12, 2011 at 18:27 UTC

    You want the "c3" method resolution order. There is builtin support for this activated using use mro qw( c3 );. The module also provides next::method and maybe::next::method as alternatives to SUPER::.

    use strict; use warnings; use feature qw( say ); { package ClsA; use mro 'c3'; sub foo { say __PACKAGE__; } } { package ClsB1; use mro 'c3'; our @ISA = qw( ClsA ); } { package ClsB2; use mro 'c3'; our @ISA = qw( ClsA ); sub foo { say __PACKAGE__; $_[0]->maybe::next::method() } } { package ClsB3; use mro 'c3'; our @ISA = qw( ClsA ); sub foo { say __PACKAGE__; $_[0]->maybe::next::method() } } { package ClsD; use mro 'c3'; our @ISA = qw( ClsB1 ClsB2 ClsB3 ); } ClsD->foo(); say "--"; ClsB2->foo();
    ClsB2 ClsB3 ClsA -- ClsB2 ClsA

    Update: Added code example and then improved it.

Re: Referring SUPER to superclass of object
by Anonymous Monk on Jan 12, 2011 at 15:23 UTC
    A comment on this blog post led me to the module SUPER, which might do what you're looking for.
Re: Referring SUPER to superclass of object
by MidLifeXis (Monsignor) on Jan 12, 2011 at 15:25 UTC

    In perlobj, if you search for SUPER, you should find some usage examples (all within about 15 lines).

    Update: Misreading of the OP on my part. Thanks for the clarification tilly.

    --MidLifeXis

      The original question makes it clear that the questioner has read that documentation and understood it, and it did not solve the questioner's problem. Your answer therefore provides no useful information.

      The problem the question has run across is the following. Suppose that class A inherits from B and C, and class B inherits from D. What the questioner needs to do is search for all of the ancestors of a method in the path (B, D, C). Unfortunately SUPER is only aware of the inheritance of the class, not the object, so if SUPER finds a method in B, then SUPER called from that method will miss D.

      This limitation is documented (somewhat tersely) in perlobj, and the original question quotes that limitation.

Re: Referring SUPER to superclass of object
by ELISHEVA (Prior) on Jan 12, 2011 at 16:32 UTC

    You may also want to take a look at the CPAN module Class::ISA which will return a list of all of the direct and indirect superclasses in the order Perl will use to search for a method definition.