Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: purpose of Super keyword

by pajout (Curate)
on Feb 10, 2010 at 22:20 UTC ( #822537=note: print w/ replies, xml ) Need Help??

in reply to purpose of Super keyword

Consider following example:

#!/usr/bin/perl use Data::Dumper; use Animal::Dog; my $rex = Animal::Dog->new(); print Dumper($rex);
package Animal; use strict; use warnings; sub new { my ($class) = @_; my $proto = {need_oxygen => 1}; return bless($proto, $class); } 1;

package Animal::Dog; use base "Animal"; use strict; use warnings; sub new { my ($class) = @_; my $self = $class->SUPER::new(); $$self{domesticated} = 1; return $self; } 1;

Running will return:
$VAR1 = bless( { 'domesticated' => 1, 'need_oxygen' => 1 }, 'Animal::Dog' );

When parent method (Animal::new in the example) does something important (it sets 'need_oxygen' for all animals), you can call it WITHOUT explicit hardcoding of the parent class, as was already noticed.

For instance, in Animal::Dog::Husky->some_method body, you can call some inherited method and you do not care if that method is written in Animal or in Animal::Dog class.

UPDATE: s/inherited/overiden/

Comment on Re: purpose of Super keyword
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://822537]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-12-01 08:00 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (796 votes), past polls