Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Experimental keys on scalar is now forbidden

by Anonymous Monk
on May 09, 2018 at 10:40 UTC ( [id://1214264]=perlquestion: print w/replies, xml ) Need Help??

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

I used to access my hash of hashes with the following in my old Perl (in a foreach my $word loop):

my @variants = keys $hash{$word}{variants};

In Perl 5.24 I get the following error:

Experimental keys on scalar is now forbidden

What is the right way to write it now?

Replies are listed 'Best First'.
Re: Experimental keys on scalar is now forbidden
by haukex (Archbishop) on May 09, 2018 at 10:45 UTC
    my @variants = keys $hash{$word}{variants};
    What is the right way to write it now?

    The "right" way to write it, or rather, the way that will work on any version of Perl 5, is:

    my @variants = keys %{ $hash{$word}{variants} };

    Update: See also perlreftut, perlref, perldsc, and Experimental push on scalar now forbidden.

    As of Perl 5.24, you can also make use of the Postfix Dereference Syntax:

    use 5.024; my @variants = keys $hash{$word}{variants}->%*;

    (Update 2: It's actually been present since 5.20, but it was still experimental then and had to be explicitly enabled.)

Re: Experimental keys on scalar is now forbidden
by huck (Prior) on May 09, 2018 at 10:47 UTC

    I would use

    my @variants = keys %{$hash{$word}{variants}};

Log In?
Username:
Password:

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

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

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.