1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Modules/ProhibitAutomaticExportation.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Modules::ProhibitAutomaticExportation;
15 use Perl::Critic::Utils qw{ :severities };
16 use List::MoreUtils qw(any);
17 use base 'Perl::Critic::Policy';
19 our $VERSION = '1.088';
21 #-----------------------------------------------------------------------------
23 Readonly::Scalar my $DESC => q{Symbols are exported by default};
24 Readonly::Scalar my $EXPL => q{Use '@EXPORT_OK' or '%EXPORT_TAGS' instead}; ## no critic
26 #-----------------------------------------------------------------------------
28 sub supported_parameters { return () }
29 sub default_severity { return $SEVERITY_HIGH }
30 sub default_themes { return qw( core bugs ) }
31 sub applies_to { return 'PPI::Document' }
33 #-----------------------------------------------------------------------------
36 my ( $self, $elem, $doc ) = @_;
38 if ( _uses_exporter($doc) ) {
39 if ( my $exp = _has_exports($doc) ) {
40 return $self->violation( $DESC, $EXPL, $exp );
46 #-----------------------------------------------------------------------------
50 my $includes_ref = $doc->find('PPI::Statement::Include');
51 return if !$includes_ref;
52 #This covers both C<use Exporter;> and C<use base 'Exporter';>
53 return scalar grep { m/ \b Exporter \b/mx } @{ $includes_ref };
60 my $wanted = sub {_our_EXPORT(@_) || _vars_EXPORT(@_) || _package_EXPORT(@_)};
61 return $doc->find_first( $wanted );
67 my (undef, $elem) = @_;
68 $elem->isa('PPI::Statement::Variable') || return 0;
69 $elem->type() eq 'our' || return 0;
70 return any { $_ eq '@EXPORT' } $elem->variables(); ## no critic(RequireInterpolationOfMetachars)
76 my (undef, $elem) = @_;
77 $elem->isa('PPI::Statement::Include') || return 0;
78 $elem->pragma() eq 'vars' || return 0;
79 return $elem =~ m{ \@EXPORT \b }mx; #Crude, but usually works
85 my (undef, $elem) = @_;
86 $elem->isa('PPI::Token::Symbol') || return 0;
87 return $elem =~ m{ \A \@ \S+ ::EXPORT \z }mx;
88 #TODO: ensure that it is in _this_ package!
95 #-----------------------------------------------------------------------------
101 Perl::Critic::Policy::Modules::ProhibitAutomaticExportation - Export symbols via C<@EXPORT_OK> or C<%EXPORT_TAGS> instead of C<@EXPORT>.
105 This Policy is part of the core L<Perl::Critic> distribution.
110 When using L<Exporter>, symbols placed in the C<@EXPORT> variable are
111 automatically exported into the caller's namespace. Although
112 convenient, this practice is not polite, and may cause serious
113 problems if the caller declares the same symbols. The best practice
114 is to place your symbols in C<@EXPORT_OK> or C<%EXPORT_TAGS> and let
115 the caller choose exactly which symbols to export.
119 use base qw(Exporter);
120 our @EXPORT = qw(&foo &bar); # not ok
121 our @EXPORT_OK = qw(&foo &bar); # ok
122 our %EXPORT_TAGS = ( all => [ qw(&foo &bar) ] ); # ok
127 This Policy is not configurable except for the standard options.
132 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
136 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
138 This program is free software; you can redistribute it and/or modify
139 it under the same terms as Perl itself. The full text of this license
140 can be found in the LICENSE file included with this module.
146 # cperl-indent-level: 4
148 # indent-tabs-mode: nil
149 # c-indentation-style: bsd
151 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :