1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/RegularExpressions/ProhibitSingleCharAlternation.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::RegularExpressions::ProhibitSingleCharAlternation;
14 use English qw(-no_match_vars);
18 use Perl::Critic::Utils qw{ :booleans :characters :severities };
19 use Perl::Critic::Utils::PPIRegexp qw{ ppiify parse_regexp };
20 use base 'Perl::Critic::Policy';
22 our $VERSION = '1.088';
24 #-----------------------------------------------------------------------------
26 Readonly::Scalar my $DESC => q{Use [abc] instead of a|b|c};
27 Readonly::Scalar my $EXPL => [265];
29 #-----------------------------------------------------------------------------
31 sub supported_parameters { return qw() }
32 sub default_severity { return $SEVERITY_LOWEST }
33 sub default_themes { return qw( core pbp performance ) }
34 sub applies_to { return qw(PPI::Token::Regexp::Match
35 PPI::Token::Regexp::Substitute
36 PPI::Token::QuoteLike::Regexp) }
38 #-----------------------------------------------------------------------------
41 my ( $self, $elem, undef ) = @_;
43 # optimization: don't bother parsing the regexp if there are no pipes
44 return if $elem !~ m/[|]/xms;
46 my $re = ppiify(parse_regexp($elem));
49 # Must pass a sub to find() because our node classes don't start with PPI::
51 $re->find(sub {$_[1]->isa('Perl::Critic::PPIRegexp::branch')});
52 return if not $branches;
53 for my $branch (@{$branches}) {
57 $_->isa('Perl::Critic::PPIRegexp::exact')
64 . join( $EMPTY, @singles )
66 . join q<|>, @singles;
67 return $self->violation( $description, $EXPL, $elem );
78 #-----------------------------------------------------------------------------
84 Perl::Critic::Policy::RegularExpressions::ProhibitSingleCharAlternation - Use C<[abc]> instead of C<a|b|c>.
88 This Policy is part of the core L<Perl::Critic> distribution.
93 Character classes (like C<[abc]>) are significantly faster than single
94 character alternations (like C<(?:a|b|c)>). This policy complains if you have
95 more than one instance of a single character in an alternation. So
96 C<(?:a|the)> is allowed, but C<(?:a|e|i|o|u)> is not.
98 NOTE: Perl 5.10 (not released as of this writing) has major regexp
99 optimizations which may mitigate the performance penalty of
100 alternations, which will be rewritten behind the scenes as something
101 like character classes.
106 This Policy is not configurable except for the standard options.
111 Initial development of this policy was supported by a grant from the Perl Foundation.
115 Chris Dolan <cdolan@cpan.org>
119 Copyright (c) 2007-2008 Chris Dolan. Many rights reserved.
121 This program is free software; you can redistribute it and/or modify
122 it under the same terms as Perl itself. The full text of this license
123 can be found in the LICENSE file included with this module
129 # cperl-indent-level: 4
131 # indent-tabs-mode: nil
132 # c-indentation-style: bsd
134 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :