tags:

views:

376

answers:

4

I noticed that when you call a superclass's methods, you need to do something like this :

my $self = $class->SUPER::new();

Why isn't that:

my $self = $class->SUPER->new();
+10  A: 

I suspect because $class->SUPER->new() would normally be the same as $class->SUPER()->new(). But there isn't a $class->SUPER() function, and its not clear what that would return.

On the other hand, $class->Foo::Bar has always been a valid way to call a method directly by full name, so making a special package-like thing — SUPER — fits in better. (I suspect that you could actually implement SUPER as a package, and maybe it historically was, I don't know)

PS: Take a look at the mro package, and $self->next::method. Also, take a look at Moose if you're going to do serious OO work in Perl.

derobert
I want to understand the stuff behind before I will use Moose.
Geo
Check out _A Gentle Introduction to Moose_ http://www.catalyzed.org/2009/06/a-gentle-introduction-to-moose.html
Drew Stephens
@Geo, You only *think* you want to know more. I can honestly tell you that you are better off not knowing that, until you come across it.
Brad Gilbert
+6  A: 

To add to what derobert said:

You're calling 'new' in the 'SUPER' namespace but passing it the object (or string), '$class'.

You don't have to use SUPER, as you can give the full package name of the parent (useful in cases of diamond inheritance):

sub init {
   my $self = shift;
   $self->ParentClass1::init();
   $self->ParentClass2::init();
}
Joe
Other trivia: You can actually do `$self->TotallyUnrelatedClass::init()`, though its probably best not to!
derobert
+5  A: 

Method calls have a number of forms:

Calls method, possibly inherited:

->method()

Explicitly calls sub Package::method, whether that's in the inheritance tree or not:

->Package::method()

Explicitly calls the referred-to sub, whether that's in the inheritance tree or not:

->$coderef()

Calls method the method that would have been called by __PACKAGE__->method() if there were no sub method in __PACKAGE__ (N.B. the class or object on the left of -> is irrelevant):

->SUPER::method()

Any of the above, depending on the contents of $method:

->$method()

While the first form is the most common, it's worth learning about the others and how they work.

ysth
+5  A: 

In short, SUPER isn't a method. It's a virtual package. It's documented in perlobj under the "Method Invocation" section.

Note, however, that SUPER bases itself on the current package, not the package of the instance you used it with.

brian d foy