1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Variables::ProhibitMatchVars;
15 use Perl::Critic::Utils qw{ :severities :data_conversion };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Match variable used};
23 Readonly::Scalar my $EXPL => [ 82 ];
25 Readonly::Array my @FORBIDDEN => qw( $` $& $' $MATCH $PREMATCH $POSTMATCH );
26 Readonly::Hash my %FORBIDDEN => hashify( @FORBIDDEN );
28 #-----------------------------------------------------------------------------
30 sub supported_parameters { return () }
31 sub default_severity { return $SEVERITY_HIGH }
32 sub default_themes { return qw( core bugs pbp ) }
33 sub applies_to { return qw( PPI::Token::Symbol
34 PPI::Statement::Include ) }
36 #-----------------------------------------------------------------------------
39 my ( $self, $elem, undef ) = @_;
40 if (_is_use_english($elem) || _is_forbidden_var($elem)) {
41 return $self->violation( $DESC, $EXPL, $elem );
46 #-----------------------------------------------------------------------------
50 $elem->isa('PPI::Statement::Include') || return;
51 $elem->type() eq 'use' || return;
52 $elem->module() eq 'English' || return;
54 # Bare, lacking -no_match_vars. Now handled by
55 # Modules::RequireNoMatchVarsWithUseEnglish.
56 return 0 if ($elem =~ m/\A use \s+ English \s* ;\z/xms);
58 return 1 if ($elem =~ m/\$(?:PRE|POST|)MATCH/xms);
59 return; # either "-no_match_vars" or a specific list
62 sub _is_forbidden_var {
64 $elem->isa('PPI::Token::Symbol') || return;
65 return exists $FORBIDDEN{$elem};
72 #-----------------------------------------------------------------------------
78 Perl::Critic::Policy::Variables::ProhibitMatchVars - Avoid C<$`>, C<$&>, C<$'> and their English equivalents.
82 This Policy is part of the core L<Perl::Critic> distribution.
87 Using the "match variables" C<$`>, C<$&>, and/or C<$'> can
88 significantly degrade the performance of a program. This policy
89 forbids using them or their English equivalents. See B<perldoc
90 English> or PBP page 82 for more information.
92 It used to forbid plain C<use English;> because it ends up causing the
93 performance side-effects of the match variables. However, the message
94 emitted for that situation was not at all clear and there is now
95 L<Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish>,
96 which addresses this situation directly.
101 This Policy is not configurable except for the standard options.
106 Chris Dolan <cdolan@cpan.org>
110 Copyright (c) 2006-2008 Chris Dolan. All rights reserved.
112 This program is free software; you can redistribute it and/or modify
113 it under the same terms as Perl itself. The full text of this license
114 can be found in the LICENSE file included with this module.
120 # cperl-indent-level: 4
122 # indent-tabs-mode: nil
123 # c-indentation-style: bsd
125 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :