« blib/lib/Perl/Lint/Policy/Variables/RequireInitializationForLocalVars.pm
↑ Summary
blib/lib/Perl/Lint/Policy/Variables/RequireNegativeIndices.pm »
| 1 | package Perl::Lint::Policy::Variables::RequireLexicalLoopIterators; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use parent "Perl::Lint::Policy"; | ||
| 6 | |||
| 7 | use constant { | ||
| 8 | DESC => 'Loop iterator is not lexical', | ||
| 9 | EXPL => [108], | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub evaluate { | ||
| 13 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 14 | |||
| 15 | my $perl_version_threshold = 5.003; | ||
| 16 | my $perl_version = 6; # to hit always on default. Yes, this is Perl6 :) | ||
| 17 | |||
| 18 | my @violations; | ||
| 19 | for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) { | ||
| 20 | $token_type = $token->{type}; | ||
| 21 | |||
| 22 | if ( | ||
| 23 | $token_type == FOR_STATEMENT || | ||
| 24 | ($token_type == FOREACH_STATEMENT && $perl_version > $perl_version_threshold) | ||
| 25 | ) { | ||
| 26 | $token = $tokens->[++$i]; | ||
| 27 | $token_type = $token->{type}; | ||
| 28 | |||
| 29 | if ($token_type != VAR_DECL && $token_type != LEFT_PAREN) { | ||
| 30 | push @violations, { | ||
| 31 | filename => $file, | ||
| 32 | line => $token->{line}, | ||
| 33 | description => DESC, | ||
| 34 | explanation => EXPL, | ||
| 35 | policy => __PACKAGE__, | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | elsif ($token_type == USE_DECL || $token_type == REQUIRE_DECL) { | ||
| 40 | $token = $tokens->[++$i]; | ||
| 41 | if ($token->{type} == DOUBLE) { | ||
| 42 | $perl_version = $token->{data}; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | return \@violations; | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||