I’m trying to:
#!/usr/bin/env perl
use 5.038;
use warnings FATAL => 'all';
use autodie ':all';
use Devel::Confess 'color';
while (my ($i, $t) = each('a','b','c')) {
say "$i, $t";
}
but I get an error:
Experimental each on scalar is now forbidden
('a','b','c') is a scalar?
I really like Perl’s each on arrays, as I don’t have to declare an iterator variable.
I have also tried
while (my ($i, $t) = each(('a','b','c'))) {
while (my ($i, $t) = each(qw('a','b','c'))) {
but get the same error.
while (my ($i, $t) = each(@{ ('a','b','c') }) {
but the above gives an error: Useless use of a constant ("a") in void context which I got from how to solve the “Experimental values on scalar is now forbidden” problem in perl
How can I convince Perl’s each that (‘a’,’b’,’c’) is an array?
>Solution :
How can I convince Perl’s each that (‘a’,’b’,’c’) is an array?
You can’t, because ('a','b','c') is just a list of string literals (scalars). It is not an array variable.
Docs for each are clear
each HASH
each ARRAY
…
So it wants a variable, either a hash or an array.