Login
Reorder methods in Documentation::PodSpelling to make violates()
[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
ES
15
16use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
17
d59eec4b
JRT
18use base 'Perl::Critic::Policy';
19
e858ba92 20our $VERSION = 1.06;
d59eec4b 21
fd5bd7b5
JRT
22#-----------------------------------------------------------------------------
23
c680a9c9
ES
24Readonly::Scalar my $EXPL => q{Find an alternative module};
25Readonly::Scalar my $DESC => q{Prohibited module used};
d59eec4b 26
6036a254 27#-----------------------------------------------------------------------------
d59eec4b 28
e178fe38 29sub supported_parameters { return qw( modules ) }
fd5bd7b5
JRT
30sub default_severity { return $SEVERITY_HIGHEST }
31sub default_themes { return qw( core bugs ) }
32sub applies_to { return 'PPI::Statement::Include' }
2c943bed 33
6036a254 34#-----------------------------------------------------------------------------
2c943bed 35
d59eec4b 36sub new {
6e7d6c9f
CD
37 my ($class, @args) = @_;
38 my $self = $class->SUPER::new(@args);
85da2bf2 39
6e7d6c9f 40 my %config = @args;
d59eec4b 41
26babf22
JRT
42 $self->{_evil_modules} = {}; #Hash
43 $self->{_evil_modules_rx} = []; #Array
44
d59eec4b 45 #Set config, if defined
85da2bf2
ES
46 if ( defined $config{modules} ) {
47 for my $module ( words_from_string( $config{modules} ) ) {
95ed7a74 48
7cc385e4 49 if ( $module =~ m{ \A [/] (.+) [/] \z }mx ) {
95ed7a74 50
702bf3be 51 # These are module name patterns (e.g. /Acme/)
a168820a 52 my $re = $1; # Untainting
7cc385e4 53 my $pattern = eval { qr/$re/ };
95ed7a74
JRT
54
55 die qq{Regexp syntax error in your profile: "$module"\n}
56 if $EVAL_ERROR;
57
58 push @{ $self->{_evil_modules_rx} }, $pattern;
26babf22
JRT
59 }
60 else {
702bf3be 61 # These are literal module names (e.g. Acme::Foo)
26babf22
JRT
62 $self->{_evil_modules}->{$module} = 1;
63 }
d59eec4b
JRT
64 }
65 }
66 return $self;
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 :