blib/lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm

filter: Statementstmt 90.2% 37 / 41 Branchbran 57.1% 8 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 80.7% 63 / 78 CC 9 SCAR 13.8
CC
9
Cov
79%
_is_use_english
9.8
violates
3.8
_is_forbidden_var
2.0
CRAP 4.0
« blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProhibitBuiltinHomonyms.pm »
1 package Perl::Critic::Policy::Variables::ProhibitMatchVars;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :data_conversion };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.150';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $DESC => q{Match variable used};
16 40 Readonly::Scalar my $EXPL => [ 82 ];
17  
18 40 Readonly::Array my @FORBIDDEN => qw( $` $& $' $MATCH $PREMATCH $POSTMATCH );
19 40 Readonly::Hash my %FORBIDDEN => hashify( @FORBIDDEN );
20  
21 #-----------------------------------------------------------------------------
22  
23 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
24 75 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 86 ▶sub default_themes { return qw( core performance pbp ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
26 33 ▶sub applies_to { return qw( PPI::Token::Symbol
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
27 PPI::Statement::Include ) }
28  
29 #-----------------------------------------------------------------------------
30  
31 sub violates {
32 239 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=3 SCAR=13.3
Pod documented
33 239 ▶ if (_is_use_english($elem) || _is_forbidden_var($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_use_english($elem) or _is_forbidden_var($elem)) 0 239 239
Condition33%1/3
_is_use_english($elem) or _is_forbidden_var($elem)
l!l&&r!l&&!r
00239
MC/DC0%0/2
_is_use_english($elem) or _is_forbidden_var($elem)
_is_use_english($elem) _is_forbidden_var($elem)
Truth table33%1/3
A or B
A= _is_use_english($elem)B= _is_forbidden_var($elem)
ABresult
1X1
011
000
34 0 return $self->violation( $DESC, $EXPL, $elem );
35 }
36 239 return; #ok!
37 }
38  
39 #-----------------------------------------------------------------------------
40  
41 sub _is_use_english {
42 239 ▶ my $elem = shift;
Subroutine _is_use_english: called CC=6 SCAR=22.8
43 239 ▶ $elem->isa('PPI::Statement::Include') || return;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Statement::Include") or return 178 61 239
44 61 ▶ $elem->type() eq 'use' || return;
Branch: 1 branch
BranchTrueFalseTotal
$elem->type eq "use" or return 2 59 61
45 59 ▶ $elem->module() eq 'English' || return;
Branch: 1 branch
BranchTrueFalseTotal
$elem->module eq "English" or return 59 0 59
46  
47 # Bare, lacking -no_match_vars. Now handled by
48 # Modules::RequireNoMatchVarsWithUseEnglish.
49 0 ▶ return 0 if ($elem =~ m/\A use \s+ English \s* ;\z/xms);
Branch: 1 branch
BranchTrueFalseTotal
if $elem =~ /\A use \s+ English \s* ;\z/msx 0 0 0
50  
51 0 ▶ return 1 if ($elem =~ m/\$(?:PRE|POST|)MATCH/xms);
Branch: 1 branch
BranchTrueFalseTotal
if $elem =~ /\$(?:PRE|POST|)MATCH/msx 0 0 0
52 0 return; # either "-no_match_vars" or a specific list
53 }
54  
55 sub _is_forbidden_var {
56 239 ▶ my $elem = shift;
Subroutine _is_forbidden_var: called CC=2 SCAR=6.9
57 239 ▶ $elem->isa('PPI::Token::Symbol') || return;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Symbol") or return 61 178 239
58 178 return exists $FORBIDDEN{$elem};
59 }
60  
61 40 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 :
« blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProhibitBuiltinHomonyms.pm »