File Coverage

blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitSpecialLiteralHeredocTerminator.pm
Criterion Covered Total %
statement 21 27 77.7
branch 0 2 0.0
condition n/a
subroutine 10 11 90.9
pod 4 5 80.0
total 35 45 77.7


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ValuesAndExpressions::ProhibitSpecialLiteralHeredocTerminator;
2              
3 40     40   27100 use 5.010001;
  40         767  
4 40     40   243 use strict;
  40         96  
  40         1290  
5 40     40   233 use warnings;
  40         96  
  40         1061  
6              
7 40     40   261 use Readonly;
  40         91  
  40         2021  
8              
9 40     40   276 use Perl::Critic::Utils qw{ :severities };
  40         129  
  40         1996  
10 40     40   5233 use parent 'Perl::Critic::Policy';
  40         97  
  40         260  
11              
12             our $VERSION = '1.150';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Hash my %SPECIAL_LITERAL => map { '__' . $_ . '__' => 1 }
17             qw( FILE LINE PACKAGE END DATA );
18             Readonly::Scalar my $DESC =>
19             q{Heredoc terminator must not be a special literal};
20              
21             #-----------------------------------------------------------------------------
22              
23 89     89 0 1797 sub supported_parameters { return () }
24 74     74 1 439 sub default_severity { return $SEVERITY_MEDIUM }
25 74     74 1 305 sub default_themes { return qw(core maintenance) }
26 30     30 1 93 sub applies_to { return 'PPI::Token::HereDoc' }
27              
28             #-----------------------------------------------------------------------------
29              
30             sub violates {
31 0     0 1   my ( $self, $elem, undef ) = @_;
32              
33             # remove << and (optional) quotes from around terminator
34 0           ( my $heredoc_terminator = $elem ) =~
35             s{ \A << ~? \s* (["']?) (.*) \1 \z }{$2}xms;
36              
37 0 0         if ( $SPECIAL_LITERAL{ $heredoc_terminator } ) {
38 0           my $expl = qq{Used "$heredoc_terminator" as heredoc terminator};
39 0           return $self->violation( $DESC, $expl, $elem );
40             }
41              
42 0           return; #ok!
43             }
44              
45             1;
46              
47             __END__
48              
49             #-----------------------------------------------------------------------------
50              
51             =pod
52              
53             =head1 NAME
54              
55             Perl::Critic::Policy::ValuesAndExpressions::ProhibitSpecialLiteralHeredocTerminator - Don't write C< print <<'__END__' >.
56              
57              
58             =head1 AFFILIATION
59              
60             This Policy is part of the core L<Perl::Critic|Perl::Critic>
61             distribution.
62              
63              
64             =head1 DESCRIPTION
65              
66             Using one of Perl's special literals as a HEREDOC terminator could be
67             confusing to tools that try to parse perl.
68              
69             print <<'__END__'; #not ok
70             Hello world
71             __END__
72              
73             print <<'__END_OF_WORLD__'; #ok
74             Goodbye world!
75             __END_OF_WORLD__
76              
77             The special literals that this policy prohibits are:
78              
79             =over
80              
81             =item __END__
82              
83             =item __DATA__
84              
85             =item __PACKAGE__
86              
87             =item __FILE__
88              
89             =item __LINE__
90              
91             =back
92              
93              
94             =head1 CONFIGURATION
95              
96             This Policy is not configurable except for the standard options.
97              
98              
99             =head1 SEE ALSO
100              
101             L<Perl::Critic::Policy::ValuesAndExpressions::RequireUpperCaseHeredocTerminator|Perl::Critic::Policy::ValuesAndExpressions::RequireUpperCaseHeredocTerminator>
102              
103             L<Perl::Critic::Policy::ValuesAndExpressions::RequireQuotedHeredocTerminator|Perl::Critic::Policy::ValuesAndExpressions::RequireQuotedHeredocTerminator>
104              
105              
106             =head1 AUTHOR
107              
108             Kyle Hasselbacher <kyle@cpan.org>
109              
110              
111             =head1 COPYRIGHT
112              
113             Copyright (c) 2009-2011 Kyle Hasselbacher.
114              
115             This program is free software; you can redistribute it and/or modify
116             it under the same terms as Perl itself. The full text of this license
117             can be found in the LICENSE file included with this module.
118              
119             =cut
120              
121             # Local Variables:
122             # mode: cperl
123             # cperl-indent-level: 4
124             # fill-column: 78
125             # indent-tabs-mode: nil
126             # c-indentation-style: bsd
127             # End:
128             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :