in reply to Question on use of Super with OO programing

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.

Replies are listed 'Best First'.
Re: Re: Question on use of Super with OO programing
by djantzen (Priest) on Jan 19, 2003 at 18:34 UTC

    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>