File Coverage

blib/lib/Text/Median.pm
Criterion Covered Total %
statement 31 72 43.0
branch 6 24 25.0
condition 2 12 16.6
subroutine 5 7 71.4
pod 3 3 100.0
total 47 118 39.8


line stmt bran cond sub pod time code
1             package Text::Median;
2              
3 1     1   29420 use 5.008009;
  1         5  
  1         37  
4 1     1   5 use strict;
  1         2  
  1         28  
5 1     1   5 use Carp;
  1         8  
  1         74  
6              
7 1     1   961 use Module::Runtime qw( require_module is_valid_module_name );
  1         2076  
  1         7  
8              
9             require Exporter;
10              
11             our @ISA = qw(Exporter);
12              
13             # Items to export into callers namespace by default. Note: do not export
14             # names by default without a very good reason. Use EXPORT_OK instead.
15             # Do not simply export all your public functions/methods/constants.
16              
17             # This allows declaration use Text::Median ':all';
18             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19             # will save memory.
20             our %EXPORT_TAGS = ( 'all' => [ qw(
21            
22             ) ] );
23              
24             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25              
26             our @EXPORT = qw(
27            
28             );
29              
30             our $VERSION = '0.02';
31              
32             sub new {
33              
34 4     4 1 1144 my $class = shift;
35 4         7 my $self = {};
36              
37 4         7 bless $self, $class;
38              
39 4         11 my %arguments = @_;
40              
41 4         14 foreach my $el (keys %arguments) {
42 6         13 $self->{$el} = $arguments{$el};
43             }
44              
45 4 100 66     26 if (!defined $self->{module} || !defined $self->{method}) {
46 1         31 carp "Both a module and a method are required.\n";
47 1         551 return undef;
48             }
49              
50 3 100       12 if (!is_valid_module_name($self->{module})) {
51 1         61 carp $self->{module}." is not a valid module name.\n";
52 1         531 return undef;
53             }
54              
55 2         20 eval {
56 2         8 require_module($self->{module});
57             };
58              
59 2 100       11005 if ($@) {
60 1         19 carp "Having a problem using that module, is it in the correct path?\n";
61 1         565 return undef;
62             }
63              
64 1         7 $self->{distancemethod} = $self->{module}."::".$self->{method};
65              
66 1         7 return $self;
67            
68             }
69              
70             sub add_data {
71              
72 0     0 1   my $self = shift;
73 0           my @data = @_;
74              
75 0           my $count = 0;
76 0 0         if (defined $self->{data}) {
77 0           $count = scalar(keys %{$self->{data}});
  0            
78             }
79 0           foreach my $el (@data) {
80 0           $self->{data}->{$count} = $el;
81 0           $count++;
82             }
83              
84 0           return 1;
85             }
86              
87             sub find_median {
88 0     0 1   my $self = shift;
89              
90 0 0 0       if (!defined $self->{data} || !scalar(keys %{$self->{data}}) ){
  0            
91 0           carp "You must have data to determine the median.\n";
92 0           return 1;
93             }
94              
95 0           my $sum = 0;
96 0           my $minsum;
97 0           my $min = -1;
98              
99 0           my $method = \&{$self->{distancemethod}};
  0            
100 0           for (my $i = 0; $i < scalar(keys %{$self->{data}}); $i++) {
  0            
101 0           for (my $j = 0; $j < scalar(keys %{$self->{data}}); $j++) {
  0            
102 0 0         next if ($i == $j);
103 0 0         if ($i < $j) {
104 0 0         if (!defined $self->{matrix}->{$i}->{$j}) {
105 0           $self->{matrix}->{$i}->{$j} = $method->($self->{data}->{$i},$self->{data}->{$j});
106             }
107 0           $sum += $self->{matrix}->{$i}->{$j};
108             }
109             else {
110 0 0         if (!defined $self->{matrix}->{$j}->{$j}) {
111 0           $self->{matrix}->{$j}->{$i} = $method->($self->{data}->{$j},$self->{data}->{$i});
112             }
113 0           $sum += $self->{matrix}->{$j}->{$i};
114             }
115             }
116 0 0         if (defined $self->{max}) {
117 0 0 0       if (!defined $minsum || $sum > $minsum) {
118 0           $minsum = $sum;
119 0           $min = $i;
120             }
121             }
122             else {
123 0 0 0       if (!defined $minsum || $sum < $minsum) {
124 0           $minsum = $sum;
125 0           $min = $i;
126             }
127             }
128 0           $sum = 0;
129             }
130              
131 0           return $self->{data}->{$min};
132              
133             }
134              
135              
136             # Preloaded methods go here.
137              
138             1;
139             __END__