File Coverage

blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters.pm
Criterion Covered Total %
statement 39 39 100.0
branch 4 4 100.0
condition n/a
subroutine 14 14 100.0
pod 5 6 83.3
total 62 63 98.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::RegularExpressions::ProhibitUnusualDelimiters;
2              
3 40     40   27263 use 5.010001;
  40         215  
4 40     40   277 use strict;
  40         117  
  40         830  
5 40     40   246 use warnings;
  40         106  
  40         1065  
6 40     40   264 use Readonly;
  40         131  
  40         2294  
7              
8 40     40   357 use English qw(-no_match_vars);
  40         126  
  40         410  
9 40     40   16831 use Carp;
  40         157  
  40         2742  
10              
11 40     40   340 use Perl::Critic::Utils qw{ :booleans :severities hashify };
  40         114  
  40         2048  
12              
13 40     40   7206 use parent 'Perl::Critic::Policy';
  40         136  
  40         287  
14              
15             our $VERSION = '1.146';
16              
17             #-----------------------------------------------------------------------------
18              
19             Readonly::Scalar my $DESC => q<Use only '//' or '{}' to delimit regexps>;
20             Readonly::Scalar my $EXPL => [246];
21              
22             Readonly::Array my @EXTRA_BRACKETS => qw{ () [] <> };
23              
24             #-----------------------------------------------------------------------------
25              
26             sub supported_parameters {
27             return (
28             {
29 93     93 0 1957 name => 'allow_all_brackets',
30             description =>
31             q[In addition to allowing '{}', allow '()', '[]', and '{}'.],
32             behavior => 'boolean',
33             },
34             );
35             }
36              
37 99     99 1 407 sub default_severity { return $SEVERITY_LOWEST }
38 84     84 1 346 sub default_themes { return qw( core pbp cosmetic ) }
39 33     33 1 133 sub applies_to { return qw(PPI::Token::Regexp::Match
40             PPI::Token::Regexp::Substitute
41             PPI::Token::QuoteLike::Regexp) }
42              
43             #-----------------------------------------------------------------------------
44              
45             sub initialize_if_enabled {
46 50     50 1 193 my ( $self, $config ) = @_;
47              
48 50         252 my %delimiters = hashify( qw< // {} > );
49 50 100       253 if ( $self->{_allow_all_brackets} ) {
50 1         9 @delimiters{ @EXTRA_BRACKETS } = (1) x @EXTRA_BRACKETS;
51             }
52              
53 50         223 $self->{_allowed_delimiters} = \%delimiters;
54              
55 50         193 return $TRUE;
56             }
57              
58             #-----------------------------------------------------------------------------
59              
60             sub violates {
61 51     51 1 89 my ( $self, $elem, undef ) = @_;
62              
63 51         76 my $allowed_delimiters = $self->{_allowed_delimiters};
64 51         120 foreach my $delimiter ($elem->get_delimiters()) {
65 69 100       910 next if $allowed_delimiters->{$delimiter};
66 25         72 return $self->violation( $DESC, $EXPL, $elem );
67             }
68              
69 26         57 return; # OK
70             }
71              
72             1;
73              
74             __END__
75              
76             #-----------------------------------------------------------------------------
77              
78             =pod
79              
80             =head1 NAME
81              
82             Perl::Critic::Policy::RegularExpressions::ProhibitUnusualDelimiters - Use only C<//> or C<{}> to delimit regexps.
83              
84              
85             =head1 AFFILIATION
86              
87             This Policy is part of the core L<Perl::Critic|Perl::Critic>
88             distribution.
89              
90              
91             =head1 DESCRIPTION
92              
93             Perl lets you delimit regular expressions with almost any character,
94             but most choices are illegible. Compare these equivalent expressions:
95              
96             s/foo/bar/; # good
97             s{foo}{bar}; # good
98             s#foo#bar#; # bad
99             s;foo;bar;; # worse
100             s|\|\||\||; # eye-gouging bad
101              
102              
103             =head1 CONFIGURATION
104              
105             There is one option for this policy, C<allow_all_brackets>. If this
106             is true, then, in addition to allowing C<//> and C<{}>, the other
107             matched pairs of C<()>, C<[]>, and C<< <> >> are allowed.
108              
109              
110             =head1 CREDITS
111              
112             Initial development of this policy was supported by a grant from the
113             Perl Foundation.
114              
115              
116             =head1 AUTHOR
117              
118             Chris Dolan <cdolan@cpan.org>
119              
120              
121             =head1 COPYRIGHT
122              
123             Copyright (c) 2007-2011 Chris Dolan. Many rights reserved.
124              
125             This program is free software; you can redistribute it and/or modify
126             it under the same terms as Perl itself. The full text of this license
127             can be found in the LICENSE file included with this module
128              
129             =cut
130              
131             # Local Variables:
132             # mode: cperl
133             # cperl-indent-level: 4
134             # fill-column: 78
135             # indent-tabs-mode: nil
136             # c-indentation-style: bsd
137             # End:
138             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :