Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

5.26 sigil reference syntax in subfunction

by chenhonkhonk (Acolyte)
on Oct 31, 2017 at 21:51 UTC ( #1202480=perlquestion: print w/replies, xml ) Need Help??

chenhonkhonk has asked for the wisdom of the Perl Monks concerning the following question:

I'm not sure if this is a bug or not. I'm running perl 5, version 26, subversion 1 (v5.26.1) built for MSWin32-x64-multi-thread from Strawberry Perl. If I have a %hash, I can pass it to a function with \%hash. In the function, I can assign elements by autovivification:
$$hash{'key1'}{"$var2"} = $value;
or
$$hash->{'key1'}->{"$var2"} = $value;
BUT, and this is the real issue, if I want to print all the 'var2's, I must do something (still in the function) like:
foreach $key (keys %$hash{'key1'}->%* ){ print "$key\n"; }
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. Perl version 5.24 is when the ->%* was supposed to be a nearly 1:1 compatible replacement for %$ dereferencing (and no longer experimental), and as far as I know there's probably not supposed to be a circumstance where you need BOTH. After further testing, with the $${}{} assignment I can get the keys inside the function using %{ $$hash{'key'} } or $$hash{'key'}->%* . Is there an explanation for the referencing/syntax going on?

Replies are listed 'Best First'.
Re: 5.26 sigil reference syntax in subfunction
by dave_the_m (Monsignor) on Oct 31, 2017 at 23:49 UTC
    my $hash = { k1 => { a => 1, b => 2, c => 3. }, }; foreach $key (sort keys $hash->{'k1'}->%* ){ print "$key\n"; }
    which prints a b c.

    Dave.

      I can't use that syntax because I'm loading multidimensional values from a table that have to be pulled by a function into an array which will have an unknown length, and even has to have values removed from it. The multidimensional aspect and passing by reference is what's messy with the syntax. Edit: after looking at the end of the day, I had done the latter as the accessor of the hash, arrows on arrows.
        I didn't understand your reply at all. I showed a line of code that was almost identical to the line you were having problems with, but which exhibited the behaviour you appear to have wanted (dumping the second-level keys of multidimensional hash). If this is insufficient, you'll have to describe in much more detail what it is you're trying to achieve.

        Dave.

Re: 5.26 sigil reference syntax in subfunction
by kcott (Bishop) on Nov 01, 2017 at 04:35 UTC

    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1202480]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2019-08-26 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?