1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Modules/RequireNoMatchVarsWithUseEnglish.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish;
16 use Perl::Critic::Utils qw< :characters :severities >;
17 use base 'Perl::Critic::Policy';
19 our $VERSION = '1.088';
21 #-----------------------------------------------------------------------------
23 Readonly::Scalar my $EXPL =>
24 q{"use English" without the '-no_match_vars' argument degrades performance.'};
25 Readonly::Scalar my $DESC => q{"use English" without '-no_match_vars' argument};
27 #-----------------------------------------------------------------------------
29 sub supported_parameters { return () }
30 sub default_severity { return $SEVERITY_LOW }
31 sub default_themes { return qw( core performance ) }
32 sub applies_to { return 'PPI::Statement::Include' }
34 #-----------------------------------------------------------------------------
37 my ( $self, $elem, $doc ) = @_;
39 # "require"ing English is kind of useless.
40 return if $elem->type() ne 'use';
41 return if $elem->module() ne 'English';
43 my @elements = $elem->schildren();
44 shift @elements; # dump "use"
45 shift @elements; # dump "English"
48 return $self->violation($DESC, $EXPL, $elem);
51 _skip_version_number( \@elements );
53 @elements = _descend_into_parenthesized_list_if_present(@elements);
56 return $self->violation($DESC, $EXPL, $elem);
59 my $current_element = $elements[0];
61 while ( $current_element ) {
62 if ( $current_element->isa('PPI::Token::Quote') ) {
63 return if $current_element->string() eq '-no_match_vars';
65 elsif ( $current_element->isa('PPI::Token::QuoteLike::Words') ) {
66 return if $current_element->content() =~ m/-no_match_vars \b/xms;
69 not $current_element->isa('PPI::Token::Operator')
70 or $current_element->content() ne $COMMA
71 and $current_element->content() ne $FATCOMMA
73 return $self->violation($DESC, $EXPL, $elem);
77 $current_element = $elements[0];
80 return $self->violation($DESC, $EXPL, $elem);
84 sub _skip_version_number {
85 my ($elements_ref) = @_;
87 my $current_element = $elements_ref->[0];
89 if ( $current_element->isa('PPI::Token::Number') ) {
90 shift @{$elements_ref};
94 and $current_element->isa('PPI::Token::Word')
95 and $current_element->content() =~ m/\A v \d+ \z/xms
96 and $elements_ref->[1]->isa('PPI::Token::Number')
98 # The above messy conditional necessary due to PPI not handling
100 shift @{$elements_ref};
101 shift @{$elements_ref};
107 sub _descend_into_parenthesized_list_if_present {
110 return if not @elements;
112 my $current_element = $elements[0];
114 if ( $current_element->isa('PPI::Structure::List') ) {
115 my @grand_children = $current_element->schildren();
116 if (not @grand_children) {
120 my $grand_child = $grand_children[0];
122 if ( $grand_child->isa('PPI::Statement::Expression') ) {
123 my @great_grand_children = $grand_child->schildren();
125 if (not @great_grand_children) {
129 return @great_grand_children;
132 return @grand_children;
143 #-----------------------------------------------------------------------------
149 Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish - C<use English> must be passed a C<-no_match_vars> argument.
154 This Policy is part of the core L<Perl::Critic> distribution.
159 Due to unfortunate history, if you use the L<English> module but don't
160 pass in a C<-no_match_vars> argument, all regular expressions in the
161 entire program, not merely the module in question, suffer a
162 significant performance penalty. See the L<English> documentation for
165 use English; # not ok
166 use English '-no_match_vars'; # ok
167 use English qw< $ERRNO -no_match_vars>; # ok
173 This Policy is not configurable except for the standard options.
178 Elliot Shank C<< <perl@galumph.com> >>
183 Copyright (c) 2008-2008 Elliot Shank. All rights reserved.
185 This program is free software; you can redistribute it and/or modify
186 it under the same terms as Perl itself. The full text of this license
187 can be found in the LICENSE file included with this module.
194 # cperl-indent-level: 4
196 # indent-tabs-mode: nil
197 # c-indentation-style: bsd
199 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :