Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

inheritance weirdness

by dash2 (Hermit)
on Jan 25, 2003 at 13:10 UTC ( [id://229835]=perlquestion: print w/replies, xml ) Need Help??

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

#perl package Foo; sub foo{print "got here";print $x++;print "\n";my $s=shif +t;die if $x>10;$s->SUPER::foo;} package Bar;@ISA=qw/Foo/; package Baz;@ISA=qw/Bar/; package main; my $f=bless {},Baz;$f->foo; # now hit Ctrl+D ... got here0 Can't locate object method "foo" via package "Foo" (perhaps you forgot + to load "Foo"?) at - line 2. #

Is this a bug? Why can't Foo::foo() call itself?

dave hj~

Replies are listed 'Best First'.
Re: inheritance weirdness
by ihb (Deacon) on Jan 25, 2003 at 13:29 UTC

    &Foo::foo can call itself. But you're not trying to call &Foo::foo. You're trying to call SUPER::foo, which will look for a &foo in any super class to Foo (when called from package Foo). I've often wanted this error message to be Can't locate object method "SUPER::foo" via package "Foo", just so you don't get all confused by this error message.

    Hope I've helped,
    ihb
Re: inheritance weirdness
by boo_radley (Parson) on Jan 25, 2003 at 13:40 UTC
    What is Foo a subclass of? Nothing, so the call to $s->SUPER::foo; is a call to die.
    You might have your package inherit from UNIVERSAL, so you can take advantage of :
    if ($s->SUPER::can("foo")) {$s->SUPER::foo ()}
    This is probably because @ISA returns Foo::foo as a method when invoked on Baz (Baz, of course, not having its own foo); even though you expect it to be Baz::foo and thus have something interesting in its SUPER pseudoclass -- probably calling Bar::foo, and then finally Foo::foo. Of course, at this point, your script would still bomb out, because it'd still try to find a superclass for Foo.
Re: inheritance weirdness
by dash2 (Hermit) on Jan 25, 2003 at 13:26 UTC
    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~

      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~

      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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://229835]
Approved by Corion
Front-paged by Trimbach
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found