http://www.perlmonks.org?node_id=416884


in reply to Re: goto superclass method
in thread goto superclass method

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

Replies are listed 'Best First'.
Re^3: goto superclass method
by Ovid (Cardinal) on Dec 22, 2004 at 21:21 UTC

    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.

        I guess in this case it would be preferable to call call can as a method and let the code die and then worry about what to do if it fails. That's probably better than my silently failing or doing the wrong thing because of an overridden can.

        Cheers,
        Ovid

        New address of my CGI Course.