1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/RegularExpressions/RequireBracesForMultiline.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline;
15 use English qw(-no_match_vars);
18 use Perl::Critic::Utils qw{ :booleans :severities };
19 use Perl::Critic::Utils::PPIRegexp qw{ get_match_string get_delimiters };
20 use base 'Perl::Critic::Policy';
22 our $VERSION = '1.088';
24 #-----------------------------------------------------------------------------
26 Readonly::Scalar my $DESC => q<Use '{' and '}' to delimit multi-line regexps>;
27 Readonly::Scalar my $EXPL => [242];
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 = ( q<{}> => 1 );
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 $re = get_match_string($elem);
71 return if $re !~ m/\n/xms;
73 my ($match_delim) = get_delimiters($elem);
74 return if $self->{_allowed_delimiters}{$match_delim};
76 return $self->violation( $DESC, $EXPL, $elem );
83 #-----------------------------------------------------------------------------
89 Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline - Use C<{> and C<}> to delimit multi-line regexps.
94 This Policy is part of the core L<Perl::Critic> distribution.
99 Long regular expressions are hard to read. A good practice is to use
100 the C<x> modifier and break the regex into multiple lines with
101 comments explaining the parts. But, with the usual C<//> delimiters,
102 the beginning and end can be hard to match, especially in a C<s///>
103 regexp. Instead, try using C<{}> characters to delimit your
109 <a \s+ href="([^"]+)">
112 /link=$1, text=$2/xms;
117 <a \s+ href="([^"]+)">
121 {link=$1, text=$2}xms;
123 Is that an improvement? Marginally, but yes. The curly braces lead
129 There is one option for this policy, C<allow_all_brackets>. If this
130 is true, then, in addition to allowing C<{}>, the other matched pairs
131 of C<()>, C<[]>, and C<< <> >> are allowed.
136 Initial development of this policy was supported by a grant from the
142 Chris Dolan <cdolan@cpan.org>
147 Copyright (c) 2007-2008 Chris Dolan. Many rights reserved.
149 This program is free software; you can redistribute it and/or modify
150 it under the same terms as Perl itself. The full text of this license
151 can be found in the LICENSE file included with this module
157 # cperl-indent-level: 4
159 # indent-tabs-mode: nil
160 # c-indentation-style: bsd
162 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :