Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

HASH keys preserve class ?

by Anonymous Monk
on Oct 04, 2006 at 03:00 UTC ( #576233=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

the C++ folks say that PERL associative hash entries lose their class (and require that the value be blessed back into their previous class) true or false?

i thought that this was true for Data:Dumper; but not for perl executable. since i have perl objects that get passed around to routines in argument hashes; i tend to not believe their claim! But i am curious where this folklore originated....

Replies are listed 'Best First'.
Re: HASH keys preserve class ?
by diotalevi (Canon) on Oct 04, 2006 at 03:19 UTC

    Utterly false when it comes to hash values. Hash values are stored without change. If they notice changes it's because of bugs in the code they're observing.

    Half true when its hash keys. Hash keys are always strings so if you use an object as a hash key the hash has only the stringified object as a key. You can generally expect the object to remain unchanged and continue to be an ordinary object. You can always expect the hash key to only be a string. Stringification is per perl's normal rules unless the object has stringification overloading and then it can be whatever is appropriate.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: HASH keys preserve class ?
by ikegami (Pope) on Oct 04, 2006 at 05:18 UTC

    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"
      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
Re: HASH keys preserve class ?
by chromatic (Archbishop) on Oct 04, 2006 at 06:04 UTC

    They're thinking of Java, which casts all objects to Object when storing them in collections.

Re: HASH keys preserve class ?
by ioannis (Abbot) on Oct 04, 2006 at 05:47 UTC
    Like other said, hash keys can only be strings. In addition, these strings also lose their tainted information -- that is, if they are later retrieved from the hash, they are considered untainted.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://576233]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2020-07-16 17:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found