« blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitTrailingWhitespace.pm
↑ Summary
blib/lib/Perl/Critic/Exception/Configuration/Option.pm »
| 1 | package Perl::Critic::Policy::Modules::RequireBarewordIncludes; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||
| 12 | |||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 40 | Readonly::Scalar my $EXPL => q{Use a bareword instead}; | |||||||||
| 16 | |||||||||||
| 17 | #----------------------------------------------------------------------------- | ||||||||||
| 18 | |||||||||||
| 19 | 90 | ▶ | sub supported_parameters { return () } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 20 | 77 | ▶ | sub default_severity { return $SEVERITY_HIGHEST } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 74 | ▶ | sub default_themes { return qw(core portability) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 22 | 37 | ▶ | sub applies_to { return 'PPI::Statement::Include' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | |||||||||||
| 24 | #----------------------------------------------------------------------------- | ||||||||||
| 25 | |||||||||||
| 26 | sub violates { | ||||||||||
| 27 | 63 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 28 | |||||||||||
| 29 | 63 | my $child = $elem->schild(1); | |||||||||
| 30 | 63 | ▶ | return if !$child; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 63 | ▶ | if ( $child->isa('PPI::Token::Quote') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 2 | my $type = $elem->type; | |||||||||
| 34 | 2 | my $desc = qq{"$type" statement with library name as string}; | |||||||||
| 35 | 2 | return $self->violation( $desc, $EXPL, $elem ); | |||||||||
| 36 | } | ||||||||||
| 37 | 61 | return; #ok! | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 40 | 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 : | ||||||||||