これが可能かどうかはわかりませんが、既知のサブクラス関数を Perl から呼び出したいと考えています。より具体的なものを呼び出すには、「一般的な」ものが必要です。私のスーパークラスは、それをサブクラス化するすべてのクラスに既知の関数が定義されていると想定します。これはJavaの「実装」に似ていると思います。
たとえば、次のコードがあるとします。
GenericStory.pm
package Story::GenericStory;
sub new{
my $class = shift;
my $self = {};
bless $self, class;
return $self;
}
sub tellStory {
my $self;
#do common things
print "Once upon a time ". $self->specifics();
}
#
Story1.pm
package Story::Story1;
use base qw ( Story::GenericStory );
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
return $self;
}
sub specifics {
my $self;
print " there was a dragon\n";
}
#
Story2.pm
package Story::Story2;
use base qw ( Story::GenericStory );
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
return $self;
}
sub specifics {
print " there was a house\n";
}
#
MAIN
my $story1 = Story::Story1->new();
my $story2 = Story::Story2->new();
#Once upon a time there was a dragon.
$story1->tellStory();
#Once upon a time there was a house.
$story2->tellStory();
編集:
コードは正常に動作します。「my $self = shift;」を忘れただけです。tellStory()で;