my $lib = test1->new();
($self->{lib}) = ($lib->print_name());
That doesn't cause any error, but it doesn't do what you want it to do. It leaves $self->{lib} == 1, which is what print returns at the end of test1::print_name.
Later, in test2::get_name, you try to use $self->{lib} as an object instance, but it's still just the number 1.
I'd fix this by changing the code in test2::new to:
my $lib = test1->new();
$self->{lib} = $lib;
$lib->print_name();
You could also eliminate a variable this way:
$self->{lib} = test1->new();
$self->{lib}->print_name();
You could also "fix" this by having test1::print_name return $self instead of the value of print
sub print_name {
my $self = shift;
print "I forgot what went here";
return $self;
}
I don't like that solution personally since the return value isn't really related to the method, but having methods return $self whenever possible is common enough. |