Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / RegularExpressions / RequireBracesForMultiline.pm
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) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use English qw(-no_match_vars);
16 use Carp;
17
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';
21
22 our $VERSION = '1.088';
23
24 #-----------------------------------------------------------------------------
25
26 Readonly::Scalar my $DESC => q<Use '{' and '}' to delimit multi-line regexps>;
27 Readonly::Scalar my $EXPL => [242];
28
29 Readonly::Array my @EXTRA_BRACKETS => qw{ () [] <> };
30
31 #-----------------------------------------------------------------------------
32
33 sub supported_parameters {
34     return (
35         {
36             name               => 'allow_all_brackets',
37             description        =>
38                 q[In addition to allowing '{}', allow '()', '[]', and '{}'.],
39             behavior           => 'boolean',
40         },
41     );
42 }
43
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) }
49
50 #-----------------------------------------------------------------------------
51
52 sub initialize_if_enabled {
53     my ( $self, $config ) = @_;
54
55     my %delimiters = ( q<{}> => 1 );
56     if ( $self->{_allow_all_brackets} ) {
57         @delimiters{ @EXTRA_BRACKETS } = (1) x @EXTRA_BRACKETS;
58     }
59
60     $self->{_allowed_delimiters} = \%delimiters;
61
62     return $TRUE;
63 }
64
65 #-----------------------------------------------------------------------------
66
67 sub violates {
68     my ( $self, $elem, undef ) = @_;
69
70     my $re = get_match_string($elem);
71     return if $re !~ m/\n/xms;
72
73     my ($match_delim) = get_delimiters($elem);
74     return if $self->{_allowed_delimiters}{$match_delim};
75
76     return $self->violation( $DESC, $EXPL, $elem );
77 }
78
79 1;
80
81 __END__
82
83 #-----------------------------------------------------------------------------
84
85 =pod
86
87 =head1 NAME
88
89 Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline - Use C<{> and C<}> to delimit multi-line regexps.
90
91
92 =head1 AFFILIATION
93
94 This Policy is part of the core L<Perl::Critic> distribution.
95
96
97 =head1 DESCRIPTION
98
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
104 expressions.
105
106 Compare these:
107
108     s/
109        <a \s+ href="([^"]+)">
110         (.*?)
111        </a>
112      /link=$1, text=$2/xms;
113
114 vs.
115
116     s{
117        <a \s+ href="([^"]+)">
118         (.*?)
119        </a>
120      }
121      {link=$1, text=$2}xms;
122
123 Is that an improvement?  Marginally, but yes.  The curly braces lead
124 the eye better.
125
126
127 =head1 CONFIGURATION
128
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.
132
133
134 =head1 CREDITS
135
136 Initial development of this policy was supported by a grant from the
137 Perl Foundation.
138
139
140 =head1 AUTHOR
141
142 Chris Dolan <cdolan@cpan.org>
143
144
145 =head1 COPYRIGHT
146
147 Copyright (c) 2007-2008 Chris Dolan.  Many rights reserved.
148
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
152
153 =cut
154
155 # Local Variables:
156 #   mode: cperl
157 #   cperl-indent-level: 4
158 #   fill-column: 78
159 #   indent-tabs-mode: nil
160 #   c-indentation-style: bsd
161 # End:
162 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :