File Coverage

blib/lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm
Criterion Covered Total %
statement 35 35 100.0
branch 14 14 100.0
condition 3 3 100.0
subroutine 13 13 100.0
pod 4 5 80.0
total 69 70 98.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Variables::ProhibitMatchVars;
2              
3 40     40   26775 use 5.010001;
  40         193  
4 40     40   295 use strict;
  40         119  
  40         941  
5 40     40   228 use warnings;
  40         113  
  40         1008  
6 40     40   232 use Readonly;
  40         130  
  40         2073  
7              
8 40     40   274 use Perl::Critic::Utils qw{ :severities :data_conversion };
  40         103  
  40         1969  
9 40     40   6737 use parent 'Perl::Critic::Policy';
  40         112  
  40         282  
10              
11             our $VERSION = '1.148';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Match variable used};
16             Readonly::Scalar my $EXPL => [ 82 ];
17              
18             Readonly::Array my @FORBIDDEN => qw( $` $& $' $MATCH $PREMATCH $POSTMATCH );
19             Readonly::Hash my %FORBIDDEN => hashify( @FORBIDDEN );
20              
21             #-----------------------------------------------------------------------------
22              
23 92     92 0 1628 sub supported_parameters { return () }
24 83     83 1 388 sub default_severity { return $SEVERITY_HIGH }
25 86     86 1 390 sub default_themes { return qw( core performance pbp ) }
26 35     35 1 118 sub applies_to { return qw( PPI::Token::Symbol
27             PPI::Statement::Include ) }
28              
29             #-----------------------------------------------------------------------------
30              
31             sub violates {
32 250     250 1 505 my ( $self, $elem, undef ) = @_;
33 250 100 100     478 if (_is_use_english($elem) || _is_forbidden_var($elem)) {
34 9         200 return $self->violation( $DESC, $EXPL, $elem );
35             }
36 241         2769 return; #ok!
37             }
38              
39             #-----------------------------------------------------------------------------
40              
41             sub _is_use_english {
42 250     250   415 my $elem = shift;
43 250 100       1097 $elem->isa('PPI::Statement::Include') || return;
44 67 100       310 $elem->type() eq 'use' || return;
45 65 100       1729 $elem->module() eq 'English' || return;
46              
47             # Bare, lacking -no_match_vars. Now handled by
48             # Modules::RequireNoMatchVarsWithUseEnglish.
49 6 100       150 return 0 if ($elem =~ m/\A use \s+ English \s* ;\z/xms);
50              
51 5 100       149 return 1 if ($elem =~ m/\$(?:PRE|POST|)MATCH/xms);
52 2         51 return; # either "-no_match_vars" or a specific list
53             }
54              
55             sub _is_forbidden_var {
56 247     247   1921 my $elem = shift;
57 247 100       887 $elem->isa('PPI::Token::Symbol') || return;
58 183         428 return exists $FORBIDDEN{$elem};
59             }
60              
61             1;
62              
63             __END__
64              
65             #-----------------------------------------------------------------------------
66              
67             =pod
68              
69             =head1 NAME
70              
71             Perl::Critic::Policy::Variables::ProhibitMatchVars - Avoid C<$`>, C<$&>, C<$'> and their English equivalents.
72              
73              
74             =head1 AFFILIATION
75              
76             This Policy is part of the core L<Perl::Critic|Perl::Critic>
77             distribution.
78              
79              
80             =head1 DESCRIPTION
81              
82             Using the "match variables" C<$`>, C<$&>, and/or C<$'> can
83             significantly degrade the performance of a program. This policy
84             forbids using them or their English equivalents. See B<perldoc
85             English> or PBP page 82 for more information.
86              
87             It used to forbid plain C<use English;> because it ends up causing the
88             performance side-effects of the match variables. However, the message
89             emitted for that situation was not at all clear and there is now
90             L<Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish|Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish>,
91             which addresses this situation directly.
92              
93              
94             =head1 CONFIGURATION
95              
96             This Policy is not configurable except for the standard options.
97              
98              
99             =head1 AUTHOR
100              
101             Chris Dolan <cdolan@cpan.org>
102              
103              
104             =head1 COPYRIGHT
105              
106             Copyright (c) 2006-2011 Chris Dolan.
107              
108             This program is free software; you can redistribute it and/or modify
109             it under the same terms as Perl itself. The full text of this license
110             can be found in the LICENSE file included with this module.
111              
112             =cut
113              
114             # Local Variables:
115             # mode: cperl
116             # cperl-indent-level: 4
117             # fill-column: 78
118             # indent-tabs-mode: nil
119             # c-indentation-style: bsd
120             # End:
121             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :