I'm trying to create the abstract method pattern using Perl and Moose. What I don't understand is that if I override a method from the AbstractClass it will eventually be called anyway. Why is this and is there a way to avoid the superclass from being called?
Main
package main;
use AbstractSort;
use OrderedSort;
# Sub class test
my $ordered = OrderedSort->new(array => [1, -1, 23, 34123, -24324]);
$ordered->sortData();
AbstractClass
package AbstractSort;
use namespace::autoclean; # Trims EXPORTER
use Moose;
has 'array' => (traits => ['Array'],
is => 'ro',
isa => 'ArrayRef[Int]',
default => sub { [] },
handles => {
get_array => 'get',
count_array => 'count',
});
sub sortData{
my $self = shift;
print "Sorting data..\n";
_sortAlgorithm($self->array);
# ...
}
# Protected method here is the actual algorithm
sub _sortAlgorithm {
die 'You must override _sortAlgorithm() in a subclass';
# but Moose will always call the superclass which then makes it die
}
SubClass
package OrderedSort;
use namespace::autoclean; # Trims EXPORTER
use Moose;
extends 'AbstractSort';
# Override and mmpl _sortAlgorithm
override _sortAlgorithm => sub {
my $self = shift;
# ....
};
before '_sortAlgorithm' => sub {
my $self = shift;
# ...
return;
};