简体   繁体   中英

Experimental values on scalar is now forbidden - perl

Experimental values on scalar is now forbidden in old software:

$link = Winners::Links->new();
my @fields = $link->column_names;     
foreach my $field ( values @fields[0]) {

I tried to make :

 foreach my $field ( values {@fields[0]}) {

 foreach my $field ( values %{@fields[0]}) {

 foreach my $field ( values %@fields[0]) {

Non of them works. Any Idea how it should be done? Thx.

Here is more on @fields object definition:

[[
  'id',
  'entry',
  'selection',
  'status'
]]

This was added in Perl 5.14 but removed in 5.23:

Experimental %s on scalar is now forbidden (F) An experimental feature added in Perl 5.14 allowed each, keys, push, pop, shift, splice, unshift, and values to be called with a scalar argument. This experiment is considered unsuccessful, and has been removed. The postderef feature may meet your needs better.

So if you were using it on a reference, dereference it first. There is some confusion arriving here though because of your original code:

foreach my $field ( values @fields[0]) {

Here @fields[0] is actually a slice, which is valid, and works. But with strict and warnings you would get something like:

Scalar value @fields[0] better written as $fields[0] at - line x.

In fact, if you're accessing an item (like a reference, probably in your case) you should be using $fields[0] instead. So first correct that, and then dereference to conform to the standard requirement for values (being a list. It accepted a scalar only as an experimental feature in the past).

foreach my $field ( values %{$fields[0]})

You de-reference an array using the $ sigil when you want a single value, not the @ sigil.

Try using:

foreach my $field ( values %{ $fields[0] } ) {
  ....
}

我没有测试过,但基于AoA参考的书面定义,我认为:

foreach my $field ( @{ $fields[0] } ) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM