Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found