È possibile utilizzare un oggetto come chiave hash?Oggetto come tasto cancelletto
Ad esempio, il seguente codice mi permette di utilizzare un'istanza di MyClass come chiave, ma quando ho scandire le chiavi e tentare di richiamare il metodo get_value
, ottengo l'errore:
non può individuare metodo di un oggetto "Get_Value" tramite pacchetto "MyClass = cancelletto (0x12a4040)" (forse si è dimenticato di caricare "MyClass = cancelletto (0x12a4040)"?)
package MyClass;
use strict;
sub new
{
my $class = shift;
my $self = {
_value => shift
};
bless $self, $class;
return $self;
}
sub get_value {
my($self) = @_;
return $self->{_value};
}
my %hash =();
%hash->{new MyClass(1)} = 0;
%hash->{new MyClass(2)} = 1;
for my $key (keys %hash)
{
print $key->get_value;
}