Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use Perl's each with a list?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

('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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading