in reply to HASH keys preserve class ?

Hash entries (values) can be anything, including objects.
Hash keys can only be strings.

If you need objects for keys, have a look at Tie::RefHash.

Update: Here's an example of storing an object:

{ package MyClass; sub new { bless {}, shift } sub doit { print("Method called\n"); } } my $o = MyClass->new(); my %hash = ( object => $o ); $hash{object}->doit(); # Prints "Method called"

Replies are listed 'Best First'.
Re^2: HASH keys preserve class ?
by Limbic~Region (Chancellor) on Oct 04, 2006 at 12:42 UTC
    ikegami,
    To be pedantic about it, hash key values can only be scalars. A reference happens to be a scalar so a hash value can get to anything you can reference. Since you can reference just about anything under the sun, you can can get to just about anything.

    To be less pedantic, I thought I heard someone say there was an XS way to make hash keys non-string but I know nothing about it beyond a vague recollection.

    Cheers - L~R

      First you say keys are scalars not strings, then you say there might be a way of allowing things other than strings.

      Except possibly through an XS trick, keys can only be strings. perldata confirms this: "Hashes are unordered collections of scalar values indexed by their associated string key." If you try to use a reference as a key, the reference will not be a key of the hash. It's string representation will.

      I guess your point was that you could attempt to use a non-string as a key. That is indeed valid. The scalar will be stringified before being used as a key.

      The following is an attempt to use a reference as a key in a hash:

      use strict; use warnings; my @var = qw( a b c ); my $ref = \@var; my %hash; $hash{$ref} = 1; foreach (keys %hash) { print @$_, "\n"; }

      In the latest Perl (5.8.8), the above code outputs

      Can't use string ("ARRAY(0x225fb8)") as an ARRAY ref while "strict ref +s" in use
        ikegami,
        First you say keys are scalars not strings

        Hrmm. I guess I should have proofed my response a 4th time before submitting. The first paragraph was intended to address "values" and the second "keys". Where I said "...hash key values can only be scalars.", I meant hash values. In fact, I said that later on in the same paragraph "...so a hash value can...".

        Regarding non-string keys - I assume some trickery that can only be done in XS land would get around perldata. Of course, I don't remember who I heard discussing it or if I understood what they were saying but I suspect it would have been diotalevi or perhaps demerphq.

        In any case, my correction to your post was concerning values only. Sorry my incorrect wording implied otherwise.

        Cheers - L~R