File Coverage

blib/lib/Perl/Critic/Policy/RegularExpressions/RequireLineBoundaryMatching.pm
Criterion Covered Total %
statement 25 25 100.0
branch 3 4 75.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 43 45 95.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::RegularExpressions::RequireLineBoundaryMatching;
2              
3 40     40   27029 use 5.010001;
  40         207  
4 40     40   265 use strict;
  40         126  
  40         822  
5 40     40   237 use warnings;
  40         163  
  40         1003  
6 40     40   249 use Readonly;
  40         143  
  40         2127  
7              
8 40     40   337 use Perl::Critic::Utils qw{ :severities };
  40         146  
  40         2096  
9              
10 40     40   5430 use parent 'Perl::Critic::Policy';
  40         128  
  40         240  
11              
12             our $VERSION = '1.148';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q{Regular expression without "/m" flag};
17             Readonly::Scalar my $EXPL => [ 237 ];
18              
19             #-----------------------------------------------------------------------------
20              
21 96     96 0 1683 sub supported_parameters { return () }
22 93     93 1 406 sub default_severity { return $SEVERITY_LOW }
23 84     84 1 375 sub default_themes { return qw(core pbp cosmetic) }
24 37     37 1 128 sub applies_to { return qw(PPI::Token::Regexp::Match
25             PPI::Token::Regexp::Substitute
26             PPI::Token::QuoteLike::Regexp) }
27              
28             #-----------------------------------------------------------------------------
29              
30             sub violates {
31 38     38 1 82 my ( $self, $elem, $doc ) = @_;
32              
33 38 50       111 my $re = $doc->ppix_regexp_from_element( $elem )
34             or return;
35 38 100       167622 $re->modifier_asserted( 'm' )
36             or return $self->violation( $DESC, $EXPL, $elem );
37              
38 19         443 return; #ok!;
39             }
40              
41             1;
42              
43             __END__
44              
45             #-----------------------------------------------------------------------------
46              
47             =pod
48              
49             =head1 NAME
50              
51             Perl::Critic::Policy::RegularExpressions::RequireLineBoundaryMatching - Always use the C</m> modifier with regular expressions.
52              
53              
54             =head1 AFFILIATION
55              
56             This Policy is part of the core L<Perl::Critic|Perl::Critic>
57             distribution.
58              
59              
60             =head1 DESCRIPTION
61              
62             Folks coming from a C<sed> or C<awk> background tend to assume that
63             C<'$'> and C<'^'> match the beginning and end of the line, rather than
64             then beginning and end of the string. Adding the '/m' flag to your
65             regex makes it behave as most people expect it should.
66              
67             my $match = m{ ^ $pattern $ }x; #not ok
68             my $match = m{ ^ $pattern $ }xm; #ok
69              
70              
71             =head1 CONFIGURATION
72              
73             This Policy is not configurable except for the standard options.
74              
75              
76             =head1 NOTES
77              
78             For common regular expressions like e-mail addresses, phone numbers,
79             dates, etc., have a look at the L<Regexp::Common|Regexp::Common> module.
80             Also, be cautions about slapping modifier flags onto existing regular
81             expressions, as they can drastically alter their meaning. See
82             L<http://www.perlmonks.org/?node_id=484238> for an interesting
83             discussion on the effects of blindly modifying regular expression
84             flags.
85              
86              
87             =head1 AUTHOR
88              
89             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
90              
91              
92             =head1 COPYRIGHT
93              
94             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
95              
96             This program is free software; you can redistribute it and/or modify
97             it under the same terms as Perl itself. The full text of this license
98             can be found in the LICENSE file included with this module.
99              
100             =cut
101              
102             # Local Variables:
103             # mode: cperl
104             # cperl-indent-level: 4
105             # fill-column: 78
106             # indent-tabs-mode: nil
107             # c-indentation-style: bsd
108             # End:
109             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :