Perl::Critic::Violation - Represents policy violations |
Perl::Critic::Violation - Represents policy violations
use PPI; use Perl::Critic::Violation;
my $elem = $doc->child(0); #$doc is a PPI::Document object my $desc = 'Offending code'; #Describe the violation my $expl = [1,45,67]; #Page numbers from PBP my $sev = 5; #Severity level of this violation
my $vio = Perl::Critic::Violation->new($desc, $expl, $node, $sev);
Perl::Critic::Violation is the generic representation of an individual
Policy violation. Its primary purpose is to provide an abstraction
layer so that clients of the Perl::Critic manpage don't have to know anything
about PPI. The violations
method of all the Perl::Critic::Policy manpage
subclasses must return a list of these Perl::Critic::Violation
objects.
new( $description, $explanation, $element, $severity )
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, and the severity of the violation (as an integer).
description()
explanation()
location()
filename()
severity()
sort_by_severity( @violation_objects )
@sorted = Perl::Critic::Violation::sort_by_severity(@violations);
sort_by_location( @violation_objects )
@sorted = Perl::Critic::Violation::sort_by_location(@violations);
diagnostics()
DESCRIPTION
section of the Policy module's POD.
policy()
source()
set_format( $FORMAT )
'%d at line %l, column
%c. %e'
. See OVERLOADS for formatting options.
get_format()
to_string()
$FORMAT
package
variable. See OVERLOADS for the details.
$Perl::Critic::Violation::FORMAT
set_format
and get_format
methods instead.
Sets the format for all Violation objects when they are evaluated in string
context. The default is '%d at line %l, column %c. %e'
. See
OVERLOADS for formatting options. If you want to change $FORMAT
, you
should probably localize it first.
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 the String::Format manpage. Valid escape characters are:
Escape Meaning ------- ---------------------------------------------------------------- %c Column number where the violation occurred %d Full diagnostic discussion of the violation %e Explanation of violation or page numbers in PBP %F Just the name of the file where the violation occurred. %f Path to the file where the violation occurred. %l Line number where the violation occurred %m Brief description of the violation %P Full name of the Policy module that created the violation %p Name of the Policy without the Perl::Critic::Policy:: prefix %r The string of source code that caused the violation %s The severity level of the violation
Here are some examples:
$Perl::Critic::Violation::FORMAT = "%m at line %l, column %c.\n"; #looks like "Mixed case variable name at line 6, column 23."
$Perl::Critic::Violation::FORMAT = "%m near '%r'\n"; #looks like "Mixed case variable name near 'my $theGreatAnswer = 42;'"
$Perl::Critic::Violation::FORMAT = "%l:%c:%p\n"; #looks like "6:23:NamingConventions::ProhibitMixedCaseVars"
$Perl::Critic::Violation::FORMAT = "%m at line %l. %e. \n%d\n"; #looks like "Mixed case variable name at line 6. See page 44 of PBP. Conway's recommended naming convention is to use lower-case words separated by underscores. Well-recognized acronyms can be in ALL CAPS, but must be separated by underscores from other parts of the name."
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.
Perl::Critic::Violation - Represents policy violations |