Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Parent cannot call Child method

by Eily (Monsignor)
on Mar 17, 2016 at 12:42 UTC ( [id://1158077]=note: print w/replies, xml ) Need Help??


in reply to Parent cannot call Child method

It actually works the other way around. An object of the class MyParent::MyChild will be able to call methods defined in the parent, if the child does not have a method of the same name itself. So you want to try my $obj = MyParent::MyChild->new(); $obj->parent_nifty_method1();

use parent qw(Parent); already sets the @ISA of the child (and it pushes the parent in the @ISA instead of replacing it, so that a class may inherit from more than one), so you don't have to do it afterwards.

Replies are listed 'Best First'.
Re^2: Parent cannot call Child method
by TorontoJim (Beadle) on Mar 17, 2016 at 13:19 UTC
    Thank you.

    I tried it that way and got this error:

    Can't locate object method "new" via package "MyParent::MyChild" (perh +aps you forgot to load "MyParent::MyChild"?) at

    This is how I changed the original call:

    use MyParent::MyChild; my $obj = MyParent::MyChild->new(); $obj->parent_nifty_method1;

    I tried use MyParent in addition to use MyParent::MyChild, neither worked.

      Did you, as Eily suggested, use parent qw(Parent); in MyParent::MyChild?
        Yes, this is the start of MyChild after reading Eily's post:
        package MyParent::MyChild; use strict; no strict "refs"; use Carp qw(croak); use parent qw(MyParent); my $VERSION = .01; sub new { my ($class) = @_; my $self = {}; bless $self, $class; return $self; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found