Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Question on use of Super with OO programing

by perlknight (Pilgrim)
on Jan 19, 2003 at 17:03 UTC ( [id://228184]=perlquestion: print w/replies, xml ) Need Help??

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

Brothers, I've come across the following code snipet from Programing Perl 3rd Edition. "SUPER" is used to overides methods in the base class.
package Mule; our @ISA = qw(Horse Donkey); sub kick { my $self = shift; print "The mule kicks!\n"; $self->SUPER::kick(@_); }
and
package Bird; use Dragonfly; { package Dragonfly; sub divebomb { shift->SUPER::divebomb(@_) }$self->Donkey::speak(@_ +);" and "shift->SUPER::divebomb(@_);". }
But I need clearification on is "$self->Donkey::speak(@_)" and "shift->SUPER::divebomb(@_)". What is the diff btw "$self" and "shift" when used in this context? Thanks.

Replies are listed 'Best First'.
Re: Question on use of Super with OO programing
by castaway (Parson) on Jan 19, 2003 at 17:32 UTC
    The second example is actually this:
    package Bird; use Dragonfly; { package Dragonfly; sub divebomb { shift->SUPER::divebomb(@_)} }
    If you look at your first example, you see that $self is set to the result of the shift function. shift returns the first value in an array (and removes it from the array), when it has no arguments it returns the first value from @_, which is also where you find the arguments of a sub.
    Doing 'shift->SUPER::divebomb(@_)' does exactly the same thing, it's just a shortcut. (It calls shift, and then calls SUPER on the returned value of shift, missing out the step of assigning it to another variable.)

    C.
    PS: Your examples are a bit mixed up, $self->Donkey::speak(@_) is part of a third example in the middle of that page:

    sub speak { my $self = shift; print "The mule speaks!\n"; $self->Donkey::speak(@_); }
Re: Question on use of Super with OO programing
by dws (Chancellor) on Jan 19, 2003 at 17:41 UTC
    What is the diff btw "$self" and "shift" when used in this context?

    If you look at the first snippet again, you'll see that it passes along its arguments to the inherited kick() method via

    $self->SUPER::kick(@_);
    Since an object reference is always the first parameter passed when a method is invoked via '->', unless you the object reference from the argument list, you'll get two of them if you pass @_ to another object method.

    The first fragment could have been written to do

    sub kick { shift->SUPER::kick(@_); }
    though this means that the object reference isn't available later in the method. Since there is no later in either case, it's not a problem. But if you needed to do more after invoking the inherited kick() behavior, you'd be out of luck.

    Explicitly removing the object reference from the argument list and storing it into $self is also stylistic. It helps the reader quickly realize that they are looking at an object method.

      Since an object reference is always the first parameter passed when a method is invoked via '->', unless you the object reference from the argument list, you'll get two of them if you pass @_ to another object method.

      I was just contemplating this recently, feeling a little irked that @_ has to be babysat in such a manner. Do you suppose it would be feasible for the magic of @_ to be expanded so that if passed unmodified to another method the first element be replaced with the new head of the current argument stack?

      I suppose it would be necessary to look at whether the subroutine was invoked with Pkg::func or like Pkg->method/$obj->method, and ensure that some reference had been taken to that first element in the enclosing scope even if of the form my ($obj) = @_, but surely this must be possible.

      Edit by ar0n -- Runaway <i>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-16 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found