Experimental values on scalar is now forbidden - perl

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

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

回答1:

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


回答2:

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


回答3:

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!