Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1008703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:48:41+00:00 2026-05-16T08:48:41+00:00

Is it possible to use an object as a hash key? For example, the

  • 0

Is it possible to use an object as a hash key?

For example, the following code allows me to use an instance of MyClass as a key but when I iterate over the keys and attempt to invoke the get_value method, I get the error:

Can’t locate object method “get_value” via package “MyClass=HASH(0x12a4040)” (perhaps you forgot to load “MyClass=HASH(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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:48:42+00:00Added an answer on May 16, 2026 at 8:48 am

    By default, all hash keys in Perl are strings, so what is happening in you code (which has other problems as well), is you are converting the object to a string and storing the string.

    In general, if you want to use a object as a key, the simplest way to do it is to use two data structures, one that holds your objects (an array), and another that maps the objects to some values (a hash). It is also possible to create a tied hash which will support objects as keys, but in general, tied hashes are going to be slower than simply using two data structures.

    The standard module Tie::RefHash provides a mechanism for using objects (and other references) as hash keys (that work properly when you get them back).

    use Tie::RefHash;
    tie my %hash, 'Tie::RefHash';
    
    $hash{MyClass->new(1)} = 0;  # never use the indirect object syntax
    ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use .slideUp, but the object only slideUp by (For instance)
So, I'd like to use a java map where the keys are an object...but
Is it possible to convert/reassemble the Object to Hash Object? For example and firstly,
Is it possible to use COM Object from DLL without register in C++ not
Possible Duplicate: What is it called when you use object… as a parameter? I'm
Is it possible to use a variable in an object reference in Lua? For
Is it possible to use an ActiveX/COM object from ColdFusion? If so, where's the
Is it somehow possible to use the 'With' keyword on an existing object? I
Is it possible to loop over an object's attributes in Rails? I have an
Possible Duplicate: Rails primary key and object id I am baffled as to why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.