File Coverage

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


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Modules::RequireBarewordIncludes;
2              
3 40     40   23492 use 5.010001;
  40         129  
4 40     40   179 use strict;
  40         62  
  40         862  
5 40     40   137 use warnings;
  40         93  
  40         1509  
6 40     40   179 use Readonly;
  40         77  
  40         2265  
7              
8 40     40   239 use Perl::Critic::Utils qw{ :severities };
  40         79  
  40         1940  
9 40     40   4443 use parent 'Perl::Critic::Policy';
  40         69  
  40         221  
10              
11             our $VERSION = '1.156';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $EXPL => q{Use a bareword instead};
16              
17             #-----------------------------------------------------------------------------
18              
19 90     90 0 610 sub supported_parameters { return () }
20 77     77 1 203 sub default_severity { return $SEVERITY_HIGHEST }
21 74     74 1 192 sub default_themes { return qw(core portability) }
22 37     37 1 64 sub applies_to { return 'PPI::Statement::Include' }
23              
24             #-----------------------------------------------------------------------------
25              
26             sub violates {
27 63     63 1 116 my ( $self, $elem, undef ) = @_;
28              
29 63         147 my $child = $elem->schild(1);
30 63 50       849 return if !$child;
31              
32 63 100       223 if ( $child->isa('PPI::Token::Quote') ) {
33 2         7 my $type = $elem->type;
34 2         45 my $desc = qq{"$type" statement with library name as string};
35 2         9 return $self->violation( $desc, $EXPL, $elem );
36             }
37 61         124 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 :