File Coverage

blib/lib/Perl/Critic/Policy/Documentation/RequireEndBeforeLastPod.pm
Criterion Covered Total %
statement 42 42 100.0
branch 8 8 100.0
condition 6 7 85.7
subroutine 11 11 100.0
pod 1 1 100.0
total 68 69 98.5


line stmt bran cond sub pod time code
1             # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Rydepod
2              
3             # This file is part of Perl-Critic-Pulp.
4              
5             # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
17              
18              
19             package Perl::Critic::Policy::Documentation::RequireEndBeforeLastPod;
20 40     40   33184 use 5.006;
  40         166  
21 40     40   230 use strict;
  40         98  
  40         822  
22 40     40   208 use warnings;
  40         92  
  40         1122  
23 40     40   243 use base 'Perl::Critic::Policy';
  40         98  
  40         4627  
24 40     40   191853 use Perl::Critic::Utils;
  40         104  
  40         745  
25              
26             our $VERSION = 99;
27              
28 40     40   36948 use constant supported_parameters => ();
  40         108  
  40         2706  
29 40     40   274 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
  40         110  
  40         2472  
30 40     40   277 use constant default_themes => qw(pulp cosmetic);
  40         95  
  40         2476  
31 40     40   284 use constant applies_to => 'PPI::Document';
  40         146  
  40         2207  
32              
33             # only ever gives one violation
34 40     40   275 use constant default_maximum_violations_per_document => 1;
  40         111  
  40         8917  
35              
36             sub violates {
37 14     14 1 551866 my ($self, $elem, $document) = @_;
38              
39 14   50     81 $elem = $elem->last_element
40             || return; # empty file
41              
42 14 100 100     370 if ($elem->isa('PPI::Statement::End')
43             || $elem->isa('PPI::Statement::Data')) {
44             # $document ends with __END__, ok
45             # or ends with __DATA__, in which case you can't use __END__ after last
46             # code, so ok
47 5         18 return;
48             }
49              
50 9         19 for (;;) {
51 17 100       225 if ($elem->significant) {
52             # document ends with code, ie. no pod after the last code, so ok
53 2         7 return;
54             }
55 15 100       50 if ($elem->isa('PPI::Token::Pod')) {
56             # found the last pod
57 6         15 last;
58             }
59             # otherwise skip PPI::Token::Comment and possibly PPI::Token::Whitespace
60 9   100     34 $elem = $elem->previous_sibling
61             || return; # $document is empty, or only comments and whitespace, so ok
62             }
63              
64 6 100       24 if (! $elem->sprevious_sibling) {
65             # there's no code before the last pod, either a pod-only file, or pod
66             # plus comments etc, so ok
67 2         57 return;
68             }
69              
70 4         141 return $self->violation
71             ('Put __END__ before POD at the end of a file.',
72             '',
73             $elem);
74             }
75              
76             1;
77             __END__
78              
79             =for stopwords ok SelfLoader Ryde
80              
81             =head1 NAME
82              
83             Perl::Critic::Policy::Documentation::RequireEndBeforeLastPod - require __END__ before POD at end of file
84              
85             =head1 DESCRIPTION
86              
87             This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
88             add-on. It requires that you put an C<__END__> before POD which is at the
89             end of a file. For example,
90              
91             program_code();
92             1;
93             __END__ # good
94              
95             =head1 NAME
96             ...
97              
98             and not merely
99              
100             program_code();
101             1; # bad
102              
103             =head1 NAME
104             ...
105              
106             This is primarily a matter of personal preference, so the policy is low
107             severity and only under the "cosmetic" theme (see L<Perl::Critic/POLICY
108             THEMES>). An C<__END__> like this has no effect on execution, but it's a
109             fairly common convention since it's a good human indication you mean the
110             code to end there, and it stops Perl parsing through the POD which may save
111             a few nanoseconds.
112              
113             This policy is looser than C<Documentation::RequirePodAtEnd>. This policy
114             allows POD to be anywhere in among the code, the requirement is only that if
115             the file ends with POD then you should have an C<__END__> between the last
116             code and last POD.
117              
118             A file of all POD, or all code, or which ends with code, is ok. Ending with
119             code is usual if you write your POD at the start of the file or in among the
120             functions etc,
121              
122             =pod
123              
124             And that's all.
125              
126             =cut
127              
128             cleanup ();
129             exit 0; # good
130              
131             A file using C<__DATA__> is always ok, since you can't have C<__END__>
132             followed by C<__DATA__>, wherever you want your POD. If the C<__DATA__> is
133             in fact C<SelfLoader> code then it can helpfully have an C<__END__> within
134             it, but as of C<perlcritic> version 1.092 no checks at all are applied to
135             SelfLoader sections.
136              
137             =head2 Disabling
138              
139             As always if you don't care about C<__END__> you can disable
140             C<RequireEndBeforeLastPod> from your F<.perlcriticrc> in the usual way (see
141             L<Perl::Critic/CONFIGURATION>),
142              
143             [-Documentation::RequireEndBeforeLastPod]
144              
145             =head1 SEE ALSO
146              
147             L<Perl::Critic::Pulp>,
148             L<Perl::Critic>,
149             L<Perl::Critic::Policy::Documentation::RequirePodAtEnd>
150              
151             =head1 HOME PAGE
152              
153             L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
154              
155             =head1 COPYRIGHT
156              
157             Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
158              
159             Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
160             under the terms of the GNU General Public License as published by the Free
161             Software Foundation; either version 3, or (at your option) any later
162             version.
163              
164             Perl-Critic-Pulp is distributed in the hope that it will be useful, but
165             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
166             or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
167             more details.
168              
169             You should have received a copy of the GNU General Public License along with
170             Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
171              
172             =cut