File Coverage

blib/lib/Perl/Lint/Policy/Variables/ProhibitMatchVars.pm
Criterion Covered Total %
statement 37 37 100.0
branch 15 18 83.3
condition 18 21 85.7
subroutine 6 6 100.0
pod 0 1 0.0
total 76 83 91.5


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Variables::ProhibitMatchVars;
2 133     133   93487 use strict;
  133         257  
  133         4750  
3 133     133   614 use warnings;
  133         195  
  133         3272  
4 133     133   1102 use Perl::Lint::Constants::Type;
  133         259  
  133         81570  
5 133     133   794 use parent "Perl::Lint::Policy";
  133         226  
  133         764  
6              
7             use constant {
8 133         54442 DESC => 'Match variable used',
9             EXPL => [82],
10 133     133   8741 };
  133         245  
11              
12             sub evaluate {
13 5     5 0 8 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 5         5 my @violations;
16 5         17 for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
17 45         47 $token_type = $token->{type};
18 45         40 $token_data = $token->{data};
19              
20 45 100 66     158 if ($token_type == SPECIFIC_VALUE) {
    100          
    100          
21 5 50 100     24 if ($token_data eq q{$`} || $token_data eq q{$&} || $token_data eq q{$'}) {
      66        
22 5         25 push @violations, {
23             filename => $file,
24             line => $token->{line},
25             description => DESC,
26             explanation => EXPL,
27             policy => __PACKAGE__,
28             };
29             }
30             }
31             elsif ($token_type == GLOBAL_VAR) {
32 3 50 100     13 if (
      66        
33             $token_data eq '$PREMATCH' ||
34             $token_data eq '$MATCH' ||
35             $token_data eq '$POSTMATCH'
36             ) {
37 3         11 push @violations, {
38             filename => $file,
39             line => $token->{line},
40             description => DESC,
41             explanation => EXPL,
42             policy => __PACKAGE__,
43             };
44             }
45             }
46             elsif ($token_type == USED_NAME && $token_data eq 'English') {
47 10         8 $token = $tokens->[++$i];
48              
49 10 100       20 if ($token->{type} == REG_LIST) {
50 9         7 $i++; # Skip the first REG_DELIM
51 9         17 for ($i++; $token = $tokens->[$i]; $i++) {
52 18         20 $token_type = $token->{type};
53 18 100       31 if ($token_type == REG_DELIM) {
    50          
54 9         19 last;
55             }
56             elsif ($token_type == REG_EXP) {
57 9         19 my @regexps = split / /, $token->{data};
58 9         12 for my $regexp (@regexps) {
59 11 100 100     48 if (
      100        
60             $regexp eq '$PREMATCH' ||
61             $regexp eq '$MATCH' ||
62             $regexp eq '$POSTMATCH'
63             ) {
64 9         45 push @violations, {
65             filename => $file,
66             line => $token->{line},
67             description => DESC,
68             explanation => EXPL,
69             policy => __PACKAGE__,
70             };
71             }
72             }
73             }
74             }
75             }
76             }
77             }
78              
79 5         18 return \@violations;
80             }
81              
82             1;
83