Sandra Schlichting
Sandra Schlichting

Reputation: 26006

How to fix "Experimental values on scalar is now forbidden"

In Perl 5.26.1 I get:

Experimental values on scalar is now forbidden at /funcx.pm line 110.

Where line 110 is the foreach in

sub checkSsh {
    foreach my $slave (values $::c{slaves}) {
      ...
    }
}

$c contains

$VAR1 = {
          'slaves' => {
                        '48' => '10.10.10.48'
                      },
        };

where

our %c = %{YAML::Syck::LoadFile($config)};

Question

What is actually the problem? And how should it be fixed?

Upvotes: 2

Views: 3301

Answers (2)

arno teunisse
arno teunisse

Reputation: 1

my $arr = 
[
        'entry1'  => {
                'first' => 'First1',
                'last'  => 'Last1',
        },
        'entry2' =>
        {
                'first' => 'First2',
                'last'  => 'Last2',
        },
];
$Data::Dumper::Indent=0;
say Data::Dumper->Dump ( [ $arr ] , [ 'arr'] );
say keys $arr->[0]->{entry1}->%* ;

Data::Dumper has no problem with it :

$arr = ['entry1',{'last' => 'Last1','first' => 'First1'},'entry2',{'last' => 'Last2','first' => 'First2'}];

But perl has :

Can't use string ("entry1") as a HASH ref while "strict refs" in use at hash_array.pl line 22.

the right way to go :

my $arr = [
    {'entry1'  => {
            'first' => 'First1',
            'last'  => 'Last1',
        }
    },
    { 'entry2' =>
        {
            'first' => 'First2',
            'last'  => 'Last2',
        }
    },
];
$Data::Dumper::Indent=0;
say Data::Dumper->Dump ( [ $arr ] , [ 'arr'] ) ;
say keys %{$arr->[1]->{entry2}}  ;
for my $idx ( 0 .. $arr->$#* ) {
    for my $key ( keys $arr->[$idx]->%* ) {
        for my $key1 ( keys $arr->[$idx]->{$key}->%* ) {
            say "$key1 => $arr->[$idx]->{$key}{$key1}";
        }
    }
}
first => First1
last => Last1
last => Last2
first => First2

using perl 5, version 40, subversion 0 (v5.40.0)

For the "strange" $#* and %* see :

Postfix-Dereference-Syntax

Upvotes: -1

Hunter McMillen
Hunter McMillen

Reputation: 61520

Perl is complaining that you are calling the values builtin on a SCALAR, in this case a HASHREF:

Properly de-referencing your slaves key allows values to work as expected:

foreach my $slave ( values %{ $c{slaves} } ) {
  ...
}

As to the specific warning you receive, they address that directly in the perldoc page:

Starting with Perl 5.14, an experimental feature allowed values to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24.

To avoid confusing would-be users of your code who are running earlier versions of Perl with mysterious syntax errors, put this sort of thing at the top of your file to signal that your code will work only on Perls of a recent vintage:

use 5.012;  # so keys/values/each work on arrays

Upvotes: 6

Related Questions