Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: goto superclass method

by Ovid (Cardinal)
on Dec 22, 2004 at 18:01 UTC ( #416846=note: print w/replies, xml ) Need Help??


in reply to goto superclass method

Well, I don't know if this is necessarily the best way to go about it, but then, I don't know what problem you're trying to solve. It sounds like a design issue rather than a syntax issue (i.e., are you sure you don't have a design flaw?) In any event, here's a rough way to do it:

sub dostuff { # you'll want to walk your inheritance tree rather than hardcode thi +s foreach my $super (search_isa()) {# <-- you implement :) if (my $method = UNIVERSAL::can($super, 'dostuff')) { goto $method; } } }

I feel dirty just telling you this :)

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re^2: goto superclass method
by ikegami (Pope) on Dec 22, 2004 at 18:11 UTC

    search_isa() is simply @ISA, because can searches the whole inheritance tree of the supplied class.

    use strict; use warnings; package AA; sub dostuff { print(scalar(caller()), $/); } package BB; package CC; BEGIN { our @ISA = 'AA'; } package DD; use vars qw(@ISA); BEGIN { @ISA = qw(BB CC); } sub dostuff { foreach my $super (@ISA) { #if (my $method = $super->can('dostuff')) { # -or- if (my $method = UNIVERSAL::can($super, 'dostuff')) { #print($super, $/); #print($method, $/); goto $method; } } } package main; #print(\&AA::dostuff, $/); bless({},'DD')->dostuff(); # Prints "main"

      search_isa() is simply @ISA, because can searches the whole inheritance tree of the supplied class.

      D'oh! I knew that. Silly me :)

      Cheers,
      Ovid

      New address of my CGI Course.

Re^2: goto superclass method
by ikegami (Pope) on Dec 22, 2004 at 19:21 UTC

    Here's an alternative that doesn't use a loop:

    You probably know that dostuff exists in a parent, you can simplify it down to:

    package MyModule; use vars qw(@ISA); BEGIN { @ISA = ...; *MyModule::Super::ISA = \@ISA; } sub dostuff { goto(MyModule::Super->can('dostuff')) if (...condition...); ... }

    Don't put any methods (or at least not 'dostuff') in the MyModule::Super package (namespace) for this to work.

Re^2: goto superclass method
by chromatic (Archbishop) on Dec 22, 2004 at 19:46 UTC

    Why go to so much work to try to make polymorphism work only to break it by calling a UNIVERSAL method explicitly?

      Well, calling can directly can break if you have something in @ISA that isn't a class (such as a coderef: Class::Dynamic). Out of habit I try to call UNIVERSAL::can unless the $object->can('can');

      Perhaps I'm being overly paranoid.

      Cheers,
      Ovid

      New address of my CGI Course.

        How do you know in general whether the object or class has its own can though? (Easy answer, but not one you'll like.)

        I prefer to use Scalar::Util's blessed to see if I can invoke operations on an object, though wrapping the call in an eval block is safer if you expect class names as well.

Re^2: goto superclass method
by avarus (Novice) on Dec 22, 2004 at 18:26 UTC
    Hi Ovid,

    Aha - You can feel less dirty as I promise not to actually implement your code for anything important!

    Considering your code example cleared up my misunderstanding regarding the $obj->SUPER syntax, and I have cleaned up my code.

    Thanks - TIM
    --
    #Tip: use 'no strict' to make those nasty errors vanish.
Re^2: goto superclass method
by ysth (Canon) on Dec 23, 2004 at 02:47 UTC
    I feel dirty just telling you this
    And so you should.

    This method of searching is flawed, since if perl does the work, it will scan the whole isa tree looking for a declared "dostuff", and then fall back on searching the whole tree for "AUTOLOAD". Your code will find an AUTOLOAD in a package with no declared "dostuff" sometimes when it shouldn't.

    This breaks superclasses that are using AUTOLOAD correctly, i.e. with a declaration of each function that may be autoloaded.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://416846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2019-09-18 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (238 votes). Check out past polls.

    Notices?