Commit | Line | Data |
---|---|---|
aaa8512c JRT |
1 | ####################################################################### |
2 | # $URL$ | |
3 | # $Date$ | |
4 | # $Author$ | |
5 | # $Revision$ | |
6 | ######################################################################## | |
7 | ||
59b05e08 JRT |
8 | package Perl::Critic; |
9 | ||
10 | use strict; | |
11 | use warnings; | |
12 | use File::Spec; | |
13 | use English qw(-no_match_vars); | |
14 | use Perl::Critic::Config; | |
15 | use Perl::Critic::Utils; | |
16 | use Carp; | |
17 | use PPI; | |
18 | ||
19 | our $VERSION = '0.13'; | |
20 | $VERSION = eval $VERSION; ## no critic | |
21 | ||
22 | #---------------------------------------------------------------------------- | |
23 | # | |
24 | sub new { | |
25 | ||
26 | my ( $class, %args ) = @_; | |
27 | ||
28 | # Default arguments | |
29 | my $priority = defined $args{-priority} ? $args{-priority} : 0; | |
30 | my $profile_path = $args{-profile}; | |
31 | my $force = $args{-force} || 0; | |
32 | ||
33 | # Create and init object | |
34 | my $self = bless {}, $class; | |
35 | $self->{_force} = $force; | |
36 | $self->{_policies} = []; | |
37 | ||
38 | # Read profile and add policies | |
39 | my $config = Perl::Critic::Config->new( %args ); | |
40 | while ( my ( $policy, $params ) = each %{$config} ) { | |
41 | $self->add_policy( -policy => $policy, -config => $params ); | |
42 | } | |
43 | return $self; | |
44 | } | |
45 | ||
46 | #---------------------------------------------------------------------------- | |
47 | # | |
48 | sub add_policy { | |
49 | ||
50 | my ( $self, %args ) = @_; | |
51 | my $module_name = $args{-policy} || return; | |
52 | my $config = $args{-config} || {}; | |
53 | ||
54 | #Qualify name if full module name not given | |
55 | my $namespace = 'Perl::Critic::Policy'; | |
56 | if ( $module_name !~ m{ \A $namespace }mx ) { | |
57 | $module_name = $namespace . q{::} . $module_name; | |
58 | } | |
59 | ||
60 | #Convert module name to file path. I'm trying to do | |
61 | #this in a portable way, but I'm not sure it actually is. | |
62 | my $module_file = File::Spec->catfile( split q{::}, $module_name ); | |
63 | $module_file .= '.pm'; | |
64 | ||
65 | #Try to load module and instantiate | |
66 | eval { | |
67 | require $module_file; ## no critic | |
68 | my $policy = $module_name->new( %{$config} ); | |
69 | push @{ $self->{_policies} }, $policy; | |
70 | }; | |
71 | ||
72 | #Failure to load is not fatal | |
73 | if ($EVAL_ERROR) { | |
74 | carp qq{Cannot load policy module $module_name: $EVAL_ERROR}; | |
75 | return; | |
76 | } | |
77 | ||
78 | return $self; | |
79 | } | |
80 | ||
81 | #---------------------------------------------------------------------------- | |
82 | # | |
83 | sub critique { | |
84 | # Here we go! | |
85 | my ( $self, $source_code ) = @_; | |
86 | ||
87 | # Parse the code | |
88 | my $doc = PPI::Document->new($source_code); | |
89 | ||
90 | # Bail on error | |
91 | if( ! defined $doc ) { | |
92 | my $errstr = PPI::Document::errstr(); | |
93 | my $file = -f $source_code ? $source_code : 'stdin'; | |
94 | die qq{Cannot parse code: $errstr of '$file'\n}; | |
95 | } | |
96 | ||
97 | # Pre-index location of each node (for speed) | |
98 | $doc->index_locations(); | |
99 | ||
100 | # Filter exempt code, if desired | |
101 | $self->{_force} || _filter_code($doc); | |
102 | ||
103 | # Remove the magic shebang fix | |
104 | _unfix_shebang($doc); | |
105 | ||
106 | # Run engine, testing each Policy at each element | |
107 | my $elems = $doc->find( 'PPI::Element' ) || return; #Nothing to do! | |
108 | my @pols = @{ $self->policies() }; @pols || return; #Nothing to do! | |
109 | return map { my $e = $_; map { $_->violates($e, $doc) } @pols } @{$elems}; | |
110 | } | |
111 | ||
112 | #---------------------------------------------------------------------------- | |
113 | # | |
114 | sub policies { $_[0]->{_policies} } | |
115 | ||
116 | #============================================================================ | |
117 | #PRIVATE SUBS | |
118 | ||
119 | sub _filter_code { | |
120 | ||
121 | my $doc = shift; | |
122 | my $nodes_ref = $doc->find('PPI::Token::Comment') || return; | |
123 | my $no_critic = qr{\A \s* \#\# \s* no \s+ critic}mx; | |
124 | my $use_critic = qr{\A \s* \#\# \s* use \s+ critic}mx; | |
125 | ||
126 | PRAGMA: | |
127 | for my $pragma ( grep { $_ =~ $no_critic } @{$nodes_ref} ) { | |
128 | ||
129 | #Handle single-line usage | |
130 | if ( my $sib = $pragma->sprevious_sibling() ) { | |
131 | if ( $sib->location->[0] == $pragma->location->[0] ) { | |
132 | $sib->statement->delete(); | |
133 | next PRAGMA; | |
134 | } | |
135 | } | |
136 | ||
137 | SIB: | |
138 | while ( my $sib = $pragma->next_sibling() ) { | |
139 | my $ended = $sib->isa('PPI::Token::Comment') && $sib =~ $use_critic; | |
140 | $sib->delete(); #$sib is undef now. | |
141 | last SIB if $ended; | |
142 | } | |
143 | } | |
144 | continue { | |
145 | $pragma->delete(); | |
146 | } | |
147 | } | |
148 | ||
149 | sub _unfix_shebang { | |
150 | ||
151 | ||
152 | #When you install a script using ExtUtils::MakeMaker or | |
153 | #Module::Build, it inserts some magical code into the top of the | |
154 | #file (just after the shebang). This code allows people to call | |
155 | #your script using a shell, like `sh my_script`. Unfortunately, | |
156 | #this code causes several Policy violations, so we just remove it. | |
157 | ||
158 | my $doc = shift; | |
159 | my $first_stmnt = $doc->schild(0) || return; | |
160 | ||
161 | ||
162 | #Different versions of MakeMaker and Build use slightly differnt | |
163 | #shebang fixing strings. This matches most of the ones I've found | |
164 | #in my own Perl distribution, but it may not be bullet-proof. | |
165 | ||
166 | my $fixin_rx = qr{^eval 'exec .* \$0 \${1\+"\$@"}'\s*[\r\n]\s*if.+;}; | |
167 | if ( $first_stmnt =~ $fixin_rx ) { $first_stmnt->delete() } | |
168 | } | |
169 | ||
170 | 1; | |
171 | ||
172 | #---------------------------------------------------------------------------- | |
173 | ||
174 | __END__ | |
175 | ||
176 | =pod | |
177 | ||
178 | =head1 NAME | |
179 | ||
180 | Perl::Critic - Critique Perl source for style and standards | |
181 | ||
182 | =head1 SYNOPSIS | |
183 | ||
184 | use Perl::Critic; | |
185 | ||
186 | #Create Critic and load Policies from default config file | |
187 | $critic = Perl::Critic->new(); | |
188 | ||
189 | #Create Critic and load only the most important Polices | |
190 | $critic = Perl::Critic->new(-priority => 1); | |
191 | ||
192 | #Create Critic and load Policies from specific config file | |
193 | $critic = Perl::Critic->new(-profile => $file); | |
194 | ||
195 | #Create Critic and load Policy by hand | |
196 | $critic = Perl::Critic->new(-profile => 'NONE'); | |
197 | $critic->add_policy('MyPolicyModule'); | |
198 | ||
199 | #Analyze code for policy violations | |
200 | @violations = $critic->critique($source_code); | |
201 | ||
202 | =head1 DESCRIPTION | |
203 | ||
204 | Perl::Critic is an extensible framework for creating and applying | |
205 | coding standards to Perl source code. Essentially, it is a static | |
206 | source code analysis engine. Perl::Critic is distributed with a | |
207 | number of L<Perl::Critic::Policy> modules that attempt to enforce | |
208 | various coding guidelines. Most Policies are based on Damian Conway's | |
209 | book B<Perl Best Practices>. You can choose and customize those | |
210 | Polices through the Perl::Critic interface. You can also create new | |
211 | Policy modules that suit your own tastes. | |
212 | ||
213 | For a convenient command-line interface to Perl::Critic, see the | |
214 | documentation for L<perlcritic>. If you want to integrate | |
215 | Perl::Critic with your build process, L<Test::Perl::Critic> provides a | |
216 | nice interface that is suitable for test scripts. | |
217 | ||
218 | =head1 CONSTRUCTOR | |
219 | ||
220 | =over 8 | |
221 | ||
222 | =item new ( [ -profile => $FILE, -priority => $N, -include => \@PATTERNS, -exclude => \@PATTERNS, -force => 1 ] ) | |
223 | ||
224 | Returns a reference to a new Perl::Critic object. Most arguments are | |
225 | just passed directly into L<Perl::Critic::Config>, but I have described | |
226 | them here as well. All arguments are optional key-value pairs as | |
227 | follows: | |
228 | ||
229 | B<-profile> is a path to a configuration file. If C<$FILE> is not | |
230 | defined, Perl::Critic::Config attempts to find a F<.perlcriticrc> | |
231 | configuration file in the current directory, and then in your home | |
232 | directory. Alternatively, you can set the C<PERLCRITIC> environment | |
233 | variable to point to a file in another location. If a configuration | |
234 | file can't be found, or if C<$FILE> is an empty string, then it | |
235 | defaults to include all the Policy modules that ship with | |
236 | Perl::Critic. See L<"CONFIGURATION"> for more information. | |
237 | ||
238 | B<-priority> is the maximum priority value of Policies that should be | |
239 | added to the Perl::Critic::Config. 1 is the "highest" priority, | |
240 | and all numbers larger than 1 have "lower" priority. Once the | |
241 | user-preferences have been read from the C<-profile>, All Policies | |
242 | that are configured with a priority greater than C<$N> will be removed | |
243 | from this Config. For a given C<-profile>, increasing C<$N> will | |
244 | result in more Policy violations. The default C<-priority> is 1. See | |
245 | L<"CONFIGURATION"> for more information. | |
246 | ||
247 | B<-include> is a reference to a list of C<@PATTERNS>. Once the | |
248 | user-preferences have been read from the C<-profile>, all Policies | |
249 | that do not match at least one C<m/$PATTERN/imx> will be removed | |
250 | from this Config. Using the C<-include> option causes the <-priority> | |
251 | option to be ignored. | |
252 | ||
253 | B<-exclude> is a reference to a list of C<@PATTERNS>. Once the | |
254 | user-preferences have been read from the C<-profile>, all Policies | |
255 | that match at least one C<m/$PATTERN/imx> will be removed from | |
256 | the Config. Using the C<-exclude> option causes the <-priority> | |
257 | option to be ignored. The C<-exclude> patterns are applied after the | |
258 | <-include> patterns, therefore, the C<-exclude> patterns take | |
259 | precedence. | |
260 | ||
261 | B<-force> controls whether Perl::Critic observes the magical C<"no | |
262 | critic"> pseudo-pragmas in your code. If set to a true value, | |
263 | Perl::Critic will analyze all code. If set to a false value (which is | |
264 | the default) Perl::Critic will overlook code that is tagged with these | |
265 | comments. See L<"BENDING THE RULES"> for more information. | |
266 | ||
267 | =back | |
268 | ||
269 | =head1 METHODS | |
270 | ||
271 | =over 8 | |
272 | ||
273 | =item add_policy( -policy => $STRING [, -config => \%HASH ] ) | |
274 | ||
275 | Loads a Policy into this Critic engine. The engine will attempt to | |
276 | C<require> the module named by $STRING and instantiate it. If the | |
277 | module fails to load or cannot be instantiated, it will throw a | |
278 | warning and return a false value. Otherwise, it returns a reference | |
279 | to this Critic engine. | |
280 | ||
281 | B<-policy> is the name of a L<Perl::Critic::Policy> subclass | |
282 | module. The C<'Perl::Critic::Policy'> portion of the name can be | |
283 | omitted for brevity. This argument is required. | |
284 | ||
285 | B<-config> is an optional reference to a hash of Policy configuration | |
286 | parameters (Note that this is B<not> a Perl::Critic::Config object). The | |
287 | contents of this hash reference will be passed into to the constructor | |
288 | of the Policy module. See the documentation in the relevant Policy | |
289 | module for a description of the arguments it supports. | |
290 | ||
291 | =item critique( $source_code ) | |
292 | ||
293 | Runs the C<$source_code> through the Perl::Critic engine using all the | |
294 | policies that have been loaded into this engine. If C<$source_code> | |
295 | is a scalar reference, then it is treated as string of actual Perl | |
296 | code. Otherwise, it is treated as a path to a file containing Perl | |
297 | code. Returns a list of L<Perl::Critic::Violation> objects for each | |
298 | violation of the loaded Policies. The list is sorted in the order | |
299 | that the Violations appear in the code. If there are no violations, | |
300 | returns an empty list. | |
301 | ||
302 | =item policies( void ) | |
303 | ||
304 | Returns a list containing references to all the Policy objects that | |
305 | have been loaded into this engine. Objects will be in the order that | |
306 | they were loaded. | |
307 | ||
308 | =back | |
309 | ||
310 | =head1 CONFIGURATION | |
311 | ||
312 | The default configuration file is called F<.perlcriticrc>. | |
313 | Perl::Critic::Config will look for this file in the current directory | |
314 | first, and then in your home directory. Alternatively, you can set | |
315 | the PERLCRITIC environment variable to explicitly point to a different | |
316 | file in another location. If none of these files exist, and the | |
317 | C<-profile> option is not given to the constructor, | |
318 | Perl::Critic::Config defaults to include all the policies that are | |
319 | shipped with Perl::Critic. | |
320 | ||
321 | The format of the configuration file is a series of named sections | |
322 | that contain key-value pairs separated by '='. Comments should | |
323 | start with '#' and can be placed on a separate line or after the | |
324 | name-value pairs if you desire. The general recipe is a series of | |
325 | blocks like this: | |
326 | ||
327 | [Perl::Critic::Policy::Category::PolicyName] | |
328 | priority = 1 | |
329 | arg1 = value1 | |
330 | arg2 = value2 | |
331 | ||
332 | C<Perl::Critic::Policy::Category::PolicyName> is the full name of a | |
333 | module that implements the policy. The Policy modules distributed | |
334 | with Perl::Critic have been grouped into categories according to the | |
335 | table of contents in Damian Conway's book B<Perl Best Practices>. For | |
336 | brevity, you can omit the C<'Perl::Critic::Policy'> part of the | |
337 | module name. All Policy modules must be a subclass of | |
338 | L<Perl::Critic::Policy>. | |
339 | ||
340 | C<priority> is the level of importance you wish to assign to this | |
341 | policy. 1 is the "highest" priority level, and all numbers greater | |
342 | than 1 have increasingly "lower" priority. Only those policies with a | |
343 | priority less than or equal to the C<-priority> value given to the | |
344 | constructor will be loaded. The priority can be an arbitrarily large | |
345 | positive integer. If the priority is not defined, it defaults to 1. | |
346 | ||
347 | The remaining key-value pairs are configuration parameters for that | |
348 | specific Policy and will be passed into the constructor of the | |
349 | L<Perl::Critic::Policy> subclass. The constructors for most Policy | |
350 | modules do not support arguments, and those that do should have | |
351 | reasonable defaults. See the documentation on the appropriate Policy | |
352 | module for more details. | |
353 | ||
354 | By default, all the policies that are distributed with Perl::Critic | |
355 | are added to the Config. Rather than assign a priority level to a | |
356 | Policy, you can simply "turn off" a Policy by prepending a '-' to the | |
357 | name of the module in the config file. In this manner, the Policy | |
358 | will never be loaded, regardless of the C<-priority> given to the | |
359 | constructor. | |
360 | ||
361 | ||
362 | A simple configuration might look like this: | |
363 | ||
364 | #-------------------------------------------------------------- | |
365 | # These are really important, so always load them | |
366 | ||
367 | [TestingAndDebugging::RequirePackageStricture] | |
368 | priority = 1 | |
369 | ||
370 | [TestingAndDebugging::RequirePackageWarnings] | |
371 | priority = 1 | |
372 | ||
373 | #-------------------------------------------------------------- | |
374 | # These are less important, so only load when asked | |
375 | ||
376 | [Variables::ProhibitPackageVars] | |
377 | priority = 2 | |
378 | ||
379 | [ControlStructures::ProhibitPostfixControls] | |
380 | priority = 2 | |
381 | ||
382 | #-------------------------------------------------------------- | |
383 | # I do not agree with these, so never load them | |
384 | ||
385 | [-NamingConventions::ProhibitMixedCaseVars] | |
386 | [-NamingConventions::ProhibitMixedCaseSubs] | |
387 | ||
388 | =head1 THE POLICIES | |
389 | ||
390 | The following Policy modules are distributed with Perl::Critic. The | |
391 | Policy modules have been categorized according to the table of | |
392 | contents in Damian Conway's book B<Perl Best Practices>. Since most | |
393 | coding standards take the form "do this..." or "don't do that...", I | |
394 | have adopted the convention of naming each module C<RequireSomething> | |
395 | or C<ProhibitSomething>. See the documentation of each module for | |
396 | it's specific details. | |
397 | ||
398 | =head2 L<Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr> | |
399 | ||
400 | Use 4-argument C<substr> instead of writing C<substr($foo, 2, 6) = $bar> | |
401 | ||
402 | =head2 L<Perl::Critic::Policy::BuiltinFunctions::ProhibitSleepViaSelect> | |
403 | ||
404 | Use L<Time::HiRes> instead of C<select(undef, undef, undef, .05)> | |
405 | ||
406 | =head2 L<Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval> | |
407 | ||
408 | Write C<eval { my $foo; bar($foo) }> instead of C<eval "my $foo; bar($foo);"> | |
409 | ||
410 | =head2 L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep> | |
411 | ||
412 | Write C<grep { $_ =~ /$pattern/ } @list> instead of C<grep /$pattern/, @list> | |
413 | ||
414 | =head2 L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap> | |
415 | ||
416 | Write C<map { $_ =~ /$pattern/ } @list> instead of C<map /$pattern/, @list> | |
417 | ||
418 | =head2 L<Perl::Critic::Policy::BuiltinFunctions::RequireGlobFunction> | |
419 | ||
420 | Use C<glob q{*}> instead of <*> | |
421 | ||
422 | =head2 L<Perl::Critic::Policy::ClassHierarchies::ProhibitOneArgBless> | |
423 | ||
424 | Write C<bless {}, $class;> instead of just C<bless {};> | |
425 | ||
426 | =head2 L<Perl::Critic::Policy::CodeLayout::ProhibitHardTabs> | |
427 | ||
428 | Use spaces instead of tabs | |
429 | ||
430 | =head2 L<Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins> | |
431 | ||
432 | Write C<open $handle, $path> instead of C<open($handle, $path)> | |
433 | ||
434 | =head2 L<Perl::Critic::Policy::CodeLayout::ProhibitQuotedWordLists> | |
435 | ||
436 | Write C< qw(foo bar baz) > instead of C< ('foo', 'bar', 'baz') > | |
437 | ||
438 | =head2 L<Perl::Critic::Policy::CodeLayout::RequireTidyCode> | |
439 | ||
440 | Must run code through L<perltidy> | |
441 | ||
442 | =head2 L<Perl::Critic::Policy::CodeLayout::RequireTrailingCommas> | |
443 | ||
444 | Put a comma at the end of every multi-line list declaration, including the last one | |
445 | ||
446 | =head2 L<Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse> | |
447 | ||
448 | Don't write long "if-elsif-elsif-elsif-elsif...else" chains | |
449 | ||
450 | =head2 L<Perl::Critic::Policy::ControlStructures::ProhibitCStyleForLoops> | |
451 | ||
452 | Write C<for(0..20)> instead of C<for($i=0; $i<=20; $i++)> | |
453 | ||
454 | =head2 L<Perl::Critic::Policy::ControlStructures::ProhibitPostfixControls> | |
455 | ||
456 | Write C<if($condition){ do_something() }> instead of C<do_something() if $condition> | |
457 | ||
458 | =head2 L<Perl::Critic::Policy::ControlStructures::ProhibitUnlessBlocks> | |
459 | ||
460 | Write C<if(! $condition)> instead of C<unless($condition)> | |
461 | ||
462 | =head2 L<Perl::Critic::Policy::ControlStructures::ProhibitUntilBlocks> | |
463 | ||
464 | Write C<while(! $condition)> instead of C<until($condition)> | |
465 | ||
466 | =head2 L<Perl::Critic::Policy::InputOutput::ProhibitBacktickOperators> | |
467 | ||
468 | Discourage stuff like C<@files = `ls $directory`> | |
469 | ||
470 | =head2 L<Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles> | |
471 | ||
472 | Write C<open my $fh, q{<}, $filename;> instead of C<open FH, q{<}, $filename;> | |
473 | ||
474 | =head2 L<Perl::Critic::Policy::InputOutput::ProhibitOneArgSelect> | |
475 | ||
476 | Never write C<select($fh)> | |
477 | ||
478 | =head2 L<Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen> | |
479 | ||
480 | Write C<open $fh, q{<}, $filename;> instead of C<open $fh, "<$filename";> | |
481 | ||
482 | =head2 L<Perl::Critic::Policy::Miscellanea::RequireRcsKeywords> | |
483 | ||
484 | Put source-control keywords in every file. | |
485 | ||
486 | =head2 L<Perl::Critic::Policy::Modules::ProhibitMultiplePackages> | |
487 | ||
488 | Put packages (especially subclasses) in separate files | |
489 | ||
490 | =head2 L<Perl::Critic::Policy::Modules::RequireBarewordIncludes> | |
491 | ||
492 | Write C<require Module> instead of C<require 'Module.pm'> | |
493 | ||
494 | =head2 L<Perl::Critic::Policy::Modules::ProhibitSpecificModules> | |
495 | ||
496 | Don't use evil modules | |
497 | ||
498 | =head2 L<Perl::Critic::Policy::Modules::RequireExplicitPackage> | |
499 | ||
500 | Always make the C<package> explicit | |
501 | ||
502 | =head2 L<Perl::Critic::Policy::Modules::RequireVersionVar> | |
503 | ||
504 | Give every module a C<$VERSION> number | |
505 | ||
506 | =head2 L<Perl::Critic::Policy::RegularExpressions::RequireLineBoundaryMatching> | |
507 | ||
508 | Always use the C</m> modifier with regular expressions | |
509 | ||
510 | =head2 L<Perl::Critic::Policy::RegularExpressions::RequireExtendedFormatting> | |
511 | ||
512 | Always use the C</x> modifier with regular expressions | |
513 | ||
514 | =head2 L<Perl::Critic::Policy::NamingConventions::ProhibitMixedCaseSubs> | |
515 | ||
516 | Write C<sub my_function{}> instead of C<sub MyFunction{}> | |
517 | ||
518 | =head2 L<Perl::Critic::Policy::NamingConventions::ProhibitMixedCaseVars> | |
519 | ||
520 | Write C<$my_variable = 42> instead of C<$MyVariable = 42> | |
521 | ||
522 | =head2 L<Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms> | |
523 | ||
524 | Don't declare your own C<open> function. | |
525 | ||
526 | =head2 L<Perl::Critic::Policy::Subroutines::ProhibitExplicitReturnUndef> | |
527 | ||
528 | Return failure with bare C<return> instead of C<return undef> | |
529 | ||
530 | =head2 L<Perl::Critic::Policy::Subroutines::ProhibitSubroutinePrototypes> | |
531 | ||
532 | Don't write C<sub my_function (@@) {}> | |
533 | ||
534 | =head2 L<Perl::Critic::Policy::TestingAndDebugging::RequirePackageStricture> | |
535 | ||
536 | Always C<use strict> | |
537 | ||
538 | =head2 L<Perl::Critic::Policy::TestingAndDebugging::RequirePackageWarnings> | |
539 | ||
540 | Always C<use warnings> | |
541 | ||
542 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitConstantPragma> | |
543 | ||
544 | Don't C< use constant $FOO => 15 > | |
545 | ||
546 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes> | |
547 | ||
548 | Write C<q{}> instead of C<''> | |
549 | ||
550 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals> | |
551 | ||
552 | Always use single quotes for literal strings. | |
553 | ||
554 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros> | |
555 | ||
556 | Write C<oct(755)> instead of C<0755> | |
557 | ||
558 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitNoisyQuotes> | |
559 | ||
560 | Use C<q{}> or C<qq{}> instead of quotes for awkward-looking strings | |
561 | ||
562 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars> | |
563 | ||
564 | Warns that you might have used single quotes when you really wanted double-quotes. | |
565 | ||
566 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::RequireNumberSeparators> | |
567 | ||
568 | Write C< 141_234_397.0145 > instead of C< 141234397.0145 > | |
569 | ||
570 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::RequireQuotedHeredocTerminator> | |
571 | ||
572 | Write C< print <<'THE_END' > or C< print <<"THE_END" > | |
573 | ||
574 | =head2 L<Perl::Critic::Policy::ValuesAndExpressions::RequireUpperCaseHeredocTerminator> | |
575 | ||
576 | Write C< <<'THE_END'; > instead of C< <<'theEnd'; > | |
577 | ||
578 | =head2 L<Perl::Critic::Policy::Variables::ProhibitLocalVars> | |
579 | ||
580 | Use C<my> instead of C<local>, except when you have to. | |
581 | ||
582 | =head2 L<Perl::Critic::Policy::Variables::ProhibitPackageVars> | |
583 | ||
584 | Eliminate globals declared with C<our> or C<use vars> | |
585 | ||
586 | =head2 L<Perl::Critic::Policy::Variables::ProhibitPunctuationVars> | |
587 | ||
588 | Write C<$EVAL_ERROR> instead of C<$@> | |
589 | ||
590 | =head1 BENDING THE RULES | |
591 | ||
592 | B<NOTE:> This feature changed in version 0.09 and is not backward | |
593 | compatible with earlier versions. | |
594 | ||
595 | Perl::Critic takes a hard-line approach to your code: either you | |
596 | comply or you don't. In the real world, it is not always practical | |
597 | (or even possible) to fully comply with coding standards. In such | |
598 | cases, it is wise to show that you are knowingly violating the | |
599 | standards and that you have a Damn Good Reason (DGR) for doing so. | |
600 | ||
601 | To help with those situations, you can direct Perl::Critic to ignore | |
602 | certain lines or blocks of code by using pseudo-pragmas: | |
603 | ||
604 | require 'LegacyLibaray1.pl'; ## no critic | |
605 | require 'LegacyLibrary2.pl'; ## no critic | |
606 | ||
607 | for my $element (@list) { | |
608 | ||
609 | ## no critic | |
610 | ||
611 | $foo = ""; #Violates 'ProhibitEmptyQuotes' | |
612 | $barf = bar() if $foo; #Violates 'ProhibitPostfixControls' | |
613 | #Some more evil code... | |
614 | ||
615 | ## use critic | |
616 | ||
617 | #Some good code... | |
618 | do_something($_); | |
619 | } | |
620 | ||
621 | The C<"## no critic"> comments direct Perl::Critic to overlook the | |
622 | remaining lines of code until the end of the current block, or until a | |
623 | C<"## use critic"> comment is found (whichever comes first). If the | |
624 | C<"## no critic"> comment is on the same line as a code statement, | |
625 | then only that line of code is overlooked. To direct perlcritic to | |
626 | ignore the C<"## no critic"> comments, use the C<-force> option. | |
627 | ||
628 | Use this feature wisely. C<"## no critic"> should be used in the | |
629 | smallest possible scope, or only on individual lines of code. If | |
630 | Perl::Critic complains about your code, try and find a compliant | |
631 | solution before resorting to this feature. | |
632 | ||
633 | =head1 EXTENDING THE CRITIC | |
634 | ||
635 | The modular design of Perl::Critic is intended to facilitate the | |
636 | addition of new Policies. To create a new Policy, make a subclass of | |
637 | L<Perl::Critic::Policy> and override the C<violates()> method. Your | |
638 | module should go somewhere in the Perl::Critic::Policy namespace. To | |
639 | use the new Policy, just add it to your F<.perlcriticrc> file. You'll | |
640 | need to have some understanding of L<PPI>, but most Policy modules are | |
641 | pretty straightforward and only require about 20 lines of code. | |
642 | ||
643 | If you develop any new Policy modules, feel free to send them to | |
644 | <thaljef@cpan.org> and I'll be happy to put them into the Perl::Critic | |
645 | distribution. | |
646 | ||
647 | =head1 IMPORTANT CHANGES | |
648 | ||
649 | As new Policy modules were added to Perl::Critic, the overall | |
650 | performance started to deteriorate rapidly. Since each module would | |
651 | traverse the document (several times for some modules), a lot of time | |
652 | was spent iterating over the same document nodes. So starting in | |
653 | version 0.11, I have switched to a stream-based approach where the | |
654 | document is traversed once and every Policy module is tested at each | |
655 | node. The result is roughly a 300% improvement. | |
656 | ||
657 | Unfortunately, Policy modules prior to version 0.11 won't be | |
658 | compatible. Hopefully, few people have started creating their own | |
659 | Policy modules. Converting them to the stream-based model is fairly | |
660 | easy, and actually results in somewhat cleaner code. Look at the | |
661 | ControlStrucutres::* modules for some examples. | |
662 | ||
663 | =head1 PREREQUISITES | |
664 | ||
665 | Perl::Critic requires the following modules: | |
666 | ||
667 | L<PPI> | |
668 | ||
669 | L<Config::Tiny> | |
670 | ||
671 | L<File::Spec> | |
672 | ||
673 | L<List::Util> | |
674 | ||
675 | L<List::MoreUtils> | |
676 | ||
677 | L<Pod::Usage> | |
678 | ||
679 | L<Pod::PlainText> | |
680 | ||
681 | L<IO::String> | |
682 | ||
683 | L<String::Format> | |
684 | ||
685 | The following modules are optional, but recommended for complete | |
686 | testing: | |
687 | ||
688 | L<Test::Pod> | |
689 | ||
690 | L<Test::Pod::Coverage> | |
691 | ||
692 | L<Test::Perl::Critic> | |
693 | ||
694 | =head1 BUGS | |
695 | ||
696 | Scrutinizing Perl code is hard for humans, let alone machines. If you | |
697 | find any bugs, particularly false-positives or false-negatives from a | |
698 | Perl::Critic::Policy, please submit them to | |
699 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Critic>. Thanks. | |
700 | ||
701 | =head1 CREDITS | |
702 | ||
703 | Adam Kennedy - For creating L<PPI>, the heart and soul of Perl::Critic. | |
704 | ||
705 | Damian Conway - For writing B<Perl Best Practices> | |
706 | ||
707 | Giuseppe Maxia - For all the great ideas and enhancements. | |
708 | ||
709 | Chris Dolan - For numerous bug reports and suggestions. | |
710 | ||
711 | Sharon, my wife - For putting up with my all-night code sessions | |
712 | ||
713 | =head1 AUTHOR | |
714 | ||
715 | Jeffrey Ryan Thalhammer <thaljef@cpan.org> | |
716 | ||
717 | =head1 COPYRIGHT | |
718 | ||
719 | Copyright (c) 2005 Jeffrey Ryan Thalhammer. All rights reserved. | |
720 | ||
721 | This program is free software; you can redistribute it and/or modify | |
722 | it under the same terms as Perl itself. The full text of this license | |
723 | can be found in the LICENSE file included with this module. | |
724 | ||
725 | =cut |