The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Perl::Critic::Violation - Represents policy violations

SYNOPSIS

  use PPI;
  use Perl::Critic::Violation;

  my $loc  = $node->location();   #$node is a PPI::Node object
  my $desc = 'Offending code';    #Describe the violation
  my $expl = [1,45,67];           #Page numbers from PBB
  my $vio  = Perl::Critic::Violation->new($desc, $expl, $loc);

DESCRIPTION

Perl::Critic::Violation is the generic represntation of an individual Policy violation. Its primary purpose is to provide an abstraction layer so that clients of Perl::Critic don't have to know anything about PPI. The violations method of all Perl::Critic::Policy subclasses must return a list of these Perl::Critic::Violation objects.

CONSTRUCTOR

new( $description, $explanation, $location, $severity )

Retruns 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 PBB (as an ARRAY ref), and the location of the violation (as an ARRAY ref). The $location must have two elements, representing the line and column number, in that order. The $severity should be an integer ranging from 1 to 5.

METHODS

description( void )

Returns a brief description of the policy that has been volated as a string.

explanation( void )

Returns an explanation of the policy as a string or as reference to an array of page numbers in PBB.

location( void )

Returns a two-element list containing the line and column number where this Violation occurred.

severity( void )

Returns the severity of this Violation as an integer ranging from 1 to 5, where 5 is the "most" severe.

sort_by_severity( @violation_objects )

If you need to sort Violations by severity, use this handy routine:

   @sorted = Perl::Critic::Violation::sort_by_severity(@violations);
sort_by_location( @violation_objects )

If you need to sort Violations by location, use this handy routine:

   @sorted = Perl::Critic::Violation::sort_by_location(@violations);
diagnostics( void )

This feature is experimental. Returns a formatted string containing a full discussion of the motivation for and details of the Policy module that created this Violation. This information is automatically extracted from the DESCRIPTION section of the Policy module's POD.

policy( void )

Returns the name of the Perl::Critic::Policy that created this Violation.

source( void )

Returns the string of source code that caused this exception. If the code spans multiple lines (e.g. multi-line statements, subroutines or other blocks), then only the first line will be returned.

to_string( void )

Returns a string repesentation of this violation. The content of the string depends on the current value of the $FORMAT package variable. See "OVERLOADS" for the details.

FIELDS

$Perl::Critic::Violation::FORMAT

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 localize it first.

OVERLOADS

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:

  Escape    Meaning
  -------   ------------------------------------------------------------------------
  %m        Brief description of the violation
  %f        Name of the file where the violation occurred.
  %l        Line number where the violation occurred
  %c        Column number where the violation occurred
  %e        Explanation of violation or page numbers in PBP
  %d        Full diagnostic discussion of the violation
  %r        The string of source code that caused the violation
  %p        Name of the Policy module that created 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."

AUTHOR

Jeffrey Ryan Thalhammer <thaljef@cpan.org>

COPYRIGHT

Copyright (c) 2005 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.