Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Experimental values on scalar is now forbidden - perl

Tags:

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'
]]
like image 631
busy Avatar asked Dec 16 '16 21:12

busy


3 Answers

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]})
like image 69
sidyll Avatar answered Nov 19 '22 21:11

sidyll


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] } ) {
  ....
}
like image 21
Hunter McMillen Avatar answered Nov 19 '22 22:11

Hunter McMillen


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

foreach my $field ( @{ $fields[0] } ) {...}
like image 1
ulix Avatar answered Nov 19 '22 22:11

ulix