Perl::Metrics::Plugin - Base class for Perl::Metrics Plugins
# Implement a simple metrics package which counts up the # use of each type of magic variable. package Perl::Metrics::Plugin::Magic; use base 'Perl::Metrics::Plugin'; # Creates the metric 'all_magic'. # The total number of magic variables. sub metric_all_magic { my ($self, $Document) = @_; return scalar grep { $_->isa('PPI::Token::Magic') } $Document->tokens; } # The number of $_ "scalar_it" magic vars sub metric_scalar_it { my ($self, $Document) = @_; return scalar grep { $_->content eq '$_' } grep { $_->isa('PPI::Token::Magic') } $Document->tokens; } # ... and so on, and so forth. 1;
The Perl::Metrics system does not in and of itself generate any actual metrics data, it merely acts as a processing and storage engine.
The generation of the actual metrics data is done via metrics packages, which as implemented as Perl::Metrics::Plugin sub-classes.
Perl::Metrics::Plugin
Implementing a metrics package is pretty easy.
First, create a Perl::Metrics::Plugin::Something package, inheriting from Perl::Metrics::Plugin.
The create a subroutine for each metric, named metric_$name.
For each subroutine, you will be passed the plugin object itself, and the PPI::Document object to generate the metric for.
Return the metric value from the subroutine. And add as many metric_ methods as you wish. Methods not matching the pattern /^metric_(.+)$/ will be ignored, and you may use them for whatever support methods you wish.
The new constructor is quite trivial at this point, and is provided merely as a convenience. You don't really need to think about this.
new
A convenience method to get the class for the plugin object, to avoid having to use ref directly (and making the intent of any code a little clearer).
The metrics method provides the list of metrics that are provided by the metrics package. By default, this list is automatically generated for you scanning for metric_$name methods that reside in the immediate package namespace.
metrics
metric_$name
Returns a reference to a HASH where the keys are the metric names, and the values are the "version" of the metric (for versioned metrics), or undef if the metric is not versioned.
HASH
undef
The process_index method will cause the metrics plugin to scan every single file entry in the database, and run any an all metrics required to bring to the database up to complete coverage for that plugin.
process_index
This process may take some time for large indexes.
The process_file method takes as argument a single Perl::Metrics::File and run any and all metrics required to bring that file up to complete coverage for the plugin.
process_file
Bugs should be reported via the CPAN bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl-Metrics
For other issues, contact the author.
Adam Kennedy <adamk@cpan.org>
Perl::Metrics, PPI
Copyright 2005 - 2008 Adam Kennedy.
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.
To install Perl::Metrics, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Metrics
CPAN shell
perl -MCPAN -e shell install Perl::Metrics
For more information on module installation, please visit the detailed CPAN module installation guide.