%foo is a hash, @foo{@keys} is a hash slice and $foo{key} is a hash element. It has nothing to do with @foo and $foo at this point (except that if they are global, they share a glob).

\%foo is a reference to the same hash which can be assigned to $foo. Then, $foo->{key} is that element again.

%foo->{key} is wrong. -> should only be used with references and classes. To either call a method on the reference if its referent is blessed (or the LHS is a class), or to access an element in the refered hash or array. %foo is not a reference, so the arrow should not be used with it.

Perl 6 will no longer use sigils to indicate how the variable is used. In Perl 5, we use a different sigil if we use the hash differently. That is: % to get the entire hash, @ to get a slice and $ to get a single element. In Perl 6, we use % for hashes, % for hashes and %for hashes. That meas %foo{key} will be valid.

Your %foo->{key} relies on a bug in Perl 5. The same, translated to Perl 6 would be %foo.{key}, which would rely on a feature in Perl 6. That feature is automatic referencing, so that what you wrote is in fact (\%foo).{key}.

%foo->{key} is unclear because no experienced Perl coder is used to seeing the arrow operator with something that is not a reference or a class. What about unexperienced Perl coders then? Well, they don't understand what it means, try to find out and find that it is not documented. They will take for granted that it works, probably not knowing that you really meant $foo{key}.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


In reply to Re: %h->{foo} vs. $h{foo}.. by Juerd
in thread %h->{foo} vs. $h{foo}.. by smferris

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.