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