in reply to Use a hashref as a key in another hashref?

> Is it possible to use hash reference as a key in another hash reference?

That's a classic! :)

Hash-keys are always stringified and there is no "direct" way to dereference the stringification.

BUT you are free to keep the original ref somewhere. Either as value in your data-structure or in a separate hash with $ref_of_str{"$key_hash"}=$key_hash. ¹

Thats much safer BTW cause a string doesn't increment the reference count ... i.e. keeping the reference somewhere protects the data from destruction if ref-count reaches 0.

(I hope it's clear now why dereferencing a string will hardly be ever possible)

HTH! :)

Cheers Rolf

(addicted to the Perl Programming Language)

update

¹) I rarely come into situations where I really need this trick. I'm sure your data-structure could be rewritten to avoid this, but tl;dr...

Replies are listed 'Best First'.
Re^2: Use a hashref as a key in another hashref?
by tobyink (Canon) on Jun 05, 2014 at 08:42 UTC

    The problem with this is that reference addresses are not guaranteed to be unique. If a hash is destroyed (e.g. has gone out of scope), then its address can be re-used when a new hash is created. On my machine at least, the following prints the same reference address twice:

    { my $x = { foo => 1 }; print $x, "\n"; } { my $y = { bar => 2 }; print $y, "\n"; }
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Sure that's why you have to keep the original unweakend ref for each key!

      The keystrings are guaranteed to be unique as long as the ref count is positive.

      And as soon as a hash entry is deleted the shadow entry should be deleted too.

      This could best be synchronized with a tied hash or a dedicated object.

      And that structure can be safely passed around then.(CPAN to rescue! ;)

      But as I already said this is rarely worth the trouble, avoiding refs as keys is the better design decision.

      Cheers Rolf

      (addicted to the Perl Programming Language)

Re^2: Use a hashref as a key in another hashref?
by mwb613 (Beadle) on Jun 05, 2014 at 07:49 UTC

    Thanks Rolf,

    Since it's a classic hopefully it at least brought a sense of nostalgia for you. ;)

    What you said regarding the key being stringified and references possibly being lost to garbage collection makes sense.

    My hope was to be able to traverse the hash and search for specific values in the "key hash" to determine whether I want the stats or not. Searching through a separate hash wherein I store the "keys" doesn't appeal to me much but I will test it as an option. Right now I'm reconsidering the idea of each "data set" having it's own key constructor and destructor subs that just ride around in the data structure with it. I could effectively have a sub that takes a search value and determines if it's part of the key similar a hash -- though I'm sure not as fast. For example

    #imagine the config was loaded from a YAML file my $string_search_function = $config->{'string_search_function'}; if(&{ $string_search_function }($key_string, $key_component_label, $va +lue_to_search_for)){ #do something interesting }

    Thanks again for your thoughts!