我有一个模块,它给出了错误“无法找到对象方法”isSubset“via package”a“(也许你忘了加载”a“?)在/path/to/set.pm第121行
SET.PM:
package set; #we will create set objects, instead of treating arrays as sets
sub new{
my $packagename = shift;
my @elements = @_;
bless { 'elements' => \@elements } => $packagename;
}
sub contains{
my $set = shift;
my ($element) = @_;
foreach ($set->elements){ if( $_ eq $element ){ return 1 } }
return 0
}
sub isElement{
my ($element,$set) = @_;
return $set->contains($element)
}
sub isSubset{
my $setA = shift;
my $setB = shift;
foreach ($setA->elements){ unless( isElement($_,$setB) ){ return 0 } }
return 1
}
*subset = *isContainedIn = *isContained = \&isSubset;
sub isSuperset{
my $setA = shift;
my $setB = shift;
return $setB->isSubset($setA) # this is line 121
}
*superset = *isContaining = *contains = \&isSuperset; # when i get rid of THIS line, it works fine.
当我注释掉最后一行时,它运行正常。你能告诉我导致失败的原因吗?我错误地使用了水滴?
致电计划:
my $a = set->new('a'..'g');
my $b = set->new('b'..'f');
print $a->isSubset($b);
答案 0 :(得分:3)
开启warnings
。 Perl会告诉你:
Subroutine set::contains redefined at ./1.pl line 44.
您使用contains
来测试元素和集合。