Question

I have a module which is giving me the error "Can't locate object method "isSubset" via package "a" (perhaps you forgot to load "a"?) at /path/to/set.pm line 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.

When I comment out the last line, it works fine. Can you enlighten me on what is causing the failure? Am I using glob incorrectly?

CALLING PROGRAM:

my $a = set->new('a'..'g');
my $b = set->new('b'..'f');
print $a->isSubset($b);
Was it helpful?

Solution

Turn on warnings. Perl will tell you:

Subroutine set::contains redefined at ./1.pl line 44.

You use contains for testing both an element and a set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top