I get the error “Not a HASH reference” with the following code. What is the proper way to test exists in a hash reference that’s a member variable of a class?
package TestClass;
sub new {
my ($class) = @_;
my $self = {
_ht => \{}
};
bless $self, $class;
return $self;
}
sub itemExists {
my ($self, $key) = @_;
my $itemExists = 0;
if(exists $self->{_ht}->{$key}) { # ERROR HERE: Not a HASH reference
$itemExists = 1;
}
return $itemExists;
}
1;
# ------------------------------------------
package Main;
my $t = new TestClass();
$t->itemExists('A')
In your constructor, you initialized
$self->{_ht}to\{}, which is a reference to a hashref. Change it to