NAME
Perl::Critic::Config - The final derived Perl::Critic configuration, combined from any profile file and command-line parameters.
DESCRIPTION
Perl::Critic::Config takes care of finding and processing user-preferences for Perl::Critic. The Config object defines which Policy modules will be loaded into the Perl::Critic engine and how they should be configured. You should never really need to instantiate Perl::Critic::Config directly because the Perl::Critic constructor will do it for you.
INTERFACE SUPPORT
This is considered to be a non-public class. Its interface is subject to change without notice.
CONSTRUCTOR
new( [ -profile => $FILE, -severity => $N, -theme => $string, -include => \@PATTERNS, -exclude => \@PATTERNS, -single-policy => $PATTERN, -top => $N, -only => $B, -profile-strictness => $PROFILE_STRICTNESS_{WARN|FATAL|QUIET}, -force => $B, -verbose => $N, -color => $B, -criticism-fatal => $B] )
new()
-
Returns a reference to a new Perl::Critic::Config object. The default value for all arguments can be defined in your .perlcriticrc file. See the "CONFIGURATION" section for more information about that. All arguments are optional key-value pairs as follows:
-profile is a path to a configuration file. If
$FILE
is not defined, Perl::Critic::Config attempts to find a .perlcriticrc configuration file in the current directory, and then in your home directory. Alternatively, you can set thePERLCRITIC
environment variable to point to a file in another location. If a configuration file can't be found, or if$FILE
is an empty string, then all Policies will be loaded with their default configuration. See "CONFIGURATION" for more information.-severity is the minimum severity level. Only Policy modules that have a severity greater than
$N
will be loaded into this Config. Severity values are integers ranging from 1 (least severe) to 5 (most severe). The default is 5. For a given-profile
, decreasing the-severity
will usually result in more Policy violations. Users can redefine the severity level for any Policy in their .perlcriticrc file. See "CONFIGURATION" for more information.-theme is special string that defines a set of Policies based on their respective themes. If
-theme
is given, only policies that are members of that set will be loaded. See the "POLICY THEMES" section for more information about themes. Unless the-severity
option is explicitly given, setting-theme
causes the-severity
to be set to 1.-include is a reference to a list of string
@PATTERNS
. Policies that match at least onem/$PATTERN/ixms
will be loaded into this Config, irrespective of the severity settings. You can use it in conjunction with the-exclude
option. Note that-exclude
takes precedence over-include
when a Policy matches both patterns.-exclude is a reference to a list of string
@PATTERNS
. Polices that match at least onem/$PATTERN/ixms
will not be loaded into this Config, irrespective of the severity settings. You can use it in conjunction with the-include
option. Note that-exclude
takes precedence over-include
when a Policy matches both patterns.-single-policy is a string
PATTERN
. Only the policy that matchesm/$PATTERN/ixms
will be used. This value overrides the-severity
,-theme
,-include
,-exclude
, and-only
options.-top is the maximum number of Violations to return when ranked by their severity levels. This must be a positive integer. Violations are still returned in the order that they occur within the file. Unless the
-severity
option is explicitly given, setting-top
silently causes the-severity
to be set to 1.-only is a boolean value. If set to a true value, Perl::Critic will only choose from Policies that are mentioned in the user's profile. If set to a false value (which is the default), then Perl::Critic chooses from all the Policies that it finds at your site.
-profile-strictness is an enumerated value, one of "$PROFILE_STRICTNESS_WARN" in Perl::Critic::Utils::Constants (the default), "$PROFILE_STRICTNESS_FATAL" in Perl::Critic::Utils::Constants, and "$PROFILE_STRICTNESS_QUIET" in Perl::Critic::Utils::Constants. If set to "$PROFILE_STRICTNESS_FATAL" in Perl::Critic::Utils::Constants, Perl::Critic will make certain warnings about problems found in a .perlcriticrc or file specified via the -profile option fatal. For example, Perl::Critic normally only
warn
s about profiles referring to non-existent Policies, but this value makes this situation fatal. Correspondingly, "$PROFILE_STRICTNESS_QUIET" in Perl::Critic::Utils::Constants makes Perl::Critic shut up about these things.-force controls whether Perl::Critic observes the magical
"## no critic"
annotations in your code. If set to a true value, Perl::Critic will analyze all code. If set to a false value (which is the default) Perl::Critic will ignore code that is tagged with these comments. See "BENDING THE RULES" in Perl::Critic for more information.-verbose can be a positive integer (from 1 to 10), or a literal format specification. See Perl::Critic::Violations for an explanation of format specifications.
-color and -pager are not used by Perl::Critic but is provided for the benefit of perlcritic.
-criticism-fatal is not used by Perl::Critic but is provided for the benefit of criticism.
-color-severity-highest is a string representing the highest severity violation color, as expected by Term::ANSIColor. It is not used by Perl::Critic, but is provided for the benefit of perlcritic. It can also be specified as -colour-severity-highest, -color-severity-5, or -colour-severity-5.
-color-severity-high is a string representing the high severity violation color, as expected by Term::ANSIColor. It is not used by Perl::Critic, but is provided for the benefit of perlcritic. It can also be specified as -colour-severity-high, -color-severity-4, or -colour-severity-4.
-color-severity-medium is a string representing the medium severity violation color, as expected by Term::ANSIColor. It is not used by Perl::Critic, but is provided for the benefit of perlcritic. It can also be specified as -colour-severity-medium, -color-severity-3, or -colour-severity-3.
-color-severity-low is a string representing the low severity violation color, as expected by Term::ANSIColor. It is not used by Perl::Critic, but is provided for the benefit of perlcritic. It can also be specified as -colour-severity-low, -color-severity-2, or -colour-severity-2.
-color-severity-lowest is a string representing the lowest severity violation color, as expected by Term::ANSIColor. It is not used by Perl::Critic, but is provided for the benefit of perlcritic. It can also be specified as -colour-severity-lowest, -color-severity-1, or -colour-severity-1.
METHODS
add_policy( -policy => $policy_name, -params => \%param_hash )
-
Creates a Policy object and loads it into this Config. If the object cannot be instantiated, it will throw a fatal exception. Otherwise, it returns a reference to this Critic.
-policy is the name of a Perl::Critic::Policy subclass module. The
'Perl::Critic::Policy'
portion of the name can be omitted for brevity. This argument is required.-params is an optional reference to a hash of Policy parameters. The contents of this hash reference will be passed into to the constructor of the Policy module. See the documentation in the relevant Policy module for a description of the arguments it supports.
all_policies_enabled_or_not()
-
Returns a list containing references to all the Policy objects that have been seen. Note that the state of these objects is not trustworthy. In particular, it is likely that some of them are not prepared to examine any documents.
policies()
-
Returns a list containing references to all the Policy objects that have been enabled and loaded into this Config.
exclude()
-
Returns the value of the
-exclude
attribute for this Config. include()
-
Returns the value of the
-include
attribute for this Config. force()
-
Returns the value of the
-force
attribute for this Config. only()
-
Returns the value of the
-only
attribute for this Config. profile_strictness()
-
Returns the value of the
-profile-strictness
attribute for this Config. severity()
-
Returns the value of the
-severity
attribute for this Config. single_policy()
-
Returns the value of the
-single-policy
attribute for this Config. theme()
-
Returns the Perl::Critic::Theme object that was created for this Config.
top()
-
Returns the value of the
-top
attribute for this Config. verbose()
-
Returns the value of the
-verbose
attribute for this Config. color()
-
Returns the value of the
-color
attribute for this Config. pager()
-
Returns the value of the
-pager
attribute for this Config. criticism_fatal()
-
Returns the value of the
-criticsm-fatal
attribute for this Config. color_severity_highest()
-
Returns the value of the
-color-severity-highest
attribute for this Config. color_severity_high()
-
Returns the value of the
-color-severity-high
attribute for this Config. color_severity_medium()
-
Returns the value of the
-color-severity-medium
attribute for this Config. color_severity_low()
-
Returns the value of the
-color-severity-low
attribute for this Config. color_severity_lowest()
-
Returns the value of the
-color-severity-lowest
attribute for this Config.
SUBROUTINES
Perl::Critic::Config has a few static subroutines that are used internally, but may be useful to you in some way.
site_policy_names()
-
Returns a list of all the Policy modules that are currently installed in the Perl::Critic:Policy namespace. These will include modules that are distributed with Perl::Critic plus any third-party modules that have been installed.
CONFIGURATION
Most of the settings for Perl::Critic and each of the Policy modules can be controlled by a configuration file. The default configuration file is called .perlcriticrc. Perl::Critic::Config will look for this file in the current directory first, and then in your home directory. Alternatively, you can set the PERLCRITIC
environment variable to explicitly point to a different file in another location. If none of these files exist, and the -profile
option is not given to the constructor, then all Policies will be loaded with their default configuration.
The format of the configuration file is a series of INI-style blocks that contain key-value pairs separated by '='. Comments should start with '#' and can be placed on a separate line or after the name-value pairs if you desire.
Default settings for Perl::Critic itself can be set before the first named block. For example, putting any or all of these at the top of your configuration file will set the default value for the corresponding Perl::Critic constructor argument.
severity = 3 #Integer from 1 to 5
only = 1 #Zero or One
force = 0 #Zero or One
verbose = 4 #Integer or format spec
top = 50 #A positive integer
theme = risky + (pbp * security) - cosmetic #A theme expression
include = NamingConventions ClassHierarchies #Space-delimited list
exclude = Variables Modules::RequirePackage #Space-delimited list
color = 1 #Zero or One
color-severity-highest = bold red #Term::ANSIColor
color-severity-high = magenta #Term::ANSIColor
color-severity-medium = #no coloring
color-severity-low = #no coloring
color-severity-lowest = #no coloring
The remainder of the configuration file is a series of blocks like this:
[Perl::Critic::Policy::Category::PolicyName]
severity = 1
set_themes = foo bar
add_themes = baz
arg1 = value1
arg2 = value2
Perl::Critic::Policy::Category::PolicyName
is the full name of a module that implements the policy. The Policy modules distributed with Perl::Critic have been grouped into categories according to the table of contents in Damian Conway's book Perl Best Practices. For brevity, you can omit the 'Perl::Critic::Policy'
part of the module name.
severity
is the level of importance you wish to assign to the Policy. All Policy modules are defined with a default severity value ranging from 1 (least severe) to 5 (most severe). However, you may disagree with the default severity and choose to give it a higher or lower severity, based on your own coding philosophy.
The remaining key-value pairs are configuration parameters that will be passed into the constructor of that Policy. The constructors for most Policy modules do not support arguments, and those that do should have reasonable defaults. See the documentation on the appropriate Policy module for more details.
Instead of redefining the severity for a given Policy, you can completely disable a Policy by prepending a '-' to the name of the module in your configuration file. In this manner, the Policy will never be loaded, regardless of the -severity
given to the Perl::Critic::Config constructor.
A simple configuration might look like this:
#--------------------------------------------------------------
# I think these are really important, so always load them
[TestingAndDebugging::RequireUseStrict]
severity = 5
[TestingAndDebugging::RequireUseWarnings]
severity = 5
#--------------------------------------------------------------
# I think these are less important, so only load when asked
[Variables::ProhibitPackageVars]
severity = 2
[ControlStructures::ProhibitPostfixControls]
allow = if unless #My custom configuration
severity = 2
#--------------------------------------------------------------
# Give these policies a custom theme. I can activate just
# these policies by saying (-theme => 'larry + curly')
[Modules::RequireFilenameMatchesPackage]
add_themes = larry
[TestingAndDebugging::RequireTestLables]
add_themes = curly moe
#--------------------------------------------------------------
# I do not agree with these at all, so never load them
[-NamingConventions::Capitalization]
[-ValuesAndExpressions::ProhibitMagicNumbers]
#--------------------------------------------------------------
# For all other Policies, I accept the default severity, theme
# and other parameters, so no additional configuration is
# required for them.
For additional configuration examples, see the perlcriticrc file that is included in this t/examples directory of this distribution.
THE POLICIES
A large number of Policy modules are distributed with Perl::Critic. They are described briefly in the companion document Perl::Critic::PolicySummary and in more detail in the individual modules themselves.
POLICY THEMES
Each Policy is defined with one or more "themes". Themes can be used to create arbitrary groups of Policies. They are intended to provide an alternative mechanism for selecting your preferred set of Policies. For example, you may wish disable a certain subset of Policies when analyzing test scripts. Conversely, you may wish to enable only a specific subset of Policies when analyzing modules.
The Policies that ship with Perl::Critic are have been broken into the following themes. This is just our attempt to provide some basic logical groupings. You are free to invent new themes that suit your needs.
THEME DESCRIPTION
--------------------------------------------------------------------------
core All policies that ship with Perl::Critic
pbp Policies that come directly from "Perl Best Practices"
bugs Policies that that prevent or reveal bugs
maintenance Policies that affect the long-term health of the code
cosmetic Policies that only have a superficial effect
complexity Policies that specificaly relate to code complexity
security Policies that relate to security issues
tests Policies that are specific to test scripts
Say `perlcritic -list`
to get a listing of all available policies and the themes that are associated with each one. You can also change the theme for any Policy in your .perlcriticrc file. See the "CONFIGURATION" section for more information about that.
Using the -theme
option, you can combine theme names with mathematical and boolean operators to create an arbitrarily complex expression that represents a custom "set" of Policies. The following operators are supported
Operator Alternative Meaning
----------------------------------------------------------------------------
* and Intersection
- not Difference
+ or Union
Operator precedence is the same as that of normal mathematics. You can also use parenthesis to enforce precedence. Here are some examples:
Expression Meaning
----------------------------------------------------------------------------
pbp * bugs All policies that are "pbp" AND "bugs"
pbp and bugs Ditto
bugs + cosmetic All policies that are "bugs" OR "cosmetic"
bugs or cosmetic Ditto
pbp - cosmetic All policies that are "pbp" BUT NOT "cosmetic"
pbp not cosmetic Ditto
-maintenance All policies that are NOT "maintenance"
not maintenance Ditto
(pbp - bugs) * complexity All policies that are "pbp" BUT NOT "bugs",
AND "complexity"
(pbp not bugs) and complexity Ditto
Theme names are case-insensitive. If -theme
is set to an empty string, then it is equivalent to the set of all Policies. A theme name that doesn't exist is equivalent to an empty set. Please See http://en.wikipedia.org/wiki/Set for a discussion on set theory.
SEE ALSO
Perl::Critic::OptionsProcessor, Perl::Critic::UserProfile
AUTHOR
Jeffrey Ryan Thalhammer <thaljef@cpan.org>
COPYRIGHT
Copyright (c) 2005-2009 Jeffrey Ryan Thalhammer. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.