perl

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?


Solution

  • 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