我有一个名为 RuleObject 的基础对象和一个继承自 RuleObjectString 的对象。我在 RuleObjectString 中有一个新方法,我想在使用该对象的代码中调用它。但我得到了错误。“无法通过 ./testobject.pl 第 10 行的包“RuleObject”找到对象方法“比较”。但我没有创建 RuleObject。我正在创建一个 RuleObjectString。我在这里做错了什么?
测试对象.pl
1 #! /usr/bin/perl
2
3 use strict;
4
5 use RuleObjectString;
6
7 my $s = RuleObjectString->new();
8 $s->value('stuff goes here');
9
10 if ($s->compare('stuff')){
11 print "MATCH!\n";
12 }else{
13 print "no match :(\n";
14 }
规则对象.pm
package RuleObject;
our @ISA = qw/Exporter/;
our @EXPORT = qw/new/;
use strict;
sub new{
my $class = shift;
my $self;
$self->{value} = undef;
bless $self;
return $self;
}
sub value{
my $self = shift;
my $value = shift;
if ($value){
$self->{value} = $value;
}else{
return $self->{value};
}
}
规则对象字符串.pm
package RuleObjectString;
our @ISA = qw/RuleObject/;
our @EXPORT = qw/compare/;
use strict;
sub compare{
my $self = shift;
my $compareto = shift;
return $self->value() =~ /$compareto/;
}