line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: A Hash Object Role for Perl 5 |
2
|
|
|
|
|
|
|
package Data::Object::Role::Hash; |
3
|
|
|
|
|
|
|
|
4
|
42
|
|
|
42
|
|
296139
|
use 5.010; |
|
42
|
|
|
|
|
140
|
|
|
42
|
|
|
|
|
1757
|
|
5
|
42
|
|
|
42
|
|
12864
|
use Data::Object::Role; |
|
42
|
|
|
|
|
80
|
|
|
42
|
|
|
|
|
436
|
|
6
|
|
|
|
|
|
|
|
7
|
42
|
|
|
42
|
|
14032
|
use Data::Object 'codify'; |
|
42
|
|
|
|
|
75
|
|
|
42
|
|
|
|
|
2566
|
|
8
|
42
|
|
|
42
|
|
221
|
use Scalar::Util 'blessed'; |
|
42
|
|
|
|
|
63
|
|
|
42
|
|
|
|
|
2118
|
|
9
|
42
|
|
|
42
|
|
31996
|
use Storable 'dclone'; |
|
42
|
|
|
|
|
132026
|
|
|
42
|
|
|
|
|
61432
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
map with($_), our @ROLES = qw( |
12
|
|
|
|
|
|
|
Data::Object::Role::Defined |
13
|
|
|
|
|
|
|
Data::Object::Role::Collection |
14
|
|
|
|
|
|
|
Data::Object::Role::Detract |
15
|
|
|
|
|
|
|
Data::Object::Role::Keyed |
16
|
|
|
|
|
|
|
Data::Object::Role::Output |
17
|
|
|
|
|
|
|
Data::Object::Role::Ref |
18
|
|
|
|
|
|
|
Data::Object::Role::Values |
19
|
|
|
|
|
|
|
Data::Object::Role::Type |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '0.20'; # VERSION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub aslice { |
25
|
1
|
|
|
1
|
0
|
4
|
goto &array_slice; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub array_slice { |
29
|
2
|
|
|
2
|
0
|
5
|
my ($hash, @arguments) = @_; |
30
|
2
|
|
|
|
|
5
|
return [@{$hash}{@arguments}]; |
|
2
|
|
|
|
|
23
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub clear { |
34
|
1
|
|
|
1
|
0
|
8
|
goto ∅ |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub defined { |
38
|
1
|
|
|
1
|
0
|
2
|
my ($hash, $argument) = @_; |
39
|
1
|
|
|
|
|
12
|
return CORE::defined $hash->{$argument}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub delete { |
43
|
1
|
|
|
1
|
0
|
3
|
my ($hash, $argument) = @_; |
44
|
1
|
|
|
|
|
12
|
return CORE::delete $hash->{$argument}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub each { |
48
|
1
|
|
|
1
|
0
|
3
|
my ($hash, $code, @arguments) = @_; |
49
|
1
|
50
|
|
|
|
4
|
$code = codify $code if !ref $code; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
9
|
for my $key (CORE::keys %$hash) { |
52
|
4
|
|
|
|
|
12
|
$code->($key, $hash->{$key}, @arguments); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
5
|
return $hash; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub each_key { |
59
|
1
|
|
|
1
|
0
|
3
|
my ($hash, $code, @arguments) = @_; |
60
|
|
|
|
|
|
|
|
61
|
1
|
50
|
|
|
|
5
|
$code = codify $code if !ref $code; |
62
|
1
|
|
|
|
|
20
|
$code->($_, @arguments) for CORE::keys %$hash; |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
|
|
16
|
return $hash; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub each_n_values { |
68
|
1
|
|
|
1
|
0
|
3
|
my ($hash, $number, $code, @arguments) = @_; |
69
|
|
|
|
|
|
|
|
70
|
1
|
50
|
|
|
|
5
|
$code = codify $code if !ref $code; |
71
|
1
|
|
|
|
|
12
|
my @values = CORE::values %$hash; |
72
|
1
|
|
|
|
|
7
|
$code->(CORE::splice(@values, 0, $number), @arguments) while @values; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
11
|
return $hash; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub each_value { |
78
|
1
|
|
|
1
|
0
|
2
|
my ($hash, $code, @arguments) = @_; |
79
|
|
|
|
|
|
|
|
80
|
1
|
50
|
|
|
|
5
|
$code = codify $code if !ref $code; |
81
|
1
|
|
|
|
|
17
|
$code->($_, @arguments) for CORE::values %$hash; |
82
|
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
15
|
return $hash; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub empty { |
87
|
2
|
|
|
2
|
0
|
5
|
my ($hash) = @_; |
88
|
2
|
|
|
|
|
21
|
CORE::delete @$hash{CORE::keys %$hash}; |
89
|
2
|
|
|
|
|
7
|
return $hash; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub exists { |
93
|
1
|
|
|
1
|
0
|
1
|
my ($hash, $key) = @_; |
94
|
1
|
|
|
|
|
9
|
return CORE::exists $hash->{$key}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub filter_exclude { |
98
|
1
|
|
|
1
|
0
|
3
|
my ($hash, @arguments) = @_; |
99
|
1
|
|
|
|
|
4
|
my %i = map { $_ => $_ } @arguments; |
|
2
|
|
|
|
|
9
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
return { |
102
|
2
|
50
|
|
|
|
14
|
CORE::map { CORE::exists $hash->{$_} ? ($_ => $hash->{$_}) : () } |
|
4
|
|
|
|
|
9
|
|
103
|
1
|
|
|
|
|
11
|
CORE::grep { not CORE::exists $i{$_} } CORE::keys %$hash |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub filter_include { |
108
|
1
|
|
|
1
|
0
|
2
|
my ($hash, @arguments) = @_; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
return { |
111
|
1
|
50
|
|
|
|
2
|
CORE::map { CORE::exists $hash->{$_} ? ($_ => $hash->{$_}) : () } |
|
2
|
|
|
|
|
15
|
|
112
|
|
|
|
|
|
|
@arguments |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub get { |
117
|
4
|
|
|
4
|
0
|
8
|
my ($hash, $argument) = @_; |
118
|
4
|
|
|
|
|
16
|
return $hash->{$argument}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub hash_slice { |
122
|
2
|
|
|
2
|
0
|
4
|
my ($hash, @arguments) = @_; |
123
|
2
|
|
|
|
|
6
|
return {CORE::map { $_ => $hash->{$_} } @arguments}; |
|
4
|
|
|
|
|
33
|
|
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub hslice { |
127
|
1
|
|
|
1
|
0
|
7
|
goto &hash_slice; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub invert { |
131
|
1
|
|
|
1
|
0
|
2
|
my ($hash) = @_; |
132
|
|
|
|
|
|
|
|
133
|
1
|
|
|
|
|
2
|
my $temp = {}; |
134
|
1
|
|
|
|
|
9
|
for (CORE::keys %$hash) { |
135
|
6
|
100
|
|
|
|
17
|
CORE::defined $hash->{$_} ? |
136
|
|
|
|
|
|
|
$temp->{CORE::delete $hash->{$_}} = $_ : |
137
|
|
|
|
|
|
|
CORE::delete $hash->{$_}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
1
|
|
|
|
|
4
|
for (CORE::keys %$temp) { |
141
|
5
|
|
|
|
|
6
|
$hash->{$_} = CORE::delete $temp->{$_}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
1
|
|
|
|
|
3
|
return $hash; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub iterator { |
148
|
1
|
|
|
1
|
0
|
1
|
my ($hash) = @_; |
149
|
1
|
|
|
|
|
2
|
my @keys = CORE::keys %{$hash}; |
|
1
|
|
|
|
|
8
|
|
150
|
|
|
|
|
|
|
|
151
|
1
|
|
|
|
|
2
|
my $i = 0; |
152
|
|
|
|
|
|
|
return sub { |
153
|
5
|
100
|
|
5
|
|
20
|
return undef if $i > $#keys; |
154
|
4
|
|
|
|
|
9
|
return $hash->{$keys[$i++]}; |
155
|
|
|
|
|
|
|
} |
156
|
1
|
|
|
|
|
6
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub keys { |
159
|
2
|
|
|
2
|
0
|
4
|
my ($hash) = @_; |
160
|
2
|
|
|
|
|
19
|
return [CORE::keys %$hash]; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub lookup { |
164
|
16
|
|
|
16
|
0
|
22
|
my ($hash, $path) = @_; |
165
|
|
|
|
|
|
|
|
166
|
16
|
50
|
33
|
|
|
176
|
return undef unless ($hash and $path) and ( |
|
|
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
167
|
|
|
|
|
|
|
('HASH' eq ref($hash)) or blessed($hash) and $hash->isa('HASH') |
168
|
|
|
|
|
|
|
); |
169
|
|
|
|
|
|
|
|
170
|
16
|
100
|
|
|
|
108
|
return $hash->{$path} if $hash->{$path}; |
171
|
|
|
|
|
|
|
|
172
|
11
|
|
|
|
|
12
|
my $next; |
173
|
|
|
|
|
|
|
my $rest; |
174
|
|
|
|
|
|
|
|
175
|
11
|
|
|
|
|
49
|
($next, $rest) = $path =~ /(.*)\.([^\.]+)$/; |
176
|
11
|
100
|
100
|
|
|
65
|
return lookup($hash->{$next}, $rest) if $next and $hash->{$next}; |
177
|
|
|
|
|
|
|
|
178
|
8
|
|
|
|
|
35
|
($next, $rest) = $path =~ /([^\.]+)\.(.*)$/; |
179
|
8
|
100
|
66
|
|
|
47
|
return lookup($hash->{$next}, $rest) if $next and $hash->{$next}; |
180
|
|
|
|
|
|
|
|
181
|
1
|
|
|
|
|
4
|
return undef; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub pairs { |
185
|
1
|
|
|
1
|
0
|
5
|
goto &pairs_array; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub pairs_array { |
189
|
2
|
|
|
2
|
0
|
5
|
my ($hash) = @_; |
190
|
2
|
|
|
|
|
20
|
return [CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash]; |
|
8
|
|
|
|
|
21
|
|
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub merge { |
194
|
1
|
|
|
1
|
0
|
2
|
my ($hash, @arguments) = @_; |
195
|
|
|
|
|
|
|
|
196
|
1
|
50
|
|
|
|
3
|
return dclone $hash unless @arguments; |
197
|
1
|
50
|
|
|
|
3
|
return dclone merge($hash, merge(@arguments)) if @arguments > 1; |
198
|
|
|
|
|
|
|
|
199
|
1
|
|
|
|
|
1
|
my ($right) = @arguments; |
200
|
|
|
|
|
|
|
|
201
|
1
|
|
|
|
|
9
|
my %merge = %$hash; |
202
|
1
|
|
|
|
|
3
|
for my $key (CORE::keys %$right) { |
203
|
2
|
|
|
|
|
3
|
my ($rv, $lv) = CORE::map { ref $$_{$key} eq 'HASH' } $right, $hash; |
|
4
|
|
|
|
|
8
|
|
204
|
2
|
50
|
33
|
|
|
7
|
if ($rv and $lv){ $merge{$key} = merge($hash->{$key}, $right->{$key}) } |
|
0
|
|
|
|
|
0
|
|
205
|
2
|
|
|
|
|
4
|
else { $merge{$key} = $right->{$key} } |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
1
|
|
|
|
|
71
|
return dclone \%merge; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub reset { |
212
|
1
|
|
|
1
|
0
|
1
|
my ($hash) = @_; |
213
|
1
|
|
|
|
|
8
|
@$hash{CORE::keys %$hash} = (); |
214
|
1
|
|
|
|
|
3
|
return $hash; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub reverse { |
218
|
1
|
|
|
1
|
0
|
2
|
my ($hash) = @_; |
219
|
|
|
|
|
|
|
|
220
|
1
|
|
|
|
|
2
|
my $temp = {}; |
221
|
1
|
|
|
|
|
12
|
for (CORE::keys %$hash) { |
222
|
5
|
100
|
|
|
|
17
|
$temp->{$_} = $hash->{$_} if CORE::defined $hash->{$_}; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
1
|
|
|
|
|
11
|
return {CORE::reverse %$temp}; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub set { |
229
|
1
|
|
|
1
|
0
|
1
|
my ($hash, $key, $argument) = @_; |
230
|
1
|
|
|
|
|
7
|
return $hash->{$key} = $argument; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
sub values { |
234
|
3
|
|
|
3
|
0
|
5
|
my ($hash) = @_; |
235
|
3
|
|
|
|
|
32
|
return [CORE::values %$hash]; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
1; |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
__END__ |