File Coverage

blib/lib/Perl/Critic/Policy/Community/PreferredAlternatives.pm
Criterion Covered Total %
statement 22 23 95.6
branch 2 2 100.0
condition 6 9 66.6
subroutine 9 10 90.0
pod 4 5 80.0
total 43 49 87.7


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   367 use warnings;
  1         2  
  1         23  
4 1     1   4  
  1         3  
  1         33  
5             use Perl::Critic::Utils qw(:severities :classification :ppi);
6 1     1   6 use parent 'Perl::Critic::Policy';
  1         24  
  1         44  
7 1     1   321  
  1         2  
  1         5  
8             our $VERSION = 'v1.0.3';
9              
10             (
11             {
12             name => 'allowed_modules',
13             description => 'Modules that you want to allow, despite there being a preferred alternative.',
14 4     4 0 12443 behavior => 'string list',
15             },
16             )
17             }
18              
19             my %modules = (
20 8     8 1 65 'Getopt::Std' => 'Getopt::Std was the original very simplistic command-line option processing module. It is now obsoleted by the much more complete solution Getopt::Long, which also supports short options, and is wrapped by module such as Getopt::Long::Descriptive and Getopt::Long::Modern for simpler usage.',
21 0     0 1 0 'JSON' => 'JSON.pm is old and full of slow logic. Use JSON::MaybeXS instead, it is a drop-in replacement in most cases.',
22 4     4 1 32013 'List::MoreUtils' => 'List::MoreUtils is a far more complex distribution than it needs to be. Use List::SomeUtils instead, or see List::Util or List::UtilsBy for alternatives.',
23             'Mouse' => 'Mouse was created to be a faster version of Moose, a niche that has since been better filled by Moo. Use Moo instead.',
24             'Readonly' => 'Readonly.pm is buggy and slow. Use Const::Fast or ReadonlyX instead, or the core pragma constant.',
25             );
26              
27             my ($self, $module, $elem) = @_;
28             my $desc = "Used module $module";
29             my $expl = $modules{$module} // "Module $module has preferred alternatives.";
30             return $self->violation($desc, $expl, $elem);
31             }
32              
33 8     8   144 my ($self, $elem) = @_;
34 8         16 return () unless defined $elem->module and exists $modules{$elem->module} and not exists $self->{_allowed_modules}{$elem->module};
35 8   33     24 return $self->_violation($elem->module, $elem);
36 8         21 }
37              
38             1;
39              
40 22     22 1 1475 =head1 NAME
41 22 100 66     45  
      100        
42 8         474 Perl::Critic::Policy::Community::PreferredAlternatives - Various modules with
43             preferred alternatives
44              
45             =head1 DESCRIPTION
46              
47             Various modules have alternatives that are preferred by some subsets of the
48             community, for various reasons which may include: buggy behavior, cruft,
49             performance problems, maintainer issues, or simply better modern replacements.
50             This is a low severity complement to
51             L<Perl::Critic::Policy::Community::DiscouragedModules>.
52              
53             =head1 MODULES
54              
55             =head2 Getopt::Std
56              
57             L<Getopt::Std> was the original very simplistic command-line option processing
58             module. It is now obsoleted by the much more complete solution L<Getopt::Long>,
59             which also supports short options, and is wrapped by modules such as
60             L<Getopt::Long::Descriptive> and L<Getopt::Long::Modern> for simpler usage.
61              
62             =head2 JSON
63              
64             L<JSON>.pm is old and full of slow logic. Use L<JSON::MaybeXS> instead, it is a
65             drop-in replacement in most cases.
66              
67             =head2 List::MoreUtils
68              
69             L<List::MoreUtils> is a far more complex distribution than it needs to be. Use
70             L<List::SomeUtils> instead, or see L<List::Util> or L<List::UtilsBy> for
71             alternatives.
72              
73             =head2 Mouse
74              
75             L<Mouse> was created to be a faster version of L<Moose>, a niche that has since
76             been better filled by L<Moo>. Use L<Moo> instead.
77              
78             =head2 Readonly
79              
80             L<Readonly>.pm is buggy and slow. Use L<Const::Fast> or L<ReadonlyX> instead,
81             or the core pragma L<constant>.
82              
83             =head1 AFFILIATION
84              
85             This policy is part of L<Perl::Critic::Community>.
86              
87             =head1 CONFIGURATION
88              
89             Occasionally you may find yourself needing to use one of these non-preferred
90             modules, and do not want the warnings. You can do so by putting something like
91             the following in a F<.perlcriticrc> file like this:
92              
93             [Community::PreferredAlternatives]
94             allowed_modules = Getopt::Std JSON
95              
96             The same option is offered for L<Perl::Critic::Policy::Community::DiscouragedModules>.
97              
98             =head1 AUTHOR
99              
100             Dan Book, C<dbook@cpan.org>
101              
102             =head1 COPYRIGHT AND LICENSE
103              
104             Copyright 2015, Dan Book.
105              
106             This library is free software; you may redistribute it and/or modify it under
107             the terms of the Artistic License version 2.0.
108              
109             =head1 SEE ALSO
110              
111             L<Perl::Critic>