I'm trying out multiple inheritance and would like to get the hang of it without using packages like Moose to sort out the issue behind the scenes.
I have two base classes, Left and Right, in a "broken" diamond:
Left Right
\ /
Multi
They both implement an overload for "". When calling a method, below named perform, in any of the base classes, those methods are supposed to use this overload to print a representation of that part of the object. Multi implements perform as so:
sub perform {
my $self = shift;
$self->Left::perform;
$self->Right::perform;
}
What happens is that both base classes perform methods are called as they are supposed to, but when those methods call any other methods (like the "" overload) it'll always be the one in Left. However, if an instance of Right is created separately (not as a part of Multi) it'll call the correct method.
- I wonder how to make a method in this scenario select its own package's methods over its left-most sibling base class' methods?
Here's what I've tried (in perl v5.26.1 and v5.32.1):
#!/usr/bin/perl
use strict;
use warnings;
package Left; #----------------------------------------------------------------
sub new {
my ($class, @args) = @_;
my $self = bless {}, $class;
return $self->_init(@args);
}
sub _init {
my $self = shift;
$self->{_leftval} = shift;
return $self;
}
sub value { shift->{_leftval}; }
use overload '""' => sub {
my $self = shift;
'Left(' . $self->value . ')';
};
sub perform {
my $self = shift;
print '# LEFT ' . $self . "\n";
}
package Right; #---------------------------------------------------------------
sub new {
my ($class, @args) = @_;
my $self = bless {}, $class;
return $self->_init(@args);
}
sub _init {
my $self = shift;
$self->{_rightval} = shift;
return $self;
}
sub value { shift->{_rightval}; }
use overload '""' => sub {
my $self = shift;
'Right(' . $self->value . ')';
};
sub perform {
my $self = shift;
print '# RIGHT ' . $self . "\n";
}
package Multi; #---------------------------------------------------------------
use parent -norequire, 'Left', 'Right' ;
sub new {
my ($class, @args) = @_;
my $self = bless {}, $class;
return $self->_init(@args);
}
sub _init {
my $self = shift;
$self->Left::_init(shift);
$self->Right::_init(shift);
return $self;
}
sub perform {
my $self = shift;
$self->Left::perform;
$self->Right::perform;
}
package main; #----------------------------------------------------------------
my $l = Left->new("a Left");
my $r = Right->new("a Right");
my $m = Multi->new("lEfT", "rIgHt");
$l->perform;
$r->perform;
print "---- and now a Multi ----\n";
$m->perform;
Expected output:
# LEFT Left(a Left)
# RIGHT Right(a Right)
---- and now a Multi ----
# LEFT Left(lEfT)
# RIGHT Right(rIgHt)
Actual output (note the last line):
# LEFT Left(a Left)
# RIGHT Right(a Right)
---- and now a Multi ----
# LEFT Left(lEfT)
# RIGHT Left(lEfT)