1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Modules/ProhibitEvilModules.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
7 package Perl::Critic::Policy::Modules::ProhibitEvilModules;
12 use English qw(-no_match_vars);
15 use List::MoreUtils qw(any);
17 use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue
18 qw{ throw_policy_value };
19 use Perl::Critic::Utils qw{
20 :booleans :characters :severities :data_conversion
23 use base 'Perl::Critic::Policy';
25 our $VERSION = '1.088';
27 #-----------------------------------------------------------------------------
29 Readonly::Scalar my $EXPL => q{Find an alternative module};
30 Readonly::Scalar my $DESC => q{Prohibited module used};
32 #-----------------------------------------------------------------------------
34 sub supported_parameters {
38 description => 'The names of or patterns for modules to forbid.',
39 default_string => $EMPTY,
40 behavior => 'string list',
45 sub default_severity { return $SEVERITY_HIGHEST }
46 sub default_themes { return qw( core bugs ) }
47 sub applies_to { return 'PPI::Statement::Include' }
49 #-----------------------------------------------------------------------------
51 sub initialize_if_enabled {
52 my ($self, $config) = @_;
54 $self->{_evil_modules} = {}; #Hash
55 $self->{_evil_modules_rx} = []; #Array
57 #Set config, if defined
58 if ( defined $self->{_modules} ) {
59 my @modules = sort keys %{ $self->{_modules} };
60 foreach my $module ( @modules ) {
61 if ( $module =~ m{ \A [/] (.+) [/] \z }mx ) {
63 # These are module name patterns (e.g. /Acme/)
64 my $re = $1; # Untainting
65 my $pattern = eval { qr/$re/ }; ##no critic (RegularExpressions::.*)
69 policy => $self->get_short_name(),
70 option_name => 'modules',
71 option_value => ( join q{", "}, @modules ),
73 qq{contains an invalid regular expression: "$module"};
76 push @{ $self->{_evil_modules_rx} }, $pattern;
79 # These are literal module names (e.g. Acme::Foo)
80 $self->{_evil_modules}->{$module} = 1;
88 #-----------------------------------------------------------------------------
91 my ( $self, $elem, undef ) = @_;
92 my $module = $elem->module();
95 if ( exists $self->{_evil_modules}->{ $module } ||
96 any { $module =~ $_ } @{ $self->{_evil_modules_rx} } ) {
98 return $self->violation( $DESC, $EXPL, $elem );
107 #-----------------------------------------------------------------------------
113 Perl::Critic::Policy::Modules::ProhibitEvilModules - Ban modules that aren't blessed by your shop.
117 This Policy is part of the core L<Perl::Critic> distribution.
122 Use this policy if you wish to prohibit the use of specific modules.
123 These may be modules that you feel are deprecated, buggy, unsupported,
124 insecure, or just don't like.
128 The set of prohibited modules is configurable via the C<modules> option. The
129 value of C<modules> should be a string of space-delimited, fully qualified
130 module names and/or regular expressions. An example of prohibiting two
131 specific modules in a F<.perlcriticrc> file:
133 [Modules::ProhibitEvilModules]
134 modules = Getopt::Std Autoload
136 Regular expressions are identified by values beginning and ending with slashes.
137 Any module with a name that matches C<m/pattern/> will be forbidden. For
140 [Modules::ProhibitEvilModules]
143 would cause all modules that match C<m/Acme::/> to be forbidden. You can add
144 any of the C<imxs> switches to the end of a pattern, but be aware that patterns
145 cannot contain whitespace because the configuration file parser uses it to
146 delimit the module names and patterns.
148 By default, there are no prohibited modules (although I can think of a few that
153 Note that this policy doesn't apply to pragmas. Future versions may
154 allow you to specify an alternative for each prohibited module, which
155 can be suggested by L<Perl::Critic>.
159 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
163 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
165 This program is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself. The full text of this license
167 can be found in the LICENSE file included with this module.
173 # cperl-indent-level: 4
175 # indent-tabs-mode: nil
176 # c-indentation-style: bsd
178 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :