File: RequireExplicitPackage.pm

package info (click to toggle)
libperl-critic-perl 1.156-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,544 kB
  • sloc: perl: 24,092; lisp: 341; makefile: 7
file content (197 lines) | stat: -rw-r--r-- 5,790 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package Perl::Critic::Policy::Modules::RequireExplicitPackage;

use 5.010001;
use strict;
use warnings;
use Readonly;

use Perl::Critic::Utils qw{ :booleans :severities :classification };
use parent 'Perl::Critic::Policy';

our $VERSION = '1.156';

#-----------------------------------------------------------------------------

Readonly::Scalar my $EXPL => q{Violates encapsulation};
Readonly::Scalar my $DESC => q{Code not contained in explicit package};

#-----------------------------------------------------------------------------

sub supported_parameters {
    return (
        {
            name           => 'exempt_scripts',
            description    => q{Don't require programs to contain a package statement.},
            default_string => '1',
            behavior       => 'boolean',
        },
        {
            name           => 'allow_import_of',
            description    => q{Allow the specified modules to be imported outside a package},
            behavior       => 'string list',
        },
    );
}

sub default_severity { return $SEVERITY_HIGH  }
sub default_themes   { return qw( core bugs ) }
sub applies_to       { return 'PPI::Document' }

sub default_maximum_violations_per_document { return 1; }

#-----------------------------------------------------------------------------

sub prepare_to_scan_document {
    my ( $self, $document ) = @_;

    return ! $self->{_exempt_scripts} || $document->is_module();
}

sub violates {
    my ( $self, undef, $doc ) = @_;

    # Find the first 'package' statement
    my $package_stmnt = $doc->find_first( 'PPI::Statement::Package' );
    my $package_line = $package_stmnt ? $package_stmnt->location()->[0] : undef;

    # Find all statements that aren't 'package' statements
    my $stmnts_ref = $doc->find( 'PPI::Statement' );
    return if !$stmnts_ref;
    my @non_packages = grep {
        $self->_is_statement_of_interest( $_ )
    } @{$stmnts_ref};
    return if !@non_packages;

    # If the 'package' statement is not defined, or the other
    # statements appear before the 'package', then it violates.

    my @viols;
    for my $stmnt ( @non_packages ) {
        my $stmnt_line = $stmnt->location()->[0];
        if ( (! defined $package_line) || ($stmnt_line < $package_line) ) {
            push @viols, $self->violation( $DESC, $EXPL, $stmnt );
        }
    }

    return @viols;
}

sub _is_statement_of_interest {
    my ( $self, $elem ) = @_;

    $elem
        or return $FALSE;

    $elem->isa( 'PPI::Statement::Package' )
        and return $FALSE;

    if ( $elem->isa( 'PPI::Statement::Include' ) ) {
        if ( my $module = $elem->module() ) {
            $self->{_allow_import_of}{$module}
                and return $FALSE;
        }
        elsif ( $elem->version ) {
            return $FALSE;
        }
    }

    return $TRUE;
}

1;

__END__

#-----------------------------------------------------------------------------

=pod

=head1 NAME

Perl::Critic::Policy::Modules::RequireExplicitPackage - Always make the C<package> explicit.


=head1 AFFILIATION

This Policy is part of the core L<Perl::Critic|Perl::Critic>
distribution.


=head1 DESCRIPTION

In general, the first statement of any Perl module or library should
be a C<package> statement.  Otherwise, all the code that comes before
the C<package> statement is getting executed in the caller's package,
and you have no idea who that is.  Good encapsulation and common
decency require your module to keep its innards to itself.

There are some valid reasons for not having a C<package> statement at
all.  But make sure you understand them before assuming that you
should do it too.

One of those reasons is having a C<use VERSION> line as the first line
of your Perl file. It I<declares> which version of the Perl language
the following code is written. Because the effect is lexical, the previous
remarks about the caller's package do not apply.

The maximum number of violations per document for this policy defaults
to 1.



=head1 CONFIGURATION

As for programs, most people understand that the default package is
C<main>, so this Policy doesn't apply to files that begin with a perl
shebang.  If you want to require an explicit C<package> declaration in
all files, including programs, then add the following to your
F<.perlcriticrc> file

    [Modules::RequireExplicitPackage]
    exempt_scripts = 0

Some users may find it desirable to exempt the load of specific modules
from this policy. For example, Perl does not support Unicode module
names because of portability problems. Users who are not concerned about
this and intend to use C<UTF-8> module names will need to specify
C<use utf8;> before the package declaration. To do this, add the
following to your F<.perlcriticrc> file

    [Modules::RequireExplicitPackage]
    allow_import_of = utf8

The C<allow_import_of> configuration option takes multiple module names,
separated by spaces.


=head1 IMPORTANT CHANGES

This policy was formerly called C<ProhibitUnpackagedCode> which
sounded a bit odd.  If you get lots of "Cannot load policy module"
errors, then you probably need to change C<ProhibitUnpackagedCode> to
C<RequireExplicitPackage> in your F<.perlcriticrc> file.


=head1 AUTHOR

Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>


=head1 COPYRIGHT

Copyright (c) 2005-2011 Imaginative Software Systems.  All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.  The full text of this license
can be found in the LICENSE file included with this module.

=cut

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 78
#   indent-tabs-mode: nil
#   c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :