File Coverage

blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitEscapedCharacters.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 46 47 97.8


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ValuesAndExpressions::ProhibitEscapedCharacters;
2              
3 40     40   31355 use 5.010001;
  40         167  
4 40     40   251 use strict;
  40         109  
  40         891  
5 40     40   221 use warnings;
  40         93  
  40         1021  
6 40     40   211 use Readonly;
  40         93  
  40         2000  
7              
8 40     40   247 use Perl::Critic::Utils qw{ :severities };
  40         92  
  40         2066  
9 40     40   4761 use parent 'Perl::Critic::Policy';
  40         111  
  40         224  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Numeric escapes in interpolated string};
16             Readonly::Scalar my $EXPL => [ 54..55 ];
17              
18             #-----------------------------------------------------------------------------
19              
20 91     91 0 1649 sub supported_parameters { return () }
21 77     77 1 351 sub default_severity { return $SEVERITY_LOW }
22 84     84 1 412 sub default_themes { return qw(core pbp cosmetic) }
23 32     32 1 116 sub applies_to { return qw(PPI::Token::Quote::Double
24             PPI::Token::Quote::Interpolate) }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 7     7 1 18 my ( $self, $elem, undef ) = @_;
30              
31 7         26 my $not_escaped = qr/(?<!\\)(?:\\\\)*/xms;
32 7         25 my $hex = qr/\\x[\dA-Fa-f]{2}/xms;
33 7         17 my $widehex = qr/\\x[{][\dA-Fa-f]+[}]/xms;
34 7         20 my $oct = qr/\\[01][0-7]/xms;
35 7 100       27 if ($elem->content =~ m/$not_escaped (?:$hex|$widehex|$oct)/xmso) {
36 3         369 return $self->violation( $DESC, $EXPL, $elem );
37             }
38 4         216 return; #ok!
39             }
40              
41             1;
42              
43             __END__
44              
45             #-----------------------------------------------------------------------------
46              
47             =pod
48              
49             =head1 NAME
50              
51             Perl::Critic::Policy::ValuesAndExpressions::ProhibitEscapedCharacters - Write C<"\N{DELETE}"> instead of C<"\x7F">, etc.
52              
53             =head1 AFFILIATION
54              
55             This Policy is part of the core L<Perl::Critic|Perl::Critic>
56             distribution.
57              
58              
59             =head1 DESCRIPTION
60              
61             Escaped numeric values are hard to read and debug. Instead, use named
62             values. The syntax is less compact, but dramatically more readable.
63              
64             $str = "\x7F\x06\x22Z"; # not ok
65              
66             use charnames ':full';
67             $str = "\N{DELETE}\N{ACKNOWLEDGE}\N{CANCEL}Z"; # ok
68              
69              
70             =head1 CONFIGURATION
71              
72             This Policy is not configurable except for the standard options.
73              
74              
75             =head1 AUTHOR
76              
77             Chris Dolan <cdolan@cpan.org>
78              
79             =head1 COPYRIGHT
80              
81             Copyright (c) 2006-2011 Chris Dolan.
82              
83             This program is free software; you can redistribute it and/or modify
84             it under the same terms as Perl itself. The full text of this license
85             can be found in the LICENSE file included with this module.
86              
87             =cut
88              
89             # Local Variables:
90             # mode: cperl
91             # cperl-indent-level: 4
92             # fill-column: 78
93             # indent-tabs-mode: nil
94             # c-indentation-style: bsd
95             # End:
96             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :