Perl::...s::File(3pm)
π Perl::Metrics::Simple::Analysis::File π
okay bestie, so this page is Perl::Metrics::Simple::Analysis::File(3pm) β the whole vibe is "I analyze ONE file and tell you everything about it." it's part of the user contributed perl documentation, and she's here to spill the tea on your code's complexity. π
β¨ SYNOPSIS β¨
use Perl::Metrics::Simple::Analysis::File;
my $object = Perl::Metrics::Simple::Analysis::File->new(file => 'path/to/file');
πΈ VERSION πΈ
This is VERSION 0.1 β baby's first version, and she's serving. π
π¦ DESCRIPTION π¦
A Perl::Metrics::Simple::Analysis::File object gets created by Perl::Metrics::Simple for each file analyzed. then all those lil objects get aggregated into a Perl::Metrics::Simple::Analysis object by Perl::Metrics::Simple. it's giving family reunion. π«Ά
In general you will not use this class directly β you'll use Perl::Metrics::Simple instead. but honestly? there's no harm in exposing the various methods this class provides. live a little. π
π CLASS METHODS π
π new
Takes named parameters, current only the path parameter is recognized:
my $file_results = BPerl::Metrics::Simple::Analysis::File->new( path => $path );
Returns a new Perl::Metrics::Simple::Analysis::File object which has been populated with the results of analyzing the file at path.
π¨ Throws an exception if the path is missing or unreadable. no path, no vibes, periodt.
π OBJECT METHODS π
Call on an object. (yes, you need an actual object, not just the class. iykyk.)
π all_counts
Convenience method. Takes no arguments and returns a hashref of all counts:
{
path => $self->path,
lines => $self->lines,
main_stats => $self->main_stats,
subs => $self->subs,
packages => $self->packages,
}
β¨ analyze_main
Takes a PPI document and an arrayref of PPI::Statement::Sub objects and returns a hashref with information about the 'main' (non-subroutine) portions of the document:
{
lines => $lines, # Line count outside subs. Skips comments and pod.
mccabe_complexity => $complexity, # Cyclomatic complexity of all non-sub areas
path => '/path/to/file',
name => '{code not in named subroutines}', # always the same name
};
πΈ get_node_length
Takes a PPI node and returns a count of the newlines it contains. PPI normalizes line endings to newlines so CR/LF, CR and LF all come out the same. The line counts reported by the various methods in this class all exclude blank lines, comment lines and pod (the PPI document is pruned before counting.) so clean. so fresh. β¨
π§ lines
Total non-blank, non-comment, non-pod lines.
π main_stats
Returns the hashref generated by analyze_main without re-analyzing document. efficiency queen. π
π₯ logic_keywords
Returns an array (in array context) or ref-to-ARRAY of the keywords used in calculating complexity. See Logic Keywords section below.
π₯ logic_operators
Returns an array (in array context) or ref-to-ARRAY of the operators used in calculating complexity. See Logic Operators section below.
π₯ method_modifiers
Returns an array (in array context) or ref-to-ARRAY of the method modifiers considered to return methods during calculating complexity. See Method modifiers section below.
π measure_complexity
Takes a PPI element and measures an approximation of the McCabe Complexity (aka Cyclomatic Complexity) of the code.
McCabe Complexity is basically a count of how many paths there are through the code. it's the "how many ways can this go" of it all. π£οΈ
We use a simplified method for counting this, which ignores things like the possibility that a 'use' statement could throw an exception.
The actual measurement we use for a chunk of code is 1 plus 1 each logic keyword or operator:
π Logic operators:
The default list is:
@Perl::Metrics::Simple::Analysis::File::DEFAULT_LOGIC_OPERATORS
!
!~
&&
&&=
//
<
<<=
<=>
==
=~
>
>>=
?
and
cmp
eq
gt
lt
ne
not
or
xor
||
||=
~~
You can supply your own list by setting: @Perl::Metrics::Simple::Analysis::File::LOGIC_OPERATORS before creating a new object.
π Logic keywords:
@Perl::Metrics::Simple::Analysis::File::DEFAULT_LOGIC_KEYWORDS
else
elsif
for
foreach
goto
grep
if
last
map
next
unless
until
while
You can supply your own list by setting: @Perl::Metrics::Simple::Analysis::File::LOGIC_KEYWORDS before creating a new object.
π¦ Method modifiers:
@Perl::Metrics::Simple::Analysis::File::DEFAULT_METHOD_MODIFIERS
before
after
around
You can supply your own list by setting: @Perl::Metrics::Simple::Analysis::File::METHOD_MODIFIERS before creating a new object.
π Examples of Complexity
Here are a couple of examples of how we count complexity:
Example of complexity count of 1:
use Foo;
print "Hello world.\n";
exit;
Example of complexity count of 2:
if ( $a ) { # The "if" adds 1.
# do something
}
Example of complexity count of 6:
sub foo { # 1: for non-empty code
if ( @list ) { # 1: "if"
foreach my $x ( @list ) { # 1: "foreach"
if ( ! $x ) { # 2: 1 for "if" and 1 for "!"
do_something($x);
}
else { # 1 for "else"
do_something_else($x);
}
}
}
return;
}
π packages
Arrayref of unique packages found in the file.
π§ path
Either the path to the file, or a scalar ref if that was supplied instead of a path.
π subs
Count of subroutines found.
π STATIC PACKAGE SUBROUTINES π
Utility subs used internally, but no harm in exposing them for now. (she's generous like that.)
β¨ hashify
%hash = Perl::Metrics::Simple::Analysis::File::hashify(@list);
Takes an array and returns a hash using the array values as the keys and with the values all set to 1.
π is_hash_key
$boolean = Perl::Metrics::Simple::Analysis::File::is_hash_key($ppi_element);
Takes a PPI::Element and returns true if the element is a hash key, for example "foo" and "bar" are hash keys in the following:
{ foo => 123, bar => $a }
Copied and somewhat simplified from http://search.cpan.org/src/THALJEF/Perl-Critic-0.19/lib/Perl/Critic/Utils.pm. See Perl::Critic::Utils.
π BUGS AND LIMITATIONS π
None reported yet ;-)
π«Ά DEPENDENCIES π«Ά
ReadonlyPerl::Metrics::Simple::Analysis
π SUPPORT π
Via CPAN:
π¬ Disussion Forum
http://www.cpanforum.com/dist/Perl-Metrics-Simple
π Bug Reports
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Metrics-Simple
π AUTHOR π
Matisse Enzer
CPAN ID: MATISSE
Eigenstate Consulting, LLC
matisse@eigenstate.net
http://www.eigenstate.net/
π LICENSE AND COPYRIGHT π
Copyright (c) 2006-2021 by Eigenstate Consulting, LLC.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
and that's the whole page, bestie π
β¨ no cap, you read all of it, iconic behavior. this was generated by perl v5.36.0, dated 2023-09-29, and the whole thing lives under the name Perl::...s::File(3pm) (aka Perl::Metrics::Simple::Analysis::File) ππ¦π
π¦ where this page comes from
- debian trixie: libperl-metrics-simple-perl 1.0.3-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libperl-metrics-simple-perl 1.0.3-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libperl-metrics-simple-perl 1.0.3-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libperl-metrics-simple-perl 1.0.3-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms