1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::RegularExpressions::ProhibitUnusualDelimiters;
15 use English qw(-no_match_vars);
18 use Perl::Critic::Utils qw{ :booleans :severities hashify };
19 use Perl::Critic::Utils::PPIRegexp qw{ get_delimiters };
20 use base 'Perl::Critic::Policy';
22 our $VERSION = '1.088';
24 #-----------------------------------------------------------------------------
26 Readonly::Scalar my $DESC => q<Use only '//' or '{}' to delimit regexps>;
27 Readonly::Scalar my $EXPL => [246];
29 Readonly::Array my @EXTRA_BRACKETS => qw{ () [] <> };
31 #-----------------------------------------------------------------------------
33 sub supported_parameters {
36 name => 'allow_all_brackets',
38 q[In addition to allowing '{}', allow '()', '[]', and '{}'.],
39 behavior => 'boolean',
44 sub default_severity { return $SEVERITY_LOWEST }
45 sub default_themes { return qw( core pbp cosmetic ) }
46 sub applies_to { return qw(PPI::Token::Regexp::Match
47 PPI::Token::Regexp::Substitute
48 PPI::Token::QuoteLike::Regexp) }
50 #-----------------------------------------------------------------------------
52 sub initialize_if_enabled {
53 my ( $self, $config ) = @_;
55 my %delimiters = hashify( qw< // {} > );
56 if ( $self->{_allow_all_brackets} ) {
57 @delimiters{ @EXTRA_BRACKETS } = (1) x @EXTRA_BRACKETS;
60 $self->{_allowed_delimiters} = \%delimiters;
65 #-----------------------------------------------------------------------------
68 my ( $self, $elem, undef ) = @_;
70 my $allowed_delimiters = $self->{_allowed_delimiters};
71 foreach my $delimiter (get_delimiters($elem)) {
72 next if $allowed_delimiters->{$delimiter};
73 return $self->violation( $DESC, $EXPL, $elem );
83 #-----------------------------------------------------------------------------
89 Perl::Critic::Policy::RegularExpressions::ProhibitUnusualDelimiters - Use only C<//> or C<{}> to delimit regexps.
94 This Policy is part of the core L<Perl::Critic> distribution.
99 Perl lets you delimit regular expressions with almost any character,
100 but most choices are illegible. Compare these equivalent expressions:
106 s|\|\||\||; # eye-gouging bad
111 There is one option for this policy, C<allow_all_brackets>. If this
112 is true, then, in addition to allowing C<//> and C<{}>, the other
113 matched pairs of C<()>, C<[]>, and C<< <> >> are allowed.
118 Initial development of this policy was supported by a grant from the
124 Chris Dolan <cdolan@cpan.org>
129 Copyright (c) 2007-2008 Chris Dolan. Many rights reserved.
131 This program is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself. The full text of this license
133 can be found in the LICENSE file included with this module
139 # cperl-indent-level: 4
141 # indent-tabs-mode: nil
142 # c-indentation-style: bsd
144 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :