Perl::Critic::Policy - Base class for all Policy modules
Perl::Critic::Policy is the abstract base class for all Policy
objects. If you're developing your own Policies, your job is to
implement and override its methods in a subclass. To work with the
Perl::Critic engine, your implementation must behave as described
below. For a detailed explanation on how to make new Policy modules,
please see the Perl::Critic::DEVELOPER document included in this
distribution.
new(key1 => value1, key2 => value2 ... )
-
Returns a reference to a new subclass of Perl::Critic::Policy. If your
Policy requires any special arguments, they will be passed in here as
key-value pairs. Users of perlcritic can specify these in their
config file. Unless you override the new method, the default
method simply returns a reference to an empty hash that has been
blessed into your subclass. However, you really should not override
this; override initialize_if_enabled() instead.
This constructor is always called regardless of whether the user has
enabled this Policy or not.
initialize_if_enabled( { key1 => value1, key2 => value2 ... } )
-
This receives the same parameters as new(), but as a reference to a
hash, and is only invoked if this Policy is enabled by the user.
Thus, this is the preferred place for subclasses to do any
initialization.
Implementations of this method should return a boolean value
indicating whether the Policy should continue to be enabled. For most
subclasses, this will always be $TRUE. Policies that depend upon
external modules or other system facilities that may or may not be
available should test for the availability of these dependencies and
return $FALSE if they are not.
violates( $element, $document )
-
Given a PPI::Element and a PPI::Document, returns one or more
Perl::Critic::Violation objects if the $element violates this
Policy. If there are no violations, then it returns an empty list.
If the Policy encounters an exception, then it should croak with an
error message and let the caller decide how to handle it.
violates() is an abstract method and it will abort if you attempt
to invoke it directly. It is the heart of all Policy modules, and
your subclass must override this method.
violation( $description, $explanation, $element )
-
Returns a reference to a new Perl::Critic::Violation object. The
arguments are a description of the violation (as string), an
explanation for the policy (as string) or a series of page numbers in
PBP (as an ARRAY ref), a reference to the PPI element that caused
the violation.
These are the same as the constructor to Perl::Critic::Violation,
but without the severity. The Policy itself knows the severity.
throw_parameter_value_exception( $option_name, $option_value, $source, $message_suffix )
-
Create and throw a
Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue.
Useful in parameter parser implementations.
get_long_name()
-
Return the full package name of this policy.
get_short_name()
-
Return the name of this policy without the "Perl::Critic::Policy::"
prefix.
applies_to()
-
Returns a list of the names of PPI classes that this Policy cares
about. By default, the result is
PPI::Element. Overriding this
method in Policy subclasses should lead to significant performance
increases.
default_severity()
-
Returns the default severity for violating this Policy. See the
$SEVERITY constants in Perl::Critic::Utils for an enumeration of
possible severity values. By default, this method returns
$SEVERITY_LOWEST. Authors of Perl::Critic::Policy subclasses
should override this method to return a value that they feel is
appropriate for their Policy. In general, Polices that are widely
accepted or tend to prevent bugs should have a higher severity than
those that are more subjective or cosmetic in nature.
get_severity()
-
Returns the severity of violating this Policy. If the severity has
not been explicitly defined by calling
set_severity, then the
default_severity is returned. See the $SEVERITY constants in
Perl::Critic::Utils for an enumeration of possible severity values.
set_severity( $N )
-
Sets the severity for violating this Policy. Clients of
Perl::Critic::Policy objects can call this method to assign a
different severity to the Policy if they don't agree with the
default_severity. See the $SEVERITY constants in
Perl::Critic::Utils for an enumeration of possible values.
default_themes()
-
Returns a sorted list of the default themes associated with this
Policy. The default method returns an empty list. Policy authors
should override this method to return a list of themes that are
appropriate for their policy.
get_themes()
-
Returns a sorted list of the themes associated with this Policy. If
you haven't added themes or set the themes explicitly, this method
just returns the default themes.
set_themes( @THEME_LIST )
-
Sets the themes associated with this Policy. Any existing themes are
overwritten. Duplicate themes will be removed.
add_themes( @THEME_LIST )
-
Appends additional themes to this Policy. Any existing themes are
preserved. Duplicate themes will be removed.
parameter_metadata_available()
-
Returns whether information about the parameters is available.
get_parameters()
-
Returns a reference to an array containing instances of
Perl::Critic::PolicyParameter.
Note that this will return an empty list if the parameters for this
policy are unknown. In order to differentiate between this
circumstance and the one where this policy does not take any
parameters, it is necessary to call parameter_metadata_available().
get_parameter( $parameter_name )
-
Returns the Perl::Critic::PolicyParameter with the specified name.
set_format( $FORMAT )
-
Class method. Sets the format for all Policy objects when they are
evaluated in string context. The default is
"%p\n". See
"OVERLOADS" for formatting options.
get_format()
-
Class method. Returns the current format for all Policy objects when
they are evaluated in string context.
to_string()
-
Returns a string representation of the policy. The content of the
string depends on the current value of the
$FORMAT package
variable. See "OVERLOADS" for the details.
When your Policy module first uses Perl::Critic::Violation, it
will try and extract the DESCRIPTION section of your Policy module's
POD. This information is displayed by Perl::Critic if the verbosity
level is set accordingly. Therefore, please include a DESCRIPTION
section in the POD for any Policy modules that you author. Thanks.
Perl::Critic::Violation overloads the "" operator to produce neat
little messages when evaluated in string context. The format depends
on the current value of the $FORMAT package variable.
Formats are a combination of literal and escape characters similar to
the way sprintf works. If you want to know the specific formatting
capabilities, look at String::Format. Valid escape characters are:
%O
-
List of supported policy parameters. Takes an option of a format
string for Perl::Critic::PolicyParameter/"to_formatted_string".
For example, this can be used like
%{%n - %d\n}O to get a list of
parameter names followed by their descriptions.
%U
-
A message stating that the parameters for the policy are unknown if
parameter_metadata_available() returns false. Takes an option of
what the message should be, which defaults to "Cannot programmatically
discover what parameters this policy takes.". The value of this
option is interpolated in order to expand the standard escape
sequences (\n, \t, etc.).
%P
-
Name of the Policy module.
%p
-
Name of the Policy without the
Perl::Critic::Policy:: prefix.
%S
-
The default severity level of the policy.
%s
-
The current severity level of the policy.
%T
-
The default themes for the policy.
%t
-
The current themes for the policy.
Jeffrey Ryan Thalhammer <thaljef@cpan.org>
Copyright (c) 2005-2007 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.