develooper Front page | perl.perl5.porters | Postings from October 2002

References as hash keys overview (was Re: Collections)

Thread Previous | Thread Next
From:
Roland Giersig
Date:
October 2, 2002 03:17
Subject:
References as hash keys overview (was Re: Collections)
Message ID:
1033553872.3d9ac7d00b3e3@webmail.namezero.com
Hmm, seems like an overview could be helpful:

We are talking about using references as hash keys, e.g.

  $a = [];
  $b = {};
  $c = \*main;
  $d = \"gunk";
  $e = sub { 'foo' };
  %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
  $a->[0] = 'foo';
  $b->{foo} = 'bar';
  for (keys %h) {
    print ref($_), "\n";
  }

as provided by the Tie::RefHash module.

But the discussion also started to circulate around related topics: how 
to compare two arbitrary objects for equalness and how to produce a 
hash key for an arbitrary complex object.

The semantics of Tie::RefHash are well-defined, it was proposed to 
create an XS version for speed-up and as a prototype of what could be 
later included into the core.  I'd like to discuss a different way:  

You can already use a reference as a hash key, but it will be converted 
into its stringized form, so 'keys %h' returns a list of scalars, not a 
list of references.  How about adding a special case to hash key 
handling: test if the key is a ref and use the stringized form to find 
a bucket entry (should already be in place), but store the ref there 
(this should be new), not the stringized scalar.  As the stringized 
form can stay attached to the reference as an alternate form, this 
would mean minimal overhead, probably only the look-up inside the 
bucket has to be adapted to also include testing for a ref.  

I have to admit I haven't taken a look at the source code and am 
speculating from what I've read about internal data structures in the 
perldocs, so this is just guessing, please forgive me if I am way off.

Regarding the second topic: how to compare complex objects for 
equalness should be further discussed, but the proposed way of creating 
a serialized version of the object via Storable (maybe with MD5 or 
compression) seems OK to me.  For blessed objects, a 'cmp' method could 
be used (if it is there) to determine equalness (which can take care of 
object peculiarities). 

But this definitely doesn't have to be included into the core at this 
time as it is quite slow, so an external module (Tie::ObjHash?) should 
be written first as a proof of concept.

Just my EUR 0.02.

Roland
-- 
RGiersig@cpan.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About