Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

how to get the parent class method of the parent class itself

by ghosh123 (Monk)
on May 21, 2014 at 13:39 UTC ( #1086969=perlquestion: print w/replies, xml ) Need Help??

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

Hi
I have a Derived2 class which has Derived as its base and Derived has Base as its base. I want to call a subroutine of Base.pm by the object of Derived2.pm
Please let me know what is going wrong in my code below :

## package Base.pm ### package Base; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub display { my $self = shift; print "This is Base display \n"; } 1; ## package Derived.pm ## package Derived; @ISA = qw (Base); use Base; sub new { my $class = shift; my $self = {}; print "this is Derived new \n"; bless $self, $class; return $self; } sub display { my $self = shift; print "This is Derived display \n"; } 1 ## package Derived2.pm ### package Derived2; use Derived; @ISA = qw(Derived); use mro; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub display { my $self = shift; print "This is Derived2 display \n"; } 1 ## Script.pl use strict; use warnings; use mro ; use Derived2; my $obj = Derived2->new(); $obj->display(); #working as expected my $gp = $obj->mro::get_linear_isa->[1]; my $gpm = $gp->can('display'); print "gp $gp |@$gp \n"; print "gp $gp |gpm $gpm \n"; $gp->display(); #does not work

Replies are listed 'Best First'.
Re: how to get the parent class method of the parent class itself
by choroba (Archbishop) on May 21, 2014 at 13:55 UTC
    Several problems:
    1. The code misses indentation. It made it hard for me to help you.
    2. display is not dislpay. Please, post the code that actually runs and shows the problem.
    3. Have you read mro? You are using get_linear_isa differently from its documentation:
      my $gp = mro::get_linear_isa(ref $obj)->[1];

      This puts a string to $gp, so you can't dereference it as an array later.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: how to get the parent class method of the parent class itself
by scorpio17 (Canon) on May 21, 2014 at 14:27 UTC
Re: how to get the parent class method of the parent class itself
by tobyink (Canon) on May 21, 2014 at 20:59 UTC

    Is this something like what you want?

    use 5.010; use strict; use warnings; use Test::More; { package Level1; sub new { bless {}, shift }; sub display { "Base" }; } { package Level2; use base qw(Level1); # just inherit from Level1 } { package Level3; use base qw(Level2); sub display { "Overridden" }; } { package Level4; use base qw(Level3); sub display { shift->Level2::display(@_) }; } is( Level4->new->display, "Base" ); done_testing;
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: how to get the parent class method of the parent class itself
by leslie (Pilgrim) on May 22, 2014 at 11:42 UTC

    You can use SUPER keyword to call your parent class members. Find below code for your reference

    use strict; use warnings; { package Level1; sub new { bless {}, shift }; sub display { print "Base\n" } } { package Level2; use base qw(Level1); sub display { my($class) = @_; $class->SUPER::display(); } } { package Level3; use base qw(Level2); sub display { my($class) = @_; $class->SUPER::display(); } } { package Level4; use base qw(Level3); sub display { my($class) = @_; $class->SUPER::display(); } } my $obj = Level4->new(); $obj->display();

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-05-29 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?