I have a package in perl , which uses two other package as its 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 the above 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:
From the output, it is clear that child object is passed to parent2's foo method and from that foo method, It is making a call to baz method. It checks for baz method in Child package first because, I am calling it with child object. Since, baz method is not in child package, It checks for the method in the base class. Parent1 is the first base class for Child. So, it finds the method in Parent1 and calls baz method of Parent1.
My question:
Is it possible to call the baz method of Parent2 without changing the order of base class in child ?
My Expected output:
Foo from Parent 2
Baz from Parent 2
The above example is just a analogy of my actual problem. I don't have access to modify the Base classes. I have only access to modify the Child class. So, Is it possible to change child class in such a way that it picks up both the methods from Parent2 without changing the order of base classes ?
Thanks!