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

In reply to Re: Parent cannot call Child method by perlfan
in thread Parent cannot call Child method by TorontoJim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.