On Tue, 2002-04-16 at 14:00, Mike Lambert wrote:
> Speaking of which, how do we ensure the immutability of keys being put
> into the hash? I think Perl copied the string, so that:
> 
> $b = "aa";
> $a{$b} = 1;
> chop $b;
> print $a{"aa"};
> 
> still works.
> 
> If we start storing full thingies into the keys of a hash, we either need
> to make deep copies of these, or copy enough to ensure the hashing
> function has all that it needs.


I thought about this myself, and I don't think Perl would do it that
way. Please, Larry, et al. correct me if I'm wrong.

I suspect it would involve:

1. Copying the key (which might be a reference) on insertion.
2. Hashing once, and caching the hash.

This means a minimum of overhead, so it's a good thing. It also means
that the structure of your hash would never need to re-compute if the
hash of a particular object changes (which I would imagine it could
easily do in any number of cases).


Reply via email to