Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day chenhonkhonk,

Welcome to the Monastery.

Those first two expressions you wrote are not the same:

$ perl -MO=Deparse -e '$$hash{"key1"}{"$var2"} = $value' $hash->{'key1'}{"$var2"} = $value; -e syntax OK $ perl -MO=Deparse -e '$$hash->{"key1"}->{"$var2"} = $value' $$hash->{'key1'}{"$var2"} = $value; -e syntax OK

You can apply postfix dereferencing to the second one, like so:

$ perl -MO=Deparse -e '$hash->$*->{"key1"}{"$var2"} = $value' $$hash->{'key1'}{"$var2"} = $value; -e syntax OK

Note that's "->$*", not "->%*".

In your last expression (with keys), your problem appears to be that you're attempting to dereference twice: once with a "%" at the start, and again with a "->%*" at the end.

$ perl -MO=Deparse -e '%$hash{"key1"}->%*' %{%$hash{'key1'}}; -e syntax OK

One of these two is probably what you are after:

$ perl -MO=Deparse -e '%{ $hash{"key1"} }' %{$hash{'key1'};}; -e syntax OK $ perl -MO=Deparse -e '$hash{"key1"}->%*' %{$hash{'key1'}}; -e syntax OK

As a general rule, when dereferencing anything more complex than a simple scalar (e.g. %$href, @$aref, etc.), and not using postfix dereference syntax, I'd recommend wrapping the reference in braces and adding the appropriate sigil in front of that (e.g. %{ ... }, @{ ... }, etc.): this makes both the code, and your intent, very clear. Some might suggest you do this always; my personal view is that it's unnecessary in the simplest cases.

When I first saw postfix deference syntax as an experimental feature in 5.20 ("perl5200delta: Experimental Postfix Dereferencing"), I thought it looked a bit weird and, as I generally avoid experimental features, didn't give it much further attention. However, I started using it in 5.24 when the "experimental" flag was lifted ("perl5240delta: Postfix dereferencing is no longer experimental") and now I much prefer it. I think it makes the code easier to read: there's a straightforward left-to-right progression; as opposed to having to go backwards to find the sigil, then downwards into nested braces to find what that sigil refers to.

For perlref documentation, see "Postfix Dereference Syntax" and "Postfix Reference Slicing".

— Ken


In reply to Re: 5.26 sigil reference syntax in subfunction by kcott
in thread 5.26 sigil reference syntax in subfunction by chenhonkhonk

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (11)
    As of 2019-09-23 13:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The room is dark, and your next move is ...












      Results (279 votes). Check out past polls.

      Notices?