I have a package in perl that uses two other packages as a base.
Parent1:
package Parent1; use strict; use warnings; sub foo { my $self = shift; print ("\n Foo from Parent 1 "); $self->baz(); } sub baz { my $self = shift; print ("\n Baz from Parent 1 "); } 1;
Parent 2:
package Parent2; use strict; use warnings; sub foo { my $self = shift; print ("\n Foo from Parent 2 "); $self->baz(); } sub baz { my $self = shift; print ("\n Baz from Parent 2 "); } 1;
Child: This uses two parent packages.
package Child; use strict; use warnings; use base qw(Parent1); use base qw(Parent2); sub new { my $class = shift; my $object = {}; bless $object,$class; return $object; } 1;
Main:
use strict; use warnings; use Child; my $childObj = new Child; $childObj->Parent2::foo();
Output:
Foo from Parent 2 Baz from Parent 1
My analysis:
It can be seen from the output that the child object is passed to the parent2 method foo and from this method foo. He makes a call to the baz method. First he checks the baz method in the Child package, because I call it with the child. Since the baz method is not in the child package, it checks the method in the base class. Parent1 is the first base class for Child. So he finds the method in Parent1 and calls the baz method for Parent1.
My question is:
Is it possible to call the baz method from Parent2 without changing the order of the base class in the child?
My expected result:
Foo from Parent 2 Baz from Parent 2
The above example is just an analogy of my real problem. I do not have access to change the base classes. I have only access to change the Child class. So, is it possible to change the child class so that it retrieves both methods from Parent2 without changing the order of the base classes?
Thanks!
inheritance subroutine perl package
jayaganthan
source share