| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Modules::RequireExplicitPackage; |
|
2
|
|
|
|
|
|
|
|
|
3
|
40
|
|
|
40
|
|
26072
|
use 5.010001; |
|
|
40
|
|
|
|
|
165
|
|
|
4
|
40
|
|
|
40
|
|
242
|
use strict; |
|
|
40
|
|
|
|
|
100
|
|
|
|
40
|
|
|
|
|
830
|
|
|
5
|
40
|
|
|
40
|
|
222
|
use warnings; |
|
|
40
|
|
|
|
|
121
|
|
|
|
40
|
|
|
|
|
1005
|
|
|
6
|
40
|
|
|
40
|
|
236
|
use Readonly; |
|
|
40
|
|
|
|
|
85
|
|
|
|
40
|
|
|
|
|
2231
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
40
|
|
|
40
|
|
269
|
use Perl::Critic::Utils qw{ :booleans :severities :classification }; |
|
|
40
|
|
|
|
|
101
|
|
|
|
40
|
|
|
|
|
2178
|
|
|
9
|
40
|
|
|
40
|
|
15605
|
use parent 'Perl::Critic::Policy'; |
|
|
40
|
|
|
|
|
107
|
|
|
|
40
|
|
|
|
|
247
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '1.150'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Readonly::Scalar my $EXPL => q{Violates encapsulation}; |
|
16
|
|
|
|
|
|
|
Readonly::Scalar my $DESC => q{Code not contained in explicit package}; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub supported_parameters { |
|
21
|
|
|
|
|
|
|
return ( |
|
22
|
|
|
|
|
|
|
{ |
|
23
|
91
|
|
|
91
|
0
|
2064
|
name => 'exempt_scripts', |
|
24
|
|
|
|
|
|
|
description => q{Don't require programs to contain a package statement.}, |
|
25
|
|
|
|
|
|
|
default_string => '1', |
|
26
|
|
|
|
|
|
|
behavior => 'boolean', |
|
27
|
|
|
|
|
|
|
}, |
|
28
|
|
|
|
|
|
|
{ |
|
29
|
|
|
|
|
|
|
name => 'allow_import_of', |
|
30
|
|
|
|
|
|
|
description => q{Allow the specified modules to be imported outside a package}, |
|
31
|
|
|
|
|
|
|
behavior => 'string list', |
|
32
|
|
|
|
|
|
|
}, |
|
33
|
|
|
|
|
|
|
); |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
77
|
|
|
77
|
1
|
296
|
sub default_severity { return $SEVERITY_HIGH } |
|
37
|
74
|
|
|
74
|
1
|
307
|
sub default_themes { return qw( core bugs ) } |
|
38
|
31
|
|
|
31
|
1
|
100
|
sub applies_to { return 'PPI::Document' } |
|
39
|
|
|
|
|
|
|
|
|
40
|
35
|
|
|
35
|
1
|
97
|
sub default_maximum_violations_per_document { return 1; } |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub prepare_to_scan_document { |
|
45
|
32
|
|
|
32
|
1
|
96
|
my ( $self, $document ) = @_; |
|
46
|
|
|
|
|
|
|
|
|
47
|
32
|
|
66
|
|
|
178
|
return ! $self->{_exempt_scripts} || $document->is_module(); |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub violates { |
|
51
|
31
|
|
|
31
|
1
|
99
|
my ( $self, $elem, $doc ) = @_; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Find the first 'package' statement |
|
54
|
31
|
|
|
|
|
160
|
my $package_stmnt = $doc->find_first( 'PPI::Statement::Package' ); |
|
55
|
31
|
100
|
|
|
|
200
|
my $package_line = $package_stmnt ? $package_stmnt->location()->[0] : undef; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Find all statements that aren't 'package' statements |
|
58
|
31
|
|
|
|
|
631
|
my $stmnts_ref = $doc->find( 'PPI::Statement' ); |
|
59
|
31
|
100
|
|
|
|
99
|
return if !$stmnts_ref; |
|
60
|
|
|
|
|
|
|
my @non_packages = grep { |
|
61
|
289
|
|
|
|
|
475
|
$self->_is_statement_of_interest( $_ ) |
|
62
|
29
|
|
|
|
|
64
|
} @{$stmnts_ref}; |
|
|
29
|
|
|
|
|
59
|
|
|
63
|
29
|
50
|
|
|
|
120
|
return if !@non_packages; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# If the 'package' statement is not defined, or the other |
|
66
|
|
|
|
|
|
|
# statements appear before the 'package', then it violates. |
|
67
|
|
|
|
|
|
|
|
|
68
|
29
|
|
|
|
|
93
|
my @viols; |
|
69
|
29
|
|
|
|
|
72
|
for my $stmnt ( @non_packages ) { |
|
70
|
260
|
|
|
|
|
546
|
my $stmnt_line = $stmnt->location()->[0]; |
|
71
|
260
|
100
|
66
|
|
|
3786
|
if ( (! defined $package_line) || ($stmnt_line < $package_line) ) { |
|
72
|
3
|
|
|
|
|
76
|
push @viols, $self->violation( $DESC, $EXPL, $stmnt ); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
29
|
|
|
|
|
138
|
return @viols; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _is_statement_of_interest { |
|
80
|
289
|
|
|
289
|
|
427
|
my ( $self, $elem ) = @_; |
|
81
|
|
|
|
|
|
|
|
|
82
|
289
|
50
|
|
|
|
608
|
$elem |
|
83
|
|
|
|
|
|
|
or return $FALSE; |
|
84
|
|
|
|
|
|
|
|
|
85
|
289
|
100
|
|
|
|
743
|
$elem->isa( 'PPI::Statement::Package' ) |
|
86
|
|
|
|
|
|
|
and return $FALSE; |
|
87
|
|
|
|
|
|
|
|
|
88
|
260
|
100
|
|
|
|
628
|
if ( $elem->isa( 'PPI::Statement::Include' ) ) { |
|
89
|
59
|
100
|
|
|
|
171
|
if ( my $module = $elem->module() ) { |
|
90
|
57
|
50
|
|
|
|
1339
|
$self->{_allow_import_of}{$module} |
|
91
|
|
|
|
|
|
|
and return $FALSE; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
260
|
|
|
|
|
554
|
return $TRUE; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=pod |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 NAME |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Perl::Critic::Policy::Modules::RequireExplicitPackage - Always make the C<package> explicit. |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 AFFILIATION |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This Policy is part of the core L<Perl::Critic|Perl::Critic> |
|
114
|
|
|
|
|
|
|
distribution. |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
In general, the first statement of any Perl module or library should |
|
120
|
|
|
|
|
|
|
be a C<package> statement. Otherwise, all the code that comes before |
|
121
|
|
|
|
|
|
|
the C<package> statement is getting executed in the caller's package, |
|
122
|
|
|
|
|
|
|
and you have no idea who that is. Good encapsulation and common |
|
123
|
|
|
|
|
|
|
decency require your module to keep its innards to itself. |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
There are some valid reasons for not having a C<package> statement at |
|
126
|
|
|
|
|
|
|
all. But make sure you understand them before assuming that you |
|
127
|
|
|
|
|
|
|
should do it too. |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
The maximum number of violations per document for this policy defaults |
|
130
|
|
|
|
|
|
|
to 1. |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 CONFIGURATION |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
As for programs, most people understand that the default package is |
|
137
|
|
|
|
|
|
|
C<main>, so this Policy doesn't apply to files that begin with a perl |
|
138
|
|
|
|
|
|
|
shebang. If you want to require an explicit C<package> declaration in |
|
139
|
|
|
|
|
|
|
all files, including programs, then add the following to your |
|
140
|
|
|
|
|
|
|
F<.perlcriticrc> file |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
[Modules::RequireExplicitPackage] |
|
143
|
|
|
|
|
|
|
exempt_scripts = 0 |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Some users may find it desirable to exempt the load of specific modules |
|
146
|
|
|
|
|
|
|
from this policy. For example, Perl does not support Unicode module |
|
147
|
|
|
|
|
|
|
names because of portability problems. Users who are not concerned about |
|
148
|
|
|
|
|
|
|
this and intend to use C<UTF-8> module names will need to specify |
|
149
|
|
|
|
|
|
|
C<use utf8;> before the package declaration. To do this, add the |
|
150
|
|
|
|
|
|
|
following to your F<.perlcriticrc> file |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
[Modules::RequireExplicitPackage] |
|
153
|
|
|
|
|
|
|
allow_import_of = utf8 |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
The C<allow_import_of> configuration option takes multiple module names, |
|
156
|
|
|
|
|
|
|
separated by spaces. |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 IMPORTANT CHANGES |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This policy was formerly called C<ProhibitUnpackagedCode> which |
|
162
|
|
|
|
|
|
|
sounded a bit odd. If you get lots of "Cannot load policy module" |
|
163
|
|
|
|
|
|
|
errors, then you probably need to change C<ProhibitUnpackagedCode> to |
|
164
|
|
|
|
|
|
|
C<RequireExplicitPackage> in your F<.perlcriticrc> file. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 AUTHOR |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved. |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
|
177
|
|
|
|
|
|
|
it under the same terms as Perl itself. The full text of this license |
|
178
|
|
|
|
|
|
|
can be found in the LICENSE file included with this module. |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# Local Variables: |
|
183
|
|
|
|
|
|
|
# mode: cperl |
|
184
|
|
|
|
|
|
|
# cperl-indent-level: 4 |
|
185
|
|
|
|
|
|
|
# fill-column: 78 |
|
186
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
|
187
|
|
|
|
|
|
|
# c-indentation-style: bsd |
|
188
|
|
|
|
|
|
|
# End: |
|
189
|
|
|
|
|
|
|
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |