Login
Moved 20_policy_prohibitevilmodules to
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / Modules / ProhibitEvilModules.pm
CommitLineData
6036a254 1##############################################################################
800f036a
CD
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
d59eec4b
JRT
7package Perl::Critic::Policy::Modules::ProhibitEvilModules;
8
9use strict;
10use warnings;
a168820a 11use Carp qw(cluck);
7cc385e4 12use English qw(-no_match_vars);
26babf22 13use List::MoreUtils qw(any);
d59eec4b 14use Perl::Critic::Utils;
d59eec4b
JRT
15use base 'Perl::Critic::Policy';
16
03967b1f 17our $VERSION = 1.00;
d59eec4b 18
fd5bd7b5
JRT
19#-----------------------------------------------------------------------------
20
d59eec4b
JRT
21my $expl = q{Find an alternative module};
22my $desc = q{Prohibited module used};
23
6036a254 24#-----------------------------------------------------------------------------
d59eec4b 25
e178fe38 26sub supported_parameters { return qw( modules ) }
fd5bd7b5
JRT
27sub default_severity { return $SEVERITY_HIGHEST }
28sub default_themes { return qw( core bugs ) }
29sub applies_to { return 'PPI::Statement::Include' }
2c943bed 30
6036a254 31#-----------------------------------------------------------------------------
2c943bed 32
d59eec4b
JRT
33sub new {
34 my ( $class, %args ) = @_;
35 my $self = bless {}, $class;
36
26babf22
JRT
37 $self->{_evil_modules} = {}; #Hash
38 $self->{_evil_modules_rx} = []; #Array
39
d59eec4b
JRT
40 #Set config, if defined
41 if ( defined $args{modules} ) {
4a7a7227 42 for my $module ( words_from_string( $args{modules} ) ) {
7cc385e4 43 if ( $module =~ m{ \A [/] (.+) [/] \z }mx ) {
702bf3be 44 # These are module name patterns (e.g. /Acme/)
a168820a 45 my $re = $1; # Untainting
7cc385e4
CD
46 my $pattern = eval { qr/$re/ };
47 if ( $EVAL_ERROR ) {
a168820a 48 cluck qq{Regexp syntax error in "$module"};
7cc385e4
CD
49 }
50 else {
51 push @{ $self->{_evil_modules_rx} }, $pattern;
52 }
26babf22
JRT
53 }
54 else {
702bf3be 55 # These are literal module names (e.g. Acme::Foo)
26babf22
JRT
56 $self->{_evil_modules}->{$module} = 1;
57 }
d59eec4b
JRT
58 }
59 }
60 return $self;
61}
62
6036a254 63#-----------------------------------------------------------------------------
d59eec4b
JRT
64
65sub violates {
e5f6c18d 66 my ( $self, $elem, undef ) = @_;
6015ad73
CD
67 my $module = $elem->module();
68 return if !$module;
26babf22
JRT
69
70 if ( exists $self->{_evil_modules}->{ $module } ||
71 any { $module =~ $_ } @{ $self->{_evil_modules_rx} } ) {
72
2c6df011 73 return $self->violation( $desc, $expl, $elem );
d59eec4b
JRT
74 }
75 return; #ok!
76}
77
781;
79
80__END__
81
6036a254 82#-----------------------------------------------------------------------------
2c943bed 83
d59eec4b
JRT
84=pod
85
86=head1 NAME
87
88Perl::Critic::Policy::Modules::ProhibitEvilModules
89
90=head1 DESCRIPTION
91
92Use this policy if you wish to prohibit the use of specific modules.
93These may be modules that you feel are deprecated, buggy, unsupported,
94insecure, or just don't like.
95
8a25c8a0 96=head1 CONFIGURATION
d59eec4b 97
8a25c8a0
ES
98The set of prohibited modules is configurable via the C<modules> option. The
99value of C<modules> should be a string of space-delimited, fully qualified
100module names and/or regular expressions. An example of prohibiting two
101specific modules in a F<.perlcriticrc> file:
d59eec4b 102
8a25c8a0
ES
103 [Modules::ProhibitEvilModules]
104 modules = Getopt::Std Autoload
d59eec4b 105
8a25c8a0
ES
106Regular expressions are identified by values beginning and ending with slashes.
107Any module with a name that matches C<m/pattern/> will be forbidden. For
108example:
26babf22
JRT
109
110 [Modules::ProhibitEvilModules]
111 modules = /Acme::/
112
8a25c8a0
ES
113would cause all modules that match C<m/Acme::/> to be forbidden. You can add
114any of the C<imxs> switches to the end of a pattern, but be aware that patterns
115cannot contain whitespace because the configuration file parser uses it to
116delimit the module names and patterns.
26babf22 117
8a25c8a0
ES
118By default, there are no prohibited modules (although I can think of a few that
119should be).
d59eec4b
JRT
120
121=head1 NOTES
122
123Note that this policy doesn't apply to pragmas. Future versions may
124allow you to specify an alternative for each prohibited module, which
125can be suggested by L<Perl::Critic>.
126
127=head1 AUTHOR
128
129Jeffrey Ryan Thalhammer <thaljef@cpan.org>
130
131=head1 COPYRIGHT
132
0d5b2dca 133Copyright (c) 2005-2007 Jeffrey Ryan Thalhammer. All rights reserved.
d59eec4b
JRT
134
135This program is free software; you can redistribute it and/or modify
136it under the same terms as Perl itself. The full text of this license
137can be found in the LICENSE file included with this module.
138
139=cut
737d3b65
CD
140
141# Local Variables:
142# mode: cperl
143# cperl-indent-level: 4
144# fill-column: 78
145# indent-tabs-mode: nil
146# c-indentation-style: bsd
147# End:
345c7562 148# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab :