editi has asked for the wisdom of the Perl Monks concerning the following question:

I have a program that using the two modules named as ParantClass and Child1Class. i am sending the values from perl program to Child1Class module.ParantClass Module gets the value from Child1Class module and return back the output.
But i couldnt get the answer
The Error is
root@local class# perl UseInheritClass.pl Now I am in Child Module Send a values to Parant Class from Child1 I Entered in to the Parant Class Method ::: UseParant :::500 and are Going to Chid Class So Take Care of Data Get Value from Parant Module ::: ::: Can't locate object method "useChild1" via package "ParantClass" at UseInheritClass.pl line 4.
the following program i wrote for run this file
File1 ::: ParantClass.pm
#!/usr/bin/perl package ParantClass; sub new { my $self = {}; $self->{Parant_Var1}=undef; $self->{Parant_Var2}=undef; bless($self); return $self; } sub UseParant { my $self = shift; if(@_) { $self->{Parant_Var1}=shift; $self->{Parant_Var2}=shift; my $SentToChild1=$self->{Parant_Var1}; my $SendToChild2=$self->{Parant_Var2}; $SendToChild1=$sendToChild1+500; $SendToChild2=$sendToChild2+"1"; print "I Entered in to the Parant Class Method ::: Use +Parant :::$SentToChild1 and $SentToChild2 are Going to Chid Class So + Take Care of Data\n\n"; push(@GoesChild,$sendToChild1); push(@GoesChild,$sendToChild2); } return @GoesChild1; } return 1;

The Second Program id :::: Child1Class.pm
#!/usr/bin/perl package Child1Class; use ParantClass; our ($GetValue1,$GetValue2); sub new { my $self=ParantClass->new(); print "Now I am in Child Module\n\n"; print "Send a values to Parant Class from Child1 "; ($GetValue1,$GetValue2)=$self->UseParant(500,"Child"); print "Get Value from Parant Module ::: $GetValue1 ::: $GetVal +ue2\n\n"; return $self; } sub UseChild1 { my ($value1,$value2)=ParantClass->UseParant(1000,"From Child") +; print "Parant Class Values=$value1 ----- $value2 \n\n"; #my $self = shift; print "This Values go to perl program and display the Values : +:::\n\n\n"; } return 1;

The Third Program is ::: UseInheritClass.pl
#!/usr/bin/perl use Child1Class; $object = Child1Class->new(); print $object->useChild1();
help me to rectify this error. thank you

Replies are listed 'Best First'.
Re: Error in Inheritance Class
by Joost (Canon) on Mar 12, 2007 at 10:33 UTC
    This line in the constructor of Child1Class:
    my $self=ParantClass->new();
    creates an object of "ParantClass". In other words, you're only ever dealing with ParantClass objects, while you think you're dealing with Child1Class objects.

    You should ALWAYS use the 2 argument form of bless() and use the first argument to the constructor (i.e. the expected classname) as the second argument:

    # in the parent class: sub new { my ($class) = @_; my $self = bless { },$class; # ... return $self; } # in the child class: # if you really need to override the constructor # you can use this: sub new { my ($class) = @_; my $self = $class->SUPER::new(); # call parent constructor # .... return $self; }
Re: Error in Inheritance Class
by Ojosh!ro (Beadle) on Mar 12, 2007 at 11:02 UTC

    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 ) }
Re: Error in Inheritance Class
by agianni (Hermit) on Mar 12, 2007 at 17:29 UTC

    Generally, if you want one class to inherit from another, you should use base. That will significantly simplify setting up the relationship. Putting this:

    use base qw( Parent );
    in the child class in Joost's example (assuming the parent class is called 'Parent') will take care of that relationship for you.

    However, if you need to manage the relationship between a parent and it's children, that's a much more complicated matter. Since your example code isn't doing anything but showing how you are setting up the relationship, it's hard to give you further advice.