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