Perl::Critic::Utils - General utility subroutines and constants for Perl::Critic and derivative distributions.
This module provides several static subs and variables that are useful for developing Perl::Critic::Policy subclasses. Unless you are writing Policy modules, you probably don't care about this package.
find_keywords( $doc, $keyword )
DEPRECATED: Since version 0.11, every Policy is evaluated at each element
of the document. So you shouldn't need to go looking for a particular
keyword. If you do want to use this, please import it via the
:deprecated
tag, rather than directly, to mark the module as needing
updating.
Given a PPI::Document as $doc
, returns a reference to an array
containing all the PPI::Token::Word elements that match $keyword
. This
can be used to find any built-in function, method call, bareword, or reserved
keyword. It will not match variables, subroutine names, literal strings,
numbers, or symbols. If the document doesn't contain any matches, returns
undef.
is_perl_global( $element )
%SIG
, %ENV
, or @ARGV
. The
sigil on the symbol is ignored, so things like $ARGV
or $ENV
will still
return true.
is_perl_builtin( $element )
is_perl_bareword( $element )
is_perl_filehandle( $element )
STDIN
, STDERR
, STDOUT
, ARGV
)
that are defined in Perl 5.8.8. Note that this function will return false if
given a filehandle that is represented as a typeglob (e.g. *STDIN
)
is_perl_builtin_with_list_context( $element )
is_perl_builtin_with_multiple_arguments( $element )
is_perl_builtin_with_no_arguments( $element )
is_perl_builtin_with_one_argument( $element )
is_perl_builtin_with_optional_argument( $element )
Given a PPI::Token::Word, PPI::Statement::Sub, or string, returns true if that token represents a call to any of the builtin functions defined in Perl 5.8.8 that takes no more than one argument.
The sets of values for which is_perl_builtin_with_multiple_arguments()
,
is_perl_builtin_with_no_arguments()
,
is_perl_builtin_with_one_argument()
, and
is_perl_builtin_with_optional_argument()
return true are disjoint and
their union is precisely the set of values that is_perl_builtin()
will
return true for.
is_perl_builtin_with_zero_and_or_one_arguments( $element )
Given a PPI::Token::Word, PPI::Statement::Sub, or string, returns true if that token represents a call to any of the builtin functions defined in Perl 5.8.8 that takes no and/or one argument.
Returns true if any of is_perl_builtin_with_no_arguments()
,
is_perl_builtin_with_one_argument()
, and
is_perl_builtin_with_optional_argument()
returns true.
is_qualified_name( $name )
precedence_of( $element )
is_hash_key( $element )
Given a PPI::Element, returns true if the element is a literal hash key. PPI doesn't distinguish between regular barewords (like keywords or subroutine calls) and barewords in hash subscripts (which are considered literal). So this subroutine is useful if your Policy is searching for PPI::Token::Word elements and you want to filter out the hash subscript variety. In both of the following examples, "foo" is considered a hash key:
$hash1{foo} = 1; %hash2 = (foo => 1);
But if the bareword is followed by an argument list, then perl treats it as a function call. So in these examples, "foo" is not considered a hash key:
$hash1{ foo() } = 1; &hash2 = (foo() => 1);
is_included_module_name( $element )
use
, require
, or no
.
is_integer( $value )
is_class_name( $element )
is_label_pointer( $element )
next
, last
, redo
, or goto
statement. Note this is not the
same thing as the label declaration.
is_method_call( $element )
is_package_declaration( $element )
is_subroutine_name( $element )
is_function_call( $element )
is_hash_key
, is_method_call
, is_subroutine_name
,
is_included_module_anme
, is_package_declaration
, is_perl_bareword
,
is_perl_filehandle
, is_label_pointer
and is_subroutine_name
all
return false for the given element.
first_arg( $element )
Given a PPI::Element that is presumed to be a function call (which is
usually a PPI::Token::Word), return the first argument. This is similar of
parse_arg_list()
and follows the same logic. Note that for the code:
int($x + 0.5)
this function will return just the $x
, not the whole expression. This is
different from the behavior of parse_arg_list()
. Another caveat is:
int(($x + $y) + 0.5)
which returns ($x + $y)
as a PPI::Structure::List instance.
parse_arg_list( $element )
split_nodes_on_comma( @nodes )
This has the same return type as parse_arg_list()
but expects to be passed
the nodes that represent the interior of a list, like:
'foo', 1, 2, 'bar'
is_script( $document )
/#!.*/
. If so, it is
judged to be a script instead of a module. See shebang_line()
.
is_in_void_context( $token )
policy_long_name( $policy_name )
policy_short_name( $policy_name )
all_perl_files( @directories )
Given a list of directories, recursively searches through all the directories (depth first) and returns a list of paths for all the files that are Perl code files. Any administrative files for CVS or Subversion are skipped, as are things that look like temporary or backup files.
A Perl code file is:
severity_to_number( $severity )
$severity
is given as an integer, this function returns $severity
but
normalized to lie between $SEVERITY_LOWEST
and $SEVERITY_HIGHEST
. If
$severity
is given as a string, this function returns the corresponding
severity number. If the string doesn't have a corresponding number, this
function will throw an exception.
is_valid_numeric_verbosity( $severity )
verbosity_to_format( $verbosity_level )
set_format
method in Perl::Critic::Violation. See the perlcritic documentation for
a listing of the predefined formats.
hashify( @list )
@list
, return a hash where @list
is in the keys and each value is
1. Duplicate values in @list
are silently squished.
interpolate( $literal )
Given a $literal
string that may contain control characters (e.g.. '\t'
'\n'), this function does a double interpolation on the string and returns it
as if it had been declared in double quotes. For example:
'foo \t bar \n' ...becomes... "foo \t bar \n"
shebang_line( $document )
#!
. If so, return that
line. Otherwise return undef.
words_from_string( $str )
is_unchecked_call( $element )
$COMMA
$FATCOMMA
$COLON
$SCOLON
$QUOTE
$DQUOTE
$BACKTICK
$PERIOD
$PIPE
$EMPTY
$SPACE
$SLASH
$BSLASH
$LEFT_PAREN
$RIGHT_PAREN
:characters
tag.
$SEVERITY_HIGHEST
$SEVERITY_HIGH
$SEVERITY_MEDIUM
$SEVERITY_LOW
$SEVERITY_LOWEST
get_severity
and default_severity
methods
of every Policy subclass must return one of these values. Can be imported via
the :severities
tag.
$DEFAULT_VERBOSITY
$DEFAULT_VERBOSITY_WITH_FILE_NAME
$DEFAULT_VERBOSITY
, but with the file name prefixed to it.
$TRUE
$FALSE
$FALSE ne $EMPTY
. Can be imported via the
:booleans
tag.
The following groups of functions and constants are available as parameters to
a use Perl::Critic::Util
statement.
:all
:booleans
$TRUE
, $FALSE
:severities
$SEVERITY_HIGHEST
,
$SEVERITY_HIGH
,
$SEVERITY_MEDIUM
,
$SEVERITY_LOW
,
$SEVERITY_LOWEST
,
@SEVERITY_NAMES
:characters
$COLON
,
$COMMA
,
$DQUOTE
,
$EMPTY
,
$FATCOMMA
,
$PERIOD
,
$PIPE
,
$QUOTE
,
$BACKTICK
,
$SCOLON
,
$SPACE
,
$SLASH
,
$BSLASH
$LEFT_PAREN
$RIGHT_PAREN
:classification
Includes:
is_function_call
,
is_hash_key
,
is_included_module_name
,
is_integer
,
is_method_call
,
is_package_declaration
,
is_perl_builtin
,
is_perl_global
,
is_perl_builtin_with_list_context
is_perl_builtin_with_multiple_arguments
is_perl_builtin_with_no_arguments
is_perl_builtin_with_one_argument
is_perl_builtin_with_optional_argument
is_perl_builtin_with_zero_and_or_one_arguments
is_script
,
is_subroutine_name
,
is_unchecked_call
is_valid_numeric_verbosity
See also Perl::Critic::Utils::PPI.
:data_conversion
Generic manipulation, not having anything specific to do with Perl::Critic.
Includes:
hashify
,
words_from_string
,
interpolate
:ppi
Things for dealing with PPI, other than classification.
Includes:
first_arg
,
parse_arg_list
See also Perl::Critic::Utils::PPI.
:internal_lookup
Translations between internal representations.
Includes:
severity_to_number
,
verbosity_to_format
:language
Information about Perl not programmatically available elsewhere.
Includes:
precedence_of
:deprecated
Not surprisingly, things that are deprecated. It is preferred to use this tag to get to these functions, rather than the function names themselves, so as to mark any module using them as needing cleanup.
Includes:
find_keywords
Perl::Critic::Utils::Constants, Perl::Critic::Utils::McCabe, Perl::Critic::Utils::PPI,
Jeffrey Ryan Thalhammer <thaljef@cpan.org>
Copyright (c) 2005-2008 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.