Perl::Metrics2::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::Metrics2::Plugin::Magic; use base 'Perl::Metrics2::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::Metrics2::Plugin sub-classes.
Perl::Metrics2::Plugin
Implementing a metrics package is pretty easy.
First, create a Perl::Metrics2::Plugin::Something package, inheriting from Perl::Metrics2::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 destructive method is used by the plugin to indicate that the PPI document passed in will be altered during the metric generation.
The value is used by the metrics engine to optimise document cloning and reduce the number of expensive cloning to a minimum.
This value defaults to true for safety reasons, and should be overridden in your subclass if your metrics are not destructive.
Bugs should be reported via the CPAN bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl-Metrics2
For other issues, contact the author.
Adam Kennedy <adamk@cpan.org>
Perl::Metrics, PPI
Copyright 2009 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::Metrics2, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Metrics2
CPAN shell
perl -MCPAN -e shell install Perl::Metrics2
For more information on module installation, please visit the detailed CPAN module installation guide.