port5432
port5432

Reputation: 6381

Syntax of Perl for loop

I've inherited a Perl script, and don't have a Perl background (yet). The script is failing:

syntax error at /home/assay/assay/bin/mpprimer/MPprimer/bin/MPprimer_dimer_check.pl line 181, near "$k qw(a g c t)"

This is the relevant code

# create a binding array for each of the four bases
for $l (0 .. $pfl-1) {
    my $mbase = substr($fprimer_r, $l, 1);
    $primer_hash{$mbase}[$l]=1;
    for $k qw(a g c t) {
        $primer_hash{$k}[$l] ||=0;
    }
}

Any pointers or suggestions would be appreciated.

Upvotes: 3

Views: 1168

Answers (2)

simbabque
simbabque

Reputation: 54323

You probably updated your Perl to 5.18. In that version the qw operator was changed to not behave like it was parentheses. From perl5180delta:

qw(...) can no longer be used as parentheses

qw lists used to fool the parser into thinking they were always surrounded by parentheses. This permitted some surprising constructions such as foreach $x qw(a b c) {...} , which should really be written foreach $x (qw(a b c)) {...} . These would sometimes get the lexer into the wrong state, so they didn't fully work, and the similar foreach qw(a b c) {...} that one might expect to be permitted never worked at all.

This side effect of qw has now been abolished. It has been deprecated since Perl v5.13.11. It is now necessary to use real parentheses everywhere that the grammar calls for them.

So there was a warning since Perl 5.13.11. If you didn't see it, it could be that you upgraded from a version earlier than 5.14.0* or because you never looked at your error logs.

The warning would have been the following, which is found in perldiag (use grep/ctrl+f).

Use of qw(...) as parentheses is deprecated

*) uneven minor versions in Perl 5 refer to developer releases, even ones to official stable releases

Upvotes: 10

jwodder
jwodder

Reputation: 57470

You need to put parentheses around the list being iterated over (the qw// construct in this case):

for $k (qw(a g c t)) {

Upvotes: 5

Related Questions