View on
Adam Kennedy > Perl-Metrics2-0.06 > Perl::Metrics2::Plugin



Annotate this POD


New  1
Open  0
View/Report Bugs
Module Version: 0.06   Source  


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') }
  # The number of $_ "scalar_it" magic vars
  sub metric_scalar_it {
      my ($self, $document) = @_;
      return scalar grep { $_->content eq '$_' }
                    grep { $_->isa('PPI::Token::Magic') }
  # ... and so on, and so forth.


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.

Implementing Your Own Metrics Package

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.


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

For other issues, contact the author.


Adam Kennedy <>


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.

syntax highlighting: