in reply to inheritance weirdness

OK, this node was rather useful. It seems that SUPER goes by package, not by class. How stupid.

I'll use Class::ISA instead.

dave hj~

Replies are listed 'Best First'.
SUPER does the right thing - don't disrespect it!
by merlyn (Sage) on Jan 25, 2003 at 16:59 UTC
    Perhaps you call it "stupid" because you don't understand what it's for.

    When you override a method in a subclass, you frequently need to modify or reuse the superclass behavior. So you need a construct that says "if I wasn't here, what would have been called".

    But let's say you were already an inherited method. To do the lookup, you can't start in the object's superclass... you have to start in the method's superclass, otherwise you'll get into an infinite loop, calling yourself over and over.

    Hence, the current behavior is precisely right, albeit a bit awkward if you don't get the purpose of the behavior. Also, the closest thing we have to "class of the method" is "package in which the method was compiled", which is not necessarily the same as "the package in which the subroutine is located", as I've illustrated in the past here. That's both a blessing and a curse, but I'll save the longer discussion for another node.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      It's a good point that you don't always want to call the object's superclass.

      However, what was frustrating was my inability to do it when I did want to do that. Sometimes you want to e.g. recurse upwards through an object's inheritance tree (for, say, finding out about class data). Anyway, I used Class::ISA, so it worked out OK in the end. I think I was a little grumpy that day. (SPOPS).

      dave hj~

Re^2: inheritance weirdness
by adrianh (Chancellor) on Jan 25, 2003 at 14:20 UTC

    Why is it stupid? That's exactly how I would expect it to behave.

    If you want to call the method recursively just do $s->foo.

    I'm not sure how Class::ISA can help - can you describe what you're trying to achieve in more detail?