blib/lib/Data/HashArray.pm

filter: Statementstmt 89.0% 65 / 73 Branchbran 60.0% 12 / 20 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.3% 14 / 15 Podpod 100% 2 / 2 totaltotal 78.3% 94 / 120 CC 15 SCAR 17.0
CC
15
Cov
79%
AUTOLOAD
11.3
hash
9.1
new
2.1
CRAP 5.5
↑ Summary
1 # $Id$
2 #
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Data::HashArray;
9 2 require 5.008;
10  
11 2 ▶use Carp qw(carp cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 $VERSION = '1.03';
17  
18 2 our $AUTOLOAD;
19  
20 # Hash deref. gives access to the first element
21 # For debugging purpose only
22 #carp "\033\[0;35m[NOTICE] Overloaded '\"\"' called (".
23 # __PACKAGE__.")\033\[0m";
24 2 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.1
25 28 ▶ '%{}' => sub { $_[0]->_hash_access; },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 0 ▶ '""' => sub { "". $_[0]->_hash_access; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 2 'fallback' => 1,
28 2 );
29  
30 # --------------------------------------------------------------------
31 # Constructor
32 # --------------------------------------------------------------------
33  
34 12 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
35 12 my ($proto, @array) = @_;
36 12 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
37  
38 12 bless \@array, $class;
39 }
40  
41  
42 # Return the first element (which is normally a hash ref) of the list
43 28 ▶sub _hash_access () {
Subroutine _hash_access: called CC=2 SCAR=7.2
44 28 my ($self) = @_;
45  
46 28 my $item = eval { $self->[0]; };
47 28 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 28 28
48 0 cluck "$@";
49 }
50  
51 #carp "Hash-access => DONE\n";
52 28 return $item;
53 }
54  
55 #-------------------------------------------------------
56 # hash(field1, field2, field3, ...)
57 #
58 # Return a hash keyed on field1. If there is no field2, this will be
59 # a hash of HashArrays. If field2 exists, this will be a hash of hashes of
60 # HashArrays. And so on...
61 #
62 # Note that field1; field2, ... may be CODE references, too. In that case, the sub gets called
63 # at least once for each item in the array. The item is passed as an argument to the sub.
64 #
65 # Breadth-first recursive.
66 #-------------------------------------------------------
67 5 ▶sub hash {
Subroutine hash: called CC=9 SCAR=22.1
Pod documented
68 5 my $self = shift;
69 5 my $class = ref($self);
70 5 ▶ return undef unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 5 5
71 5 my $field = shift;
72 5 ▶ return undef unless defined($field);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 5 5
73
74 5 my $h = {};
75
76 # Hash the array on '$field';
77 5 foreach my $item (@$self) {
78 12 my $key;
79
80 12 ▶ if (ref($field) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $field eq 'CODE') { } 3 9 12
elsif (UNIVERSAL::can($item, $field)) { } 0 9 9
81 # Field is a CODE refernce. Call it, with 'item' passed as an argument
82 3 $key = &$field($item);
83 } elsif (UNIVERSAL::can($item, $field)) {
84 # Field has an accessor. Call it (the resukt should stringify to a hash key).
85 0 $key = $item->$field();
86 } else {
87 # Field should otherwise stringify to a hash key
88 9 $key = $item->{$field};
89 }
90
91 # If the keyed item doesn't yet exist, create a new NodeArray and assign it.
92 12 ▶ unless ( exists($h->{$key}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $h->{$key}) 11 1 12
93 11 $h->{$key} = $class->new();
94 }
95
96 # Push the item on the keyed NodeArray.
97 12 my $array= $h->{$key};
98 12 push @$array, $item;
99 }
100  
101 # If we don't have any more fields, just return the hash.
102 5 ▶ return $h unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 1 5
103  
104 # Otherwise, further hash each item in the hash on the remaining fields.
105 1 foreach my $key (keys %$h) {
106 2 my $array = $h->{$key};
107 2 $h->{$key} = $array->hash(@_);
108 }
109 1 return $h;
110 }
111  
112  
113 #-------------------------------------------------------
114 # By default, all method calls are delegated to the first element.
115 #-------------------------------------------------------
116 12 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=24.2
117 12 my $self = shift;
118 12 my $func = $AUTOLOAD;
119 12 $func =~ s/.*:://;
120  
121 12 ▶ if ($func =~ /^[0-9]+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func =~ /^[0-9]+$/o) 0 12 12
122 0 return eval { $self->[$func]; };
123 }
124  
125 12 ▶ return undef if $func eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $func eq "DESTROY" 12 0 12
126 0 ▶ if (reftype($self) && reftype($self) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($self) and builtin::reftype($self) eq 'ARRAY') { } 0 0 0
Condition0%0/3
builtin::reftype($self) and builtin::reftype($self) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($self) and builtin::reftype($self) eq "ARRAY"
builtin::reftype($self) builtin::reftype($self) eq "ARRAY"
Truth table0%0/3
A and B
A= builtin::reftype($self)B= builtin::reftype($self) eq "ARRAY"
ABresult
0X0
100
111
127 0 $self->_hash_access->$func(@_);
128 } else {
129 0 cluck "*** \$self->$func";
130 }
131 }
132  
133  
134 2 1;
135  
136  
137 __END__
↑ Summary