I should say that I don't believe I've fully understood what you're looking for.

In answer to the first part of your question, if you need to use any kind of reference as a hash key, what's wrong with using the stringified version of it as a hash key? It's unique (even under threads and fork) so it should do the trick. Yet you should think about what this means: it is the reference itself that it the key in this case, not the contents (whatever you get when you deference it). Is this what you mean? Maybe. I can think of times when this is useful. The only caveat is that the stringified version of the reference won't increase the reference count of the object itself, so you will have to be careful that the object isn't destroyed when you still need it. Embedding the reference inside the value of the hash element for which it is the key might be a good trick to force that.

In the second part of your question though you seem to want something different: you seem to want to index based on the value of the object pointed to. This will not work of course: [ qw( a b c ) ] != [ qw (a b c ) ] (they are different objects). Do you want to use a serialized (stringified) representation of the contents of the reference? That's not very doable in general (think of self referential data structures or structures that contain code references). So I guess it depends on what you are really trying to do.

A major disclaimer being: I am a HUGE fan of programatic data structures. By this I mean I have a tendancy to create static complex data structures that will allow for conditional switching, and cool one-liners to utilize easily modifiable structures rather than having to go back for raw logic changes later; just change the structure.

I don't consider that "disclaimer material". Behaviour driven by data ans the real "code" only acts in support of that. It's very maintainable and often self documenting. A very good practice, in my opinion, as long as you pay attention to security issues.


In reply to Re: Array refs as hash keys by Celada
in thread Array refs as hash keys by wazzuteke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.