File Coverage

blib/lib/Perl/Lint/Policy/InputOutput/ProhibitReadlineInForLoop.pm
Criterion Covered Total %
statement 43 44 97.7
branch 16 18 88.8
condition 6 6 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 71 75 94.6


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::InputOutput::ProhibitReadlineInForLoop;
2 133     133   102234 use strict;
  133         265  
  133         5298  
3 133     133   613 use warnings;
  133         294  
  133         3632  
4 133     133   1240 use Perl::Lint::Constants::Type;
  133         233  
  133         83654  
5 133     133   867 use parent "Perl::Lint::Policy";
  133         239  
  133         901  
6              
7             use constant {
8 133         51328 DESC => 'Readline inside "for" loop',
9             EXPL => [211],
10 133     133   9449 };
  133         259  
11              
12             sub evaluate {
13 3     3 0 9 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 3         6 my @violations;
16 3         20 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 79         93 my $token_type = $token->{type};
18              
19 79 100 100     312 if ($token_type == FOR_STATEMENT || $token_type == FOREACH_STATEMENT) {
20 18         15 $i++;
21 18         23 $token = $tokens->[$i];
22 18         20 $token_type = $token->{type};
23 18 100 100     59 if ($token_type == DIAMOND || $token_type == LESS) {
24             # ~~~~ XXX should be HandleDecl in fact
25 8         29 push @violations, {
26             filename => $file,
27             line => $token->{line},
28             description => DESC,
29             explanation => EXPL,
30             policy => __PACKAGE__,
31             };
32 8         21 next;
33             }
34              
35 10         24 for (; my $token = $tokens->[$i]; $i++) {
36 21         24 $token_type = $token->{type};
37 21         19 my $left_paren_num = 0;
38 21 100       55 if ($token_type == LEFT_PAREN) {
39 10         9 $left_paren_num++;
40 10         24 for ($i++; my $token = $tokens->[$i]; $i++) {
41 20         23 $token_type = $token->{type};
42 20 50       61 if ($token_type == LEFT_PAREN) {
    100          
    100          
    100          
43 0         0 $left_paren_num++;
44             }
45             elsif ($token_type == RIGHT_PAREN) {
46 10 50       24 last if --$left_paren_num <= 0;
47             }
48             elsif ($token_type == DIAMOND) {
49 3         20 push @violations, {
50             filename => $file,
51             line => $token->{line},
52             description => DESC,
53             explanation => EXPL,
54             policy => __PACKAGE__,
55             };
56             }
57             elsif ($token_type == HANDLE_DELIM) {
58 6         21 for ($i++; my $token = $tokens->[$i]; $i++) {
59 12         16 $token_type = $token->{type};
60 12 100       30 if ($token_type == HANDLE_DELIM) {
61 6         40 push @violations, {
62             filename => $file,
63             line => $token->{line},
64             description => DESC,
65             explanation => EXPL,
66             policy => __PACKAGE__,
67             };
68 6         23 last;
69             }
70             }
71             }
72             }
73 10         28 last;
74             }
75             }
76             }
77             }
78              
79 3         19 return \@violations;
80             }
81              
82             1;
83