Perl::...::Utils(3pm)
π NAME π
okay so this is the User Contributed Perl Documentation page for Perl::Critic::Utils(3pm) β general utility subroutines and constants for Perl::Critic and derivative distributions. Thatβs the whole NAME vibe, bestie. π
π DESCRIPTION π
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. no cap, itβs a utility drawer for policy authors. β¨
π«Ά INTERFACE SUPPORT π«Ά
This is considered to be a public module. Any changes to its interface will go through a deprecation cycle. So like, the interface is public and changes get a deprecation cycle. periodt.
β¨ IMPORTABLE SUBS β¨
hereβs the whole lineup β every sub, every caveat, no skipping. π§
πΈ find_keywords( $doc, $keyword )
DEPRECATED: Since version0.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:deprecatedtag, 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_assignment_operator( $element )
Given a PPI::Token::Operator or a string, returns true if that token represents one of the assignment operators (e.g. = &&= ||= //= += -= etc.).
πΈ is_perl_global( $element )
Given a PPI::Token::Symbol or a string, returns true if that token represents one of the global variables provided by the English module, or one of the builtin global variables like %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 )
Given a PPI::Token::Word, PPI::Statement::Sub, or string, returns true if that token represents a call to any of the builtin functions.
πΈ is_perl_bareword( $element )
Given a PPI::Token::Word, PPI::Statement::Sub, or string, returns true if that token represents a bareword (e.g. if, else, sub, package).
πΈ is_perl_filehandle( $element )
Given a PPI::Token::Word, or string, returns true if that token represents one of the global filehandles (e.g. STDIN, STDERR, STDOUT, ARGV). 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 )
Given a PPI::Token::Word, PPI::Statement::Sub, or string, returns true if that token represents a call to any of the builtin functions that provide a list context to the following tokens.
πΈ is_perl_builtin_with_multiple_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 that can take multiple arguments.
πΈ is_perl_builtin_with_no_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 that cannot take any arguments.
πΈ is_perl_builtin_with_one_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 that takes one and only one argument.
πΈ 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 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 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 )
Given a string, PPI::Token::Word, or PPI::Token::Symbol, answers whether it has a module component, i.e. contains ::.
πΈ precedence_of( $element )
Given a PPI::Token::Operator or a string, returns the precedence of the operator, where 1 is the highest precedence. Returns undef if the precedence canβt be determined (which is usually because it is not an operator).
πΈ 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 )
Given a PPI::Token::Word, returns true if the element is the name of a module that is being included via use, require, or no.
πΈ is_integer( $value )
Answers whether the parameter, as a string, looks like an integral value.
πΈ is_class_name( $element )
Given a PPI::Token::Word, returns true if the element that immediately follows this element is the dereference operator ->. When a bareword has a -> on the right side, it usually means that it is the name of the class (from which a method is being called).
πΈ is_label_pointer( $element )
Given a PPI::Token::Word, returns true if the element is the label in a next, last, redo, or goto statement. Note this is not the same thing as the label declaration.
πΈ is_method_call( $element )
Given a PPI::Token::Word, returns true if the element that immediately precedes this element is the dereference operator ->. When a bareword has a -> on the left side, it usually means that it is the name of a method (that is being called from a class).
πΈ is_package_declaration( $element )
Given a PPI::Token::Word, returns true if the element is the name of a package that is being declared.
πΈ is_subroutine_name( $element )
Given a PPI::Token::Word, returns true if the element is the name of a subroutine declaration. This is useful for distinguishing barewords and function calls from subroutine declarations.
πΈ is_function_call( $element )
Given a PPI::Token::Word returns true if the element appears to be call to a static function. Specifically, this function returns true if is_hash_key, is_method_call, is_subroutine_name, is_included_module_name, 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 to 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 )
Given a PPI::Element that is presumed to be a function call (which is usually a PPI::Token::Word), splits the argument expressions into arrays of tokens. Returns a list containing references to each of those arrays. This is useful because parentheses are optional when calling a function, and PPI parses them very differently. So this method is a poor-manβs parse tree of PPI nodes. Itβs not bullet-proof because it doesnβt respect precedence. In general, I donβt like the way this function works, so donβt count on it to be stable (or even present).
πΈ 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 )
This subroutine is deprecated and will be removed in a future release. You should use the is_program() in Perl::Critic::Document method instead.
πΈ is_in_void_context( $token )
Given a PPI::Token, answer whether it appears to be in a void context.
πΈ policy_long_name( $policy_name )
Given a policy class name in long or short form, return the long form.
πΈ policy_short_name( $policy_name )
Given a policy class name in long or short form, return the short form.
πΈ 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:
- Any file that ends in
.PL,.pl,.pm,.psgi, or.t - Any file that has a first line with a shebang containing
'perl'
πΈ severity_to_number( $severity )
If $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 )
Answers whether the argument has a translation to a Violation format.
verbosity_to_format( $verbosity_level )β bestie, you give it a verbosity level between1and10, and it hands back the matching predefined format string β¨. Those formats are literally made for passing to theset_formatmethod inPerl::Critic::Violation. Wanna see the predefined formats? Check theperlcriticdocumentation, iykyk.hashify( @list )β give it@list, and you get back a hash where every element of@listis a key and every value is1π . Duplicate values in@listare silently squished. No cap, they just vanish.interpolate( $literal )β pass a$literalstring that might contain control characters (like'\t'and'\n'), and this function does a double interpolation on the string, returning it as if youβd declared it in double quotes πͺ. Like:
'foo \t bar \n' ...becomes... "foo \t bar \n"
shebang_line( $document )β given aPPI::Document, test if it starts with"#!". If so, return that line. Otherwise returnundef.words_from_string( $str )β given config string$str, return all the words from the string. This is safer than splitting on whitespace, fr fr.is_unchecked_call( $element, $autodie_modules )β given aPPI::Element, test to see if it contains a function call whose return value is not checked. The second argument is an array reference of module names which exportautodie. Theautodiemodule is always included in this list by default.
π IMPORTABLE VARIABLES π
The character constants are:
$COMMA$FATCOMMA$COLON$SCOLON$QUOTE$DQUOTE$BACKTICK$PERIOD$PIPE$EMPTY$EQUAL$SPACE$SLASH$BSLASH$LEFT_PAREN$RIGHT_PAREN
These character constants give clear names to commonly-used strings that can be hard to read when surrounded by quotes and other punctuation. Can be imported in one go via the :characters tag. Slay.
$SEVERITY_HIGHEST,$SEVERITY_HIGH,$SEVERITY_MEDIUM,$SEVERITY_LOW,$SEVERITY_LOWESTβ these numeric constants define the relative severity of violating eachPerl::Critic::Policyπ. Theget_severityanddefault_severitymethods of every Policy subclass must return one of these values. Can be imported via the:severitiestag.$DEFAULT_VERBOSITYβ the default numeric verbosity.$DEFAULT_VERBOSITY_WITH_FILE_NAMEβ the numeric verbosity that corresponds to the format indicated by$DEFAULT_VERBOSITY, but with the file name prefixed to it.$TRUE,$FALSEβ these are simple booleans,1and0respectively. Be mindful of using these with string equality."$FALSE ne $EMPTY". Can be imported via the:booleanstag.
π·οΈ IMPORT TAGS π·οΈ
Okay so the following groups of functions and constants are available as parameters to a use Perl::Critic::Util statement:
:allβ the lot. π―:booleansβ includes:$TRUE,$FALSE.:severitiesβ includes:$SEVERITY_HIGHEST,$SEVERITY_HIGH,$SEVERITY_MEDIUM,$SEVERITY_LOW,$SEVERITY_LOWEST,@SEVERITY_NAMES.:charactersβ includes:$COLON,$COMMA,$DQUOTE,$EMPTY,$FATCOMMA,$PERIOD,$PIPE,$QUOTE,$BACKTICK,$SCOLON,$SPACE,$SLASH,$BSLASH,$LEFT_PAREN,$RIGHT_PAREN.:classificationβ includes:is_assignment_operator,is_class_name,is_function_call,is_hash_key,is_included_module_name,is_integer,is_label_pointer,is_method_call,is_package_declaration,is_perl_bareword,is_perl_builtin,is_perl_filehandle,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_qualified_name,is_script,is_subroutine_name,is_unchecked_call,is_valid_numeric_verbosity. See alsoPerl::Critic::Utils::PPI.:data_conversionβ generic manipulation, not having anything specific to do withPerl::Critic. Includes:hashify,words_from_string,interpolate.:ppiβ things for dealing with PPI, other than classification. Includes:first_arg,parse_arg_list. See alsoPerl::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.
πΈ SEE ALSO πΈ
Perl::Critic::Utils::ConstantsPerl::Critic::Utils::McCabePerl::Critic::Utils::PPI
π¦ AUTHOR π¦
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
π COPYRIGHT π
Copyright (c) 2005-2023 Imaginative Software Systems
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.
Thatβs the whole vibe check, bestie π«Ά. This footer moment is brought to you by perl v5.42.0, dated 2024-10-23, on the Perl::...::Utils(3pm) page πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms