Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: 5.26 sigil reference syntax in subfunction

by kcott (Bishop)
on Nov 01, 2017 at 04:35 UTC ( #1202493=note: print w/replies, xml ) Need Help??


in reply to 5.26 sigil reference syntax in subfunction

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

Replies are listed 'Best First'.
Re^2: 5.26 sigil reference syntax in subfunction
by chenhonkhonk (Acolyte) on Nov 01, 2017 at 17:19 UTC

    What you wrote is not what I am doing. What you suggest produces errors, which I said in the original post: Omitting the ->%* produces an error "Experimental keys on scalar is now forbidden" and if I omit the leading % it says the global symbol $hash is not defined. ... After further testing, with the $${}{} assignment I can get the keys inside the function using %{ $$hash{'key'} } or $$hash{'key'}->%*

      kcott is actually suggesting that you follow rule 2 in Using References. This rule always works. All the other dereferencing rules are essentially shortcuts that work in special situations.
      Bill

        G'day Bill,

        ++ Thanks for posting that link.

        It's been quite some time since I read that, and had forgotten it was there; however, that does reflect what I was saying (plus examples, plus gotchas).

        — Ken

        Rule 2 case 3 is closest to what I am doing. The perlref page is pretty poor on explaining assigning values to subthings when the original value is a reference.

        Additionally, arrows can be omitted between brackets and braces EXCEPT when the first element is a reference (otherwise you must have $$ref{}{}thing or $ref->{}{}thing is a pretty glaring exception to "easier reading". Having ->% but not ->\% just adds to the complexity of when and which order symbols have to be used during referencing/dereferencing.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1202493]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2019-09-21 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (269 votes). Check out past polls.

    Notices?