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