blib/lib/Array/Average.pm

filter: Statementstmt 97.4% 38 / 39 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 96.8% 62 / 64 CC 10 SCAR 15.4
CC
10
Cov
96%
_flatten
8.0
average
3.0
BEGIN
1.0
CRAP 4.7
↑ Summary
1 package Array::Average;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Scalar::Util qw{reftype};
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION='0.02';
7  
8 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 @ISA = qw(Exporter);
12 1 @EXPORT = qw(average);
13 }
14  
15 =head1 NAME
16  
17 Array::Average - Calculates the average of the values passed to the function.
18  
19 =head1 SYNOPSIS
20  
21 use Array::Average;
22 print average(4,5,6);
23  
24 =head1 DESCRIPTION
25  
26 Array::Average is an L<Exporter> which exports exactly one function called average.
27  
28 =head1 USAGE
29  
30 use Array::Average;
31 print average(4,5,6);
32  
33 use Array::Average qw{};
34 print Array::Average::average(4,5,6);
35  
36 =head1 FUNCTIONS
37  
38 =head2 average
39  
40 Returns the average of all defined scalars and objects which overload "+".
41  
42 my $average=average(1,2,3);
43 my $average=average([1,2,3]);
44 my $average=average({a=>1,b=>2,c=>3}); #only values not keys
45  
46 =cut
47  
48 27 ▶sub average {
Subroutine average: called CC=3 SCAR=11.0
Pod documented
49 27 my @data=_flatten(@_);
50 27 ▶ if (@data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) { } 24 3 27
51 24 my $sum=0;
52 24 $sum+=$_ foreach @data;
53 24 return $sum/scalar(@data);
54 } else {
55 3 return undef;
56 }
57  
58 187 ▶ sub _flatten {
Subroutine _flatten: called CC=8 SCAR=20.8
59 187 ▶ if (@_ == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 2 185 187
elsif (@_ == 1) { } 119 66 185
60 2 return @_;
61 } elsif (@_ == 1) {
62 119 my $val=shift;
63 119 ▶ if (!defined $val) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $val) { } 2 117 119
elsif (not defined builtin::reftype($val)) { } 89 28 117
elsif (builtin::reftype($val) eq 'HASH') { } 7 21 28
elsif (builtin::reftype($val) eq 'ARRAY') { } 11 10 21
elsif (builtin::reftype($val) eq 'SCALAR') { } 10 0 10
64 2 return ();
65 } elsif (!defined reftype($val)) {
66 89 return $val;
67 } elsif (reftype($val) eq "HASH") {
68 7 return _flatten(values %{$val});
69 } elsif (reftype($val) eq "ARRAY") {
70 11 return _flatten(@{$val});
71 } elsif (reftype($val) eq "SCALAR") {
72 10 return _flatten(${$val});
73 } else {
74 0 return $val;
75 }
76 } else {
77 66 return _flatten(shift), _flatten(@_);
78 }
79 }
80 }
81  
82 =head1 BUGS
83  
84 Please log on RT and send an email to the author.
85  
86 =head1 SUPPORT
87  
88 DavisNetworks.com supports all Perl applications including this package.
89  
90 =head1 AUTHOR
91  
92 Michael R. Davis
93 CPAN ID: MRDVT
94 Satellite Tracking of People, LLC
95 mdavis@stopllc.com
96 http://www.stopllc.com/
97  
98 =head1 COPYRIGHT
99  
100 This program is free software licensed under the...
101  
102 The BSD License
103  
104 The full text of the license can be found in the LICENSE file included with this module.
105  
106 =head1 SEE ALSO
107  
108 L<Data::Average>
109  
110 =cut
111  
112 1 1;
↑ Summary