| 1 |
NAME |
| 2 |
criticism - Perl pragma to enforce coding standards and best-practices |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use criticism; |
| 6 |
|
| 7 |
use criticism 'gentle'; |
| 8 |
use criticism 'stern'; |
| 9 |
use criticism 'harsh'; |
| 10 |
use criticism 'cruel'; |
| 11 |
use criticism 'brutal'; |
| 12 |
|
| 13 |
use criticism ( -profile => '/foo/bar/perlcriticrc' ); |
| 14 |
use criticism ( -severity => 3, -verbose => '%m at %f line %l' ); |
| 15 |
|
| 16 |
DESCRIPTION |
| 17 |
This pragma enforces coding standards and promotes best-practices by |
| 18 |
running your file through Perl::Critic before every execution. In a |
| 19 |
production system, this usually isn't feasible because it adds a lot of |
| 20 |
overhead at start-up. If you have a separate development environment, |
| 21 |
you can effectively bypass the "criticism" pragma by not installing |
| 22 |
Perl::Critic in the production environment. If Perl::Critic can't be |
| 23 |
loaded, then "criticism" just fails silently. |
| 24 |
|
| 25 |
Alternatively, the "perlcritic" command-line (which is distributed with |
| 26 |
Perl::Critic) can be used to analyze your files on-demand and has some |
| 27 |
additional configuration features. And Test::Perl::Critic provides a |
| 28 |
nice interface for analyzing files during the build process. |
| 29 |
|
| 30 |
If you'd like to try Perl::Critic without installing anything, there is |
| 31 |
a web-service available at <http://perlcritic.com>. The web-service does |
| 32 |
not yet support all the configuration features that are available in the |
| 33 |
native Perl::Critic API, but it should give you a good idea of what it |
| 34 |
does. You can also invoke the perlcritic web-service from the command |
| 35 |
line by doing an HTTP-post, such as one of these: |
| 36 |
|
| 37 |
$> POST http://perlcritic.com/perl/critic.pl < MyModule.pm |
| 38 |
$> lwp-request -m POST http://perlcritic.com/perl/critic.pl < MyModule.pm |
| 39 |
$> wget -q -O - --post-file=MyModule.pm http://perlcritic.com/perl/critic.pl |
| 40 |
|
| 41 |
Please note that the perlcritic web-service is still alpha code. The URL |
| 42 |
and interface to the service are subject to change. |
| 43 |
|
| 44 |
CONFIGURATION |
| 45 |
If there is exactly one import argument, then it is taken to be a named |
| 46 |
equivalent to one of the numeric severity levels supported by |
| 47 |
Perl::Critic. For example, "use criticism 'gentle';" is equivalent to |
| 48 |
setting the "-severity => 5", which reports only the most dangerous |
| 49 |
violations. On the other hand, "use criticism 'brutal';" is like setting |
| 50 |
the "-severity => 1", which reports every violation. If there are no |
| 51 |
import arguments, then it defaults to 'gentle'. |
| 52 |
|
| 53 |
If there is more than one import argument, then they will all be passed |
| 54 |
directly into the Perl::Critic constructor. So you can use whatever |
| 55 |
arguments are supported by Perl::Critic. |
| 56 |
|
| 57 |
The "criticism" pragma will also obey whatever configurations you have |
| 58 |
set in your .perlcriticrc file. In particular, setting the |
| 59 |
"criticism-fatal" option to a true value will cause your program to |
| 60 |
immediately "die" if any Perl::Critic violations are found. Otherwise, |
| 61 |
violations are merely advisory. This option can be set in the global |
| 62 |
section at the top of your .perlcriticrc file, like this: |
| 63 |
|
| 64 |
# Top of your .perlcriticrc file... |
| 65 |
criticism-fatal = 1 |
| 66 |
|
| 67 |
# per-policy configurations follow... |
| 68 |
|
| 69 |
You can also pass "('-criticism-fatal' => 1)" as import arguments, just |
| 70 |
like any other Perl::Critic argument. See "CONFIGURATION" in |
| 71 |
Perl::Critic for details on the other configuration options. |
| 72 |
|
| 73 |
DIAGNOSTICS |
| 74 |
Usually, the "criticism" pragma fails silently if it cannot load |
| 75 |
Perl::Critic. So by not installing Perl::Critic in your production |
| 76 |
environment, you can leave the "criticism" pragma in your production |
| 77 |
source code and it will still compile, but it won't be analyzed by |
| 78 |
Perl::Critic each time it runs. |
| 79 |
|
| 80 |
However, if you set the "DEBUG" environment variable to a true value or |
| 81 |
run your program under the Perl debugger, you will get a warning when |
| 82 |
"criticism" fails to load Perl::Critic. |
| 83 |
|
| 84 |
NOTES |
| 85 |
The "criticism" pragma applies to the entire file, so it is not affected |
| 86 |
by scope or package boundaries and "use"-ing it multiple times will just |
| 87 |
cause it to repeatedly process the same file. There isn't a reciprocal |
| 88 |
"no criticism" pragma. However, Perl::Critic does support a |
| 89 |
pseudo-pragma that directs it to overlook certain lines or blocks of |
| 90 |
code. See "BENDING THE RULES" in Perl::Critic for more details. |
| 91 |
|
| 92 |
AUTHOR |
| 93 |
Jeffrey Ryan Thalhammer <[email protected]> |
| 94 |
|
| 95 |
COPYRIGHT |
| 96 |
Copyright (c) 2006-2007 Jeffrey Ryan Thalhammer. All rights reserved. |
| 97 |
|
| 98 |
This program is free software; you can redistribute it and/or modify it |
| 99 |
under the same terms as Perl itself. The full text of this license can |
| 100 |
be found in the LICENSE file included with this module. |
| 101 |
|