blib/lib/perl5i/2/HASH.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 144 CC 14 SCAR 36.4
CC
14
Cov
0%
intersect
72.0
diff
56.0
BEGIN
2.0
CRAP 38.1
1 # vi: set ts=4 sw=4 ht=4 et :
2 package perl5i::2::HASH;
3 use 5.010;
4  
5 use strict;
6 use warnings;
7  
8 # Don't accidentally turn carp/croak into methods.
9 require Carp::Fix::1_25;
10 require Hash::StoredIterator;
11  
12 use perl5i::2::Signatures;
13  
14 method each($callback) {
15 return Hash::StoredIterator::hmap( $callback, $self );
16 }
17  
18 sub flip {
19 Carp::Fix::1_25::croak("Can't flip hash with references as values")
20 if grep { ref } values %{$_[0]};
21  
22 my %flipped = reverse %{$_[0]};
23  
24 return wantarray ? %flipped : \%flipped;
25 }
26  
27 sub merge {
28 require Hash::Merge::Simple;
29 my $merged = Hash::Merge::Simple::merge(@_);
30  
31 return wantarray ? %$merged : $merged;
32 }
33  
34 sub print {
35 my $hash = shift;
36 print join(" ", map { "$_ => $hash->{$_}" } keys %$hash);
37 }
38  
39 sub say {
40 my $hash = shift;
41 print join(" ", map { "$_ => $hash->{$_}" } keys %$hash), "\n";
42 }
43  
44 my $common = sub {
45 # Return all things in first array that are also present in second.
46 my ($c, $d) = @_;
47  
48 no warnings 'uninitialized';
49 my %seen = map { $_ => 1 } @$d;
50  
51 my @common = grep { $seen{$_} } @$c;
52  
53 return \@common;
54 };
55  
56 sub diff {
57 my ($base, @rest) = @_;
58 unless (@rest) {
59 return wantarray ? %$base : $base;
60 }
61  
62 die "Arguments must be hash references" if grep { ref $_ ne 'HASH' } @rest;
63  
64 # make a copy so that we can delete kv pairs without modifying the
65 # original hashref.
66 my %base = %$base;
67  
68 require perl5i::2::equal;
69  
70 foreach my $hash (@rest) {
71  
72 my $common_keys = $common->( [ keys %$base ], [ keys %$hash ] );
73  
74 next unless @$common_keys;
75  
76 # Keys are equal, are values also equal?
77 foreach my $key (@$common_keys) {
78 delete $base{$key} if perl5i::2::equal::are_equal( $base->{$key}, $hash->{$key} );
79 }
80  
81 }
82  
83 return wantarray ? %base : \%base;
84 }
85  
86 my $different = sub {
87 # Return all things in first array that are not present in second.
88 my ($c, $d) = @_;
89  
90 no warnings 'uninitialized';
91 my %seen = map { $_ => 1 } @$d;
92  
93 my @different = grep { not $seen{$_} } @$c;
94  
95 return \@different;
96 };
97  
98 sub intersect {
99 my ($base, @rest) = @_;
100  
101 unless (@rest) {
102 return wantarray ? %$base : $base;
103 }
104  
105 die "Arguments must be hash references" if grep { ref $_ ne 'HASH' } @rest;
106  
107 # make a copy so that we can delete kv pairs without modifying the
108 # original hashref.
109 my %base = %$base;
110  
111 require perl5i::2::equal;
112  
113 foreach my $hash (@rest) {
114  
115 my $different_keys = $different->( [ keys %$base ], [ keys %$hash ] );
116  
117 delete @base{@$different_keys};
118  
119 return wantarray ? () : {} unless %base;
120  
121 my $common_keys = $common->( [ keys %$base ], [ keys %$hash ] );
122  
123 # Keys are equal, are values also equal?
124 foreach my $key (@$common_keys) {
125 delete $base{$key} unless perl5i::2::equal::are_equal( $base->{$key}, $hash->{$key} );
126 }
127  
128 }
129  
130 return wantarray ? %base : \%base;
131 }
132  
133 1;