Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Parent cannot call Child method

by perlfan (Priest)
on Mar 17, 2016 at 15:09 UTC ( #1158101=note: print w/replies, xml ) Need Help??


in reply to Parent cannot call Child method

You need to use SUPER. See perlobj. However, You're doing it wrong by putting a reference to parent in the child object. The whole point of inheritance is to make the parent's methods available to the child. See the second example.

Example 1: Your current code fixed up (as a monolithic file):

ackage MyParent; use strict; use Carp qw(carp croak); our $VERSION = .01; sub new { my $class = shift; my $self = bless { something => "foo", }, $class; return $self; } sub parent_nifty_method1 { my $self = shift; print $self->{something}; } sub parent_nifty_method2 { my $self = shift; #... pah ... } package MyParent::MyChild; use strict; use base qw(MyParent); our $VERSION = .01; sub new { my $class = shift; my $parent = $class->SUPER::new; my $self = bless { PARENT => $parent, }, $class; return $self; } sub child_method_a { my $self = shift; #read parent variable, not change it print $self->{PARENT}->{something}; } sub child_method_b { my $self = shift; #... incredibly cool stuff ... } package main; #use MyParent; #use MyParent::MyChild; #use strict; my $parentobject = MyParent->new(); $parentobject->parent_nifty_method1; #prints: foo #Now I want to access something or other in the MyChild, doing it this + way (which is what inheritance means to me) my $childobject = MyParent::MyChild->new; $childobject->child_method_a; #Prints: foo 1;

Example 2 - redefined MyParent::MyChild that actually inherits MyParent and builds on it:

package MyParent::MyChild; use strict; use base qw(MyParent); our $VERSION = .01; sub new { my $class = shift; my $parent = $class->SUPER::new; # extend blessed reference $parent with a new field $parent->{another_something} = "bar"; my $self = bless $parent, $class; return $self; } sub child_method_a { my $self = shift; #read parent variable, not change it $self->parent_nifty_method1; } sub child_method_b { my $self = shift; print $self->{another_something}; } package main; #use MyParent; #use MyParent::MyChild; #use strict; my $parentobject = MyParent->new(); $parentobject->parent_nifty_method1; #prints: foo #Now I want to access something or other in the MyChild, doing it this + way (which is what inheritance means to me) my $childobject = MyParent::MyChild->new; $childobject->child_method_a; #Prints: foo $childobject->parent_nifty_method1; #Prints: foo $childobject->child_method_b; #Prints: bar

Replies are listed 'Best First'.
Re^2: Parent cannot call Child method
by TorontoJim (Sexton) on Mar 17, 2016 at 18:55 UTC
    As it turns out, it was not a problem with how I was "creating" the modules and the parent/child relationship. The problem was my understanding of inheritance.

    After some more reading, I went back and rewrote them. I had originally written one big module and tried to then make smaller children from it.

    As my ex would attest, I have a problem making children.

    Now that I have changed my expectation and no longer expect this parent/child structure to work like one big module, everything started falling into place.

    Thank you all for your help in getting me here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2020-06-06 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (41 votes). Check out past polls.

    Notices?