| 1 | package Perl::Metrics2::Plugin::Core; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use bytes (); | ||
| 5 | use Perl::Metrics2::Plugin (); | ||
| 6 | |||
| 7 | use vars qw{$VERSION @ISA}; | ||
| 8 | BEGIN { | ||
| 9 | $VERSION = '0.06'; | ||
| 10 | @ISA = 'Perl::Metrics2::Plugin'; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub process_metrics { | ||
| 14 | my $self = shift; | ||
| 15 | my $document = shift; | ||
| 16 | my %metric = (); | ||
| 17 | |||
| 18 | SCOPE: { | ||
| 19 | my @tokens = $document->tokens; | ||
| 20 | |||
| 21 | # The number of tokens in the file | ||
| 22 | $metric{tokens} = scalar @tokens; | ||
| 23 | |||
| 24 | # The number of significant tokens | ||
| 25 | $metric{significant_tokens} = scalar grep { $_->significant } @tokens; | ||
| 26 | } | ||
| 27 | |||
| 28 | SCOPE: { | ||
| 29 | my $string = $document->serialize; | ||
| 30 | |||
| 31 | # The number of bytes in the file | ||
| 32 | $metric{bytes} = bytes::length($string); | ||
| 33 | |||
| 34 | # The number of lines in the file | ||
| 35 | my $newlines =()= $string =~ /\n/g; | ||
| 36 | $metric{lines} = $newlines + 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | SCOPE: { | ||
| 40 | # The final metrics (Source Lines of Code) is more complex (and destructive) | ||
| 41 | $document->prune( sub { | ||
| 42 | # Cull out the normal content | ||
| 43 | ! $_[1]->significant | ||
| 44 | and | ||
| 45 | # Cull out the high-volume whitespace tokens | ||
| 46 | ! $_[1]->isa('PPI::Token::Whitespace') | ||
| 47 | and ( | ||
| 48 | $_[1]->isa('PPI::Token::Comment') | ||
| 49 | or | ||
| 50 | $_[1]->isa('PPI::Token::Pod') | ||
| 51 | or | ||
| 52 | $_[1]->isa('PPI::Token::End') | ||
| 53 | or | ||
| 54 | $_[1]->isa('PPI::Token::Data') | ||
| 55 | ) | ||
| 56 | } ); | ||
| 57 | |||
| 58 | # Split the serialized for and find the number of non-blank lines | ||
| 59 | $metric{sloc} = scalar grep { /\S/ } split /\n/, $document->serialize; | ||
| 60 | } | ||
| 61 | |||
| 62 | return %metric; | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | =pod | ||
| 68 | |||
| 69 | =head1 NAME | ||
| 70 | |||
| 71 | Perl::Metrics2::Plugin::Core - The Core Perl Metrics Package | ||
| 72 | |||
| 73 | =head1 DESCRIPTION | ||
| 74 | |||
| 75 | This class provides a set of core metrics for Perl documents, based on | ||
| 76 | very simple code using only the core L<PPI> package. | ||
| 77 | |||
| 78 | =head1 METRICS | ||
| 79 | |||
| 80 | As with all L<Perl::Metrics::Plugin> packages, all metrics can be | ||
| 81 | referenced with the global identifier C<Perl::Metrics::Plugin::Core::metric>. | ||
| 82 | |||
| 83 | Metrics are listed as "datatype name". | ||
| 84 | |||
| 85 | =head2 integer bytes | ||
| 86 | |||
| 87 | The C<bytes> metric represents the number of bytes in the file. | ||
| 88 | |||
| 89 | =head2 integer lines | ||
| 90 | |||
| 91 | The C<lines> metric represents the number of raw lines in the file. | ||
| 92 | |||
| 93 | =head2 integer sloc | ||
| 94 | |||
| 95 | The C<sloc> metric represents Source Lines of Code. That is, raw lines | ||
| 96 | minus __END__ content, __DATA__ content, POD, comments and blank lines. | ||
| 97 | |||
| 98 | =head2 integer tokens | ||
| 99 | |||
| 100 | The C<tokens> metric represents the total number of L<PPI::Token> objects | ||
| 101 | contained in the document. | ||
| 102 | |||
| 103 | For example, the following one-line document would have a C<tokens> metric | ||
| 104 | of 5 (assuming a single trailing newline) | ||
| 105 | |||
| 106 | print "Hello World!\n"; | ||
| 107 | |||
| 108 | =head2 integer significant_tokens | ||
| 109 | |||
| 110 | The C<significant_tokens> metric represents the total number of | ||
| 111 | C<significant> tokens contained in the document. | ||
| 112 | |||
| 113 | This filters out things like whitespace and comments, and refers (more or | ||
| 114 | less) to only the parts of the document that actually do something. | ||
| 115 | |||
| 116 | For more information on significance, see L<PPI::Element/significant>. | ||
| 117 | |||
| 118 | =head1 SUPPORT | ||
| 119 | |||
| 120 | Bugs should be reported via the CPAN bug tracker at | ||
| 121 | |||
| 122 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl-Metrics2> | ||
| 123 | |||
| 124 | For other issues, contact the author. | ||
| 125 | |||
| 126 | =head1 AUTHOR | ||
| 127 | |||
| 128 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 129 | |||
| 130 | =head1 SEE ALSO | ||
| 131 | |||
| 132 | L<Perl::Metrics::Plugin>, L<Perl::Metrics>, L<PPI> | ||
| 133 | |||
| 134 | =head1 COPYRIGHT | ||
| 135 | |||
| 136 | Copyright 2009 Adam Kennedy. | ||
| 137 | |||
| 138 | This program is free software; you can redistribute | ||
| 139 | it and/or modify it under the same terms as Perl itself. | ||
| 140 | |||
| 141 | The full text of the license can be found in the | ||
| 142 | LICENSE file included with this module. | ||
| 143 | |||
| 144 | =cut |