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}};