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

If the value of an element in a hash is a reference, then<KBD> ref($hash{"keyname"})</KBD>
returns something like<KBD> SCALAR</KBD>.

The result is much different if the key itself contains a reference.

  print ref( $key ) foreach $key (keys %hash);

<KBD>ref( $key ) </KBD>returns nothing if "$key" is in fact a reference. So I have been
resorting to testing, say:

  index($key, "SCALAR")

... which seems to border on downright cheesy. Is there some less cheesy way
to make "$key" give up its little secret?

Replies are listed 'Best First'.
Re: If a hash key is a reference...
by btrott (Parson) on Jun 18, 2000 at 01:24 UTC
    A hash key is never a reference, even if it looks like one. Hash keys are automatically stringified, which means that they're changed into strings, and a stringified reference can no longer be treated as a reference.
Re: If a hash key is a reference...
by cbraga (Pilgrim) on Jun 18, 2000 at 05:13 UTC
    You can use the Tie::RefHash module.
    The following should work:

    use Tie::RefHash;

    tie %hash, "Tie::RefHash";
    $hash{$somereference} = ...