File Coverage

blib/lib/Perl/Critic/Policy/Modules/RequireBarewordIncludes.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 4 100.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 48 49 97.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Modules::RequireBarewordIncludes;
2              
3 40     40   26091 use 5.010001;
  40         165  
4 40     40   235 use strict;
  40         118  
  40         832  
5 40     40   190 use warnings;
  40         338  
  40         1108  
6 40     40   250 use Readonly;
  40         114  
  40         2400  
7              
8 40     40   332 use Perl::Critic::Utils qw{ :severities };
  40         106  
  40         2141  
9 40     40   5467 use parent 'Perl::Critic::Policy';
  40         147  
  40         232  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $EXPL => q{Use a bareword instead};
16              
17             #-----------------------------------------------------------------------------
18              
19 92     92 0 1676 sub supported_parameters { return () }
20 81     81 1 360 sub default_severity { return $SEVERITY_HIGHEST }
21 74     74 1 352 sub default_themes { return qw(core portability) }
22 39     39 1 163 sub applies_to { return 'PPI::Statement::Include' }
23              
24             #-----------------------------------------------------------------------------
25              
26             sub violates {
27 77     77 1 186 my ( $self, $elem, undef ) = @_;
28              
29 77         259 my $child = $elem->schild(1);
30 77 100       1423 return if !$child;
31              
32 76 100       388 if ( $child->isa('PPI::Token::Quote') ) {
33 7         27 my $type = $elem->type;
34 7         176 my $desc = qq{"$type" statement with library name as string};
35 7         40 return $self->violation( $desc, $EXPL, $elem );
36             }
37 69         208 return; #ok!
38             }
39              
40              
41             1;
42              
43             __END__
44              
45             #-----------------------------------------------------------------------------
46              
47             =pod
48              
49             =head1 NAME
50              
51             Perl::Critic::Policy::Modules::RequireBarewordIncludes - Write C<require Module> instead of C<require 'Module.pm'>.
52              
53              
54             =head1 AFFILIATION
55              
56             This Policy is part of the core L<Perl::Critic|Perl::Critic>
57             distribution.
58              
59              
60             =head1 DESCRIPTION
61              
62             When including another module (or library) via the C<require> or
63             C<use> statements, it is best to identify the module (or library)
64             using a bareword rather than an explicit path. This is because paths
65             are usually not portable from one machine to another. Also, Perl
66             automatically assumes that the filename ends in '.pm' when the library
67             is expressed as a bareword. So as a side-effect, this Policy
68             encourages people to write '*.pm' modules instead of the old-school
69             '*.pl' libraries.
70              
71             use 'My/Perl/Module.pm'; #not ok
72             use My::Perl::Module; #ok
73              
74              
75             =head1 CONFIGURATION
76              
77             This Policy is not configurable except for the standard options.
78              
79              
80             =head1 NOTES
81              
82             This Policy is a replacement for C<ProhibitRequireStatements>, which
83             completely banned the use of C<require> for the sake of eliminating
84             the old '*.pl' libraries from Perl4. Upon further consideration, I
85             realized that C<require> is quite useful and necessary to enable
86             run-time loading. Thus, C<RequireBarewordIncludes> does allow you to
87             use C<require>, but still encourages you to write '*.pm' modules.
88              
89             Sometimes, you may want to load modules at run-time, but you don't
90             know at design-time exactly which module you will need to load
91             (L<Perl::Critic|Perl::Critic> is an example of this). In that case,
92             just attach the C<'## no critic'> annotation like so:
93              
94             require $module_name; ## no critic
95              
96              
97             =head1 CREDITS
98              
99             Chris Dolan <cdolan@cpan.org> was instrumental in identifying the
100             correct motivation for and behavior of this Policy. Thanks Chris.
101              
102              
103             =head1 AUTHOR
104              
105             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
106              
107              
108             =head1 COPYRIGHT
109              
110             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
111              
112             This program is free software; you can redistribute it and/or modify
113             it under the same terms as Perl itself. The full text of this license
114             can be found in the LICENSE file included with this module.
115              
116             =cut
117              
118             # Local Variables:
119             # mode: cperl
120             # cperl-indent-level: 4
121             # fill-column: 78
122             # indent-tabs-mode: nil
123             # c-indentation-style: bsd
124             # End:
125             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :