busy
busy

Reputation: 369

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'
]]

Upvotes: 10

Views: 23388

Answers (3)

ulix
ulix

Reputation: 373

I've not tested, but based on written definition of an AoA reference, I think:

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

Upvotes: 1

sidyll
sidyll

Reputation: 59297

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]})

Upvotes: 25

Hunter McMillen
Hunter McMillen

Reputation: 61520

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] } ) {
  ....
}

Upvotes: 9

Related Questions