perlperl-critic

Perl + PerlCritic | loop iterator is not lexical


I have this code

...
    my $line = '';
        foreach $line ( split( /\n/x, $raw ) ) {
            chomp $line;
            my ( $key, $val ) = split( /=/x, $line );
            $param{$key} = $val;
        }
...

After perlcritic checking, i get messsage "Loop iterator is not lexical." Whats wrong?

I can use

 #my $line = '';
            foreach my $line ( split( /\n/x, $raw ) )

but why? :)


Solution

  • PerlCritic wants the loop variable to only have a scope within the loop, i.e. the variable should not exist after the end of the loop. This could be considered excessively purist/pedantic, but I tend to agree, and usually write my Perl code that way as well.

    Also, this looks like a configurable option