Says snax:

> So this would suggest that in this context %h and @a above are
> references--or treated as references.
>
> Anyone know if there is a deep reason for this?

It would suggest that, but it isn't really true. Since it isn't true, there isn't any reason for it at all, deep or otherwise.

Here is what is really going on: When you write $h->{key}, Perl looks up the glob *h, and extracts the scalar part of the glob. Then it converts the scalar into a hash using a routine called rv2hv; if the scalar did not contain a hashref, rv2hv would raise a fatal error.

When you write %h->{key} it is almost the same. Perl looks up the glob *h and extracts the hash part of the glob instead of the scalar part. Then it calls rv2hv. rv2hv is written so that if it has a hash already, it simply returns it, as if it had been passed a reference to that hash in the first place.

rv2hv could have raised an error here, but it doesn't. As a result, there's an oddity in the language that you can write %h->{key} and Perl interprets it as if you had written {\%h}->{key} instead.

(If $h or %h is a lexical variable, replace 'looks up the glob' with 'looks in the pad', as appropriate.)


In reply to Re: What's with the -> operator? by Dominus
in thread What's with the tt-/tt operator? by snax

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.