我有一个名为RuleObject的基础对象和一个从名为RuleObjectString继承的对象。我在RuleObjectString中有一个新方法,我想在使用该对象的代码中调用它。但我得到错误。 '通过包“RuleObject”在./testobject.pl第10行找不到对象方法“比较”。'但我没有创建一个RuleObject。我正在创建一个RuleObjectString。我在这里做错了什么?在Perl中扩展对象
testobject.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 }
RuleObject.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};
}
}
RuleObjectString.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/;
}
您不应该'@ EXPORT'您的类和实例方法,并且您的模块通常不应该继承'Exporter',除非它们具有真正的导出过程函数。 – pilcrow
对于需要导出*函数*的情况,'Sub :: Exporter'比'Exporter'好得多。但是,您应该避免从也是类定义的包中导出函数。 ' –