Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Error in Inheritance Class

by Ojosh!ro (Beadle)
on Mar 12, 2007 at 11:02 UTC ( [id://604317]=note: print w/replies, xml ) Need Help??


in reply to Error in Inheritance Class


It's not entirely clear to me what you're trying to do, but the errormessage you get is correct. There is no method "useChild1" in the Parant class.

Is what you try to do:
PARENT CLASS
(Parent.pm)

package Parent; use strict; use warnings; use Child; sub new { my $class = shift; my $this = {}; bless $this, $class; $this->setChild( Child->new( @_ ) ); return $this; } sub setChild { my ( $this, $child ) = @_; if ( defined $child ) { $this->{_child} = $child; return 1; } return undef; } sub getChild { my $this = shift; return $this->{_child}; } sub setChildValue1 { my ( $this, $value ) = @_; if ( defined $value ) { return $this->getChild()->setValue1( $value ); } return undef; } sub getChildValue1 { my $this = shift; return $this->getChild()->getValue1(); } sub setChildValue2 { my ( $this, $value ) = @_; if ( defined $value ) { return $this->getChild()->setValue2( $value ); } return undef; } sub getChildValue2 { my $this = shift; return $this->getChild()->getValue2(); } 1;

CHILD CLASS
(Child.pm)
package Child; use strict; use warnings; sub new { my ( $class, $value1, $value2 ) = @_; my $this = {}; bless $this, $class; $this->setValue1( $value1 ); $this->setValue2( $value2 ); return $this; } sub setValue1 { my ( $this, $value ) = @_; if ( defined $value ) { $this->{_value1} = $value; return 1; } return undef; } sub getValue1 { my $this = shift; return $this->{_value1}; } sub setValue2 { my ( $this, $value ) = @_; if ( defined $value ) { $this->{_value2} = $value; return 1; } return undef; } sub getValue2 { my $this = shift; return $this->{_value2}; } 1;

MAIN
(main.pl)
use Parent; use strict; use warnings; my $parent = new Parent( 'value1', 'value2' ); my $child = $parent->getChild(); print "Values 1 and two from the child are:\n1....", $child->getValue1(),"\n2....", $child->getValue2(),"\n"; print "Values 1 and two from the child via the parent are:\n1....", $parent->getChildValue1(),"\n2....", $parent->getChildValue2(),"\n";
In other words, access properties of the child-object via the parent?
Mind you, nothing is inheriting anything from anyone. Not in my example, nor in yours.

if( exists $aeons{strange} ){ die $death unless ( $death%2 ) }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2026-03-06 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.