File Coverage

blib/lib/Perl/Critic/Policy/RegularExpressions/RequireBracesForMultiline.pm
Criterion Covered Total %
statement 26 33 78.7
branch 1 6 16.6
condition n/a
subroutine 11 12 91.6
pod 5 6 83.3
total 43 57 75.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline;
2              
3 40     40   26145 use 5.010001;
  40         188  
4 40     40   270 use strict;
  40         149  
  40         891  
5 40     40   273 use warnings;
  40         152  
  40         935  
6 40     40   240 use Readonly;
  40         105  
  40         2057  
7              
8 40     40   304 use Perl::Critic::Utils qw{ :booleans :severities };
  40         146  
  40         2011  
9              
10 40     40   6343 use parent 'Perl::Critic::Policy';
  40         164  
  40         269  
11              
12             our $VERSION = '1.150';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q<Use '{' and '}' to delimit multi-line regexps>;
17             Readonly::Scalar my $EXPL => [242];
18              
19             Readonly::Array my @EXTRA_BRACKETS => qw{ () [] <> };
20              
21             #-----------------------------------------------------------------------------
22              
23             sub supported_parameters {
24             return (
25             {
26 90     90 0 1910 name => 'allow_all_brackets',
27             description =>
28             q[In addition to allowing '{}', allow '()', '[]', and '{}'.],
29             behavior => 'boolean',
30             },
31             );
32             }
33              
34 74     74 1 313 sub default_severity { return $SEVERITY_LOWEST }
35 84     84 1 389 sub default_themes { return qw( core pbp cosmetic ) }
36 30     30 1 113 sub applies_to { return qw(PPI::Token::Regexp::Match
37             PPI::Token::Regexp::Substitute
38             PPI::Token::QuoteLike::Regexp) }
39              
40             #-----------------------------------------------------------------------------
41              
42             sub initialize_if_enabled {
43 47     47 1 198 my ( $self, $config ) = @_;
44              
45 47         195 my %delimiters = ( q<{}> => 1 );
46 47 50       201 if ( $self->{_allow_all_brackets} ) {
47 0         0 @delimiters{ @EXTRA_BRACKETS } = (1) x @EXTRA_BRACKETS;
48             }
49              
50 47         143 $self->{_allowed_delimiters} = \%delimiters;
51              
52 47         189 return $TRUE;
53             }
54              
55             #-----------------------------------------------------------------------------
56              
57             sub violates {
58 0     0 1   my ( $self, $elem, undef ) = @_;
59              
60 0           my $re = $elem->get_match_string();
61 0 0         return if $re !~ m/\n/xms;
62              
63 0           my ($match_delim) = $elem->get_delimiters();
64 0 0         return if $self->{_allowed_delimiters}{$match_delim};
65              
66 0           return $self->violation( $DESC, $EXPL, $elem );
67             }
68              
69             1;
70              
71             __END__
72              
73             #-----------------------------------------------------------------------------
74              
75             =pod
76              
77             =head1 NAME
78              
79             Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline - Use C<{> and C<}> to delimit multi-line regexps.
80              
81              
82             =head1 AFFILIATION
83              
84             This Policy is part of the core L<Perl::Critic|Perl::Critic>
85             distribution.
86              
87              
88             =head1 DESCRIPTION
89              
90             Long regular expressions are hard to read. A good practice is to use
91             the C<x> modifier and break the regex into multiple lines with
92             comments explaining the parts. But, with the usual C<//> delimiters,
93             the beginning and end can be hard to match, especially in a C<s///>
94             regexp. Instead, try using C<{}> characters to delimit your
95             expressions.
96              
97             Compare these:
98              
99             s/
100             <a \s+ href="([^"]+)">
101             (.*?)
102             </a>
103             /link=$1, text=$2/xms;
104              
105             vs.
106              
107             s{
108             <a \s+ href="([^"]+)">
109             (.*?)
110             </a>
111             }
112             {link=$1, text=$2}xms;
113              
114             Is that an improvement? Marginally, but yes. The curly braces lead
115             the eye better.
116              
117              
118             =head1 CONFIGURATION
119              
120             There is one option for this policy, C<allow_all_brackets>. If this
121             is true, then, in addition to allowing C<{}>, the other matched pairs
122             of C<()>, C<[]>, and C<< <> >> are allowed.
123              
124              
125             =head1 CREDITS
126              
127             Initial development of this policy was supported by a grant from the
128             Perl Foundation.
129              
130              
131             =head1 AUTHOR
132              
133             Chris Dolan <cdolan@cpan.org>
134              
135              
136             =head1 COPYRIGHT
137              
138             Copyright (c) 2007-2023 Chris Dolan
139              
140             This program is free software; you can redistribute it and/or modify
141             it under the same terms as Perl itself. The full text of this license
142             can be found in the LICENSE file included with this module
143              
144             =cut
145              
146             # Local Variables:
147             # mode: cperl
148             # cperl-indent-level: 4
149             # fill-column: 78
150             # indent-tabs-mode: nil
151             # c-indentation-style: bsd
152             # End:
153             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :