blib/lib/Data/Dmap.pm

filter: Statementstmt 30.2% 26 / 86 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.6% 7 / 15 Podpod 100% 2 / 2 totaltotal 26.5% 35 / 132 CC 16 SCAR 48.0
CC
16
Cov
23%
_dmap
240.0
__ANON__
6.0
_store_cache
2.0
CRAP 121.0
↑ Summary
1 package Data::Dmap;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 require v5.10;
6 2 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our @EXPORT = qw{ dmap };
9 2 our @EXPORT_OK = qw{ cut };
10 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util qw{ reftype refaddr };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Data::Dmap - just like map, but on deep data structures
17  
18 =head1 VERSION
19  
20 Version 0.08.
21  
22 =cut
23  
24 2 our $VERSION = '0.08';
25  
26 =head1 SYNOPSIS
27  
28 This module provides the single function C<dmap> which carries out a
29 C<map>-like operation on deep data structures.
30  
31 use Data::Dmap;
32  
33 my $foo = {
34 cars => [ 'ford', 'opel', 'BMW' ],
35 birds => [ 'cuckatoo', 'ostrich', 'frigate' ],
36 handler => sub { print "barf\n" }
37 };
38  
39 # This removes all keys named 'cars'
40 my($bar) = dmap { delete $_->{cars} if ref eq 'HASH'; $_ } $foo;
41  
42 # This replaces arrays with the number of elements they contains
43 my($other) = dmap { $_ = scalar @$_ if ref eq 'ARRAY'; $_ } $foo;
44  
45 use Data::Dumper;
46 print Dumper $other;
47 #
48 # Prints
49 # {
50 # birds => 3,
51 # handler => sub { "DUMMY" }
52 # }
53 # (Data::Dumper doesn't dump subs)
54  
55 $other->{handler}->();
56 # Prints
57 # barf
58  
59 =head1 EXPORTS
60  
61 =over
62  
63 =item C<dmap> (always exported) - the dmap function that does deep in-place mapping
64  
65 =item C<cut> (optional) - a function for stopping recursion.
66  
67 =back
68  
69 =head1 SUBROUTINES
70  
71 =head2 C<dmap>
72  
73 This function works like C<map> - it takes an expression followed by a list,
74 evaluates the expression on each member of the list and returns the result.
75  
76 The only difference is that any references returned by the expression will
77 also be traversed and passed to the expression once again, thus making it
78 possible to make deep traversal of any data structure.
79  
80 Objects (references blessed to something) are just traversed as if they
81 weren't blessed.
82  
83 =head3 Examples
84  
85 Delete all hash references
86  
87 use Data::Dmap;
88 use Data::Dump 'pp';
89
90 pp dmap { return $_ unless ref eq 'HASH'; return; } 1, 'foo', [ { a => 1 }, 2];
91
92 # Prints:
93 # (1, "foo", [2])
94
95 Delete every odd number
96  
97 use Data::Dmap;
98 use Data::Dump 'pp';
99
100 pp dmap { return if $_ % 2; $_ } [ 1 .. 10 ];
101  
102 # Prints:
103 # [2, 4, 6, 8, 10]
104  
105 Replace all hash refs with some C<$object> of class C<thingy>.
106  
107 use Data::Dmap;
108 use Data::Dump 'pp';
109
110 pp dmap { return bless $_, 'thingy' if ref eq 'HASH'; $_ } [ 1, "hello", { a => 1 } ];
111
112 # Prints:
113 # [1, "hello", bless({ a => 1 }, "thingy")]
114  
115 C<dmap> understands what you want, if you return nothing (as opposed to C<undef>) when
116 evaluating the expression for a hash key:
117  
118 use Data::Dmap;
119 use Data::Dump 'pp;
120  
121 my $characters = { main => 'pooh', secondary => 'piglet' };
122 pp dmap { return if $_ eq "piglet"; $_ } $characters;
123
124 # Prints:
125 # { main => "pooh" }
126
127 Because the output from the expression is being traversed, you can use C<dmap> to generate
128 data structures:
129  
130 use Data::Dmap;
131 use Data::Dump 'pp';
132
133 my $height = 3;
134 pp dmap { if(ref eq 'HASH' and $height--) { $_->{a} = {height => $height} } $_ } {};
135
136 # Prints:
137 # {
138 # a => {
139 # a => {
140 # a => {
141 # height => 0
142 # },
143 # height => 1
144 # },
145 # height => 2
146 # }
147 # }
148 # (My own formatting above.)
149  
150 =head2 C<cut>
151  
152 The C<cut> routine stops recursion at any point and returns any data as it is
153 in place of the current node.
154  
155 =head3 Examples
156  
157 use Data::Dmap 'cut';
158 use Data::Dump 'pp';
159
160 my $deep = {
161 level => 1,
162 data => {
163 level => 2,
164 data => {
165 level => 3
166 }
167 }
168 };
169
170 pp dmap { cut('stop') if ref eq 'HASH' and $_->{level} == 2} $deep;
171  
172 # Prints:
173 #
174 # { data => { data => "stop", level => 2 }, level => 1 }
175
176
177 =cut
178  
179 0 ▶sub _store_cache {
Subroutine _store_cache: not called CC=1 SCAR=6.9
180 0 my $cache = shift;
181 0 my $ref = shift;
182 0 $cache->{refaddr($ref)} = [@_];
183 }
184  
185 0 ▶sub _get_cache {
Subroutine _get_cache: not called CC=1 SCAR=6.9
186 0 my $cache = shift;
187 0 my $ref = shift;
188 0 @{$cache->{refaddr($ref)}};
189 }
190  
191 0 ▶sub _has_cache {
Subroutine _has_cache: not called CC=1 SCAR=6.9
192 0 my $cache = shift;
193 0 my $ref = shift;
194 0 exists $cache->{refaddr($ref)};
195 }
196  
197 0 ▶sub _dmap {
Subroutine _dmap: not called CC=15 SCAR=54.8
198 0 my $cache = shift;
199 0 my $callback = shift;
200 map {
201 0 my @result;
202 0 ▶ if(ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
203 0 my $orig_ref = $_;
204 0 ▶ if(not _has_cache($cache, $orig_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _has_cache($cache, $orig_ref)) { } 0 0 0
205 0 my $recurse = 1;
206 0 my @mapped;
207 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 @mapped = $callback->($orig_ref);
209 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
210 0 ▶ if(ref eq 'Data::Dmap::Cut') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'Data::Dmap::Cut') { } 0 0 0
211 0 $recurse = 0;
212 0 @result = @$_;
213 } else {
214 0 die $_
215 }
216 0 };
217 0 ▶ if($recurse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recurse) 0 0 0
218 0 foreach my $val (@mapped) {
219 0 given(reftype $val) {
220 0 when('HASH') {
221 0 for(keys %$val) {
222 0 my @res = _dmap($cache, $callback, $val->{$_});
223 0 ▶ croak 'Multi value return in hash value assignment'
Branch: 1 branch
BranchTrueFalseTotal
if @res > 1 0 0 0
224 if @res > 1;
225 0 ▶ if(@res) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res) { } 0 0 0
226 0 $val->{$_} = $res[0];
227 } else {
228 0 delete $val->{$_};
229 }
230 }
231 0 push @result, $val;
232 }
233 0 when('ARRAY') {
234 0 my $i = 0;
235 0 ▶ while($i <= $#$val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $val->$#*) 0 0 0
236 0 ▶ if(exists $val->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $val->[$i]) 0 0 0
237 # TODO Use splice to allow multi-value returns
238 0 my @res = _dmap($cache, $callback, $val->[$i]);
239 0 ▶ croak 'Multi value return in array single value assignment'
Branch: 1 branch
BranchTrueFalseTotal
if @res > 1 0 0 0
240 if @res > 1;
241 0 ▶ if(@res) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res) { } 0 0 0
242 0 $val->[$i] = $res[0];
243 } else {
244 0 splice @$val, $i, 1;
245 }
246 }
247 0 $i++;
248 }
249 0 push @result, $val;
250 }
251 0 when('SCALAR') {
252 0 my @res = _dmap($cache, $callback, $$val);
253 0 ▶ croak 'Multi value return in single value assignment'
Branch: 1 branch
BranchTrueFalseTotal
if @res > 1 0 0 0
254 if @res > 1;
255 0 ▶ $$val = $res[0] if @res and $$val ne $res[0];
Branch: 1 branch
BranchTrueFalseTotal
if @res and $$val ne $res[0] 0 0 0
Condition0%0/3
@res and $$val ne $res[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@res and $$val ne $res[0]
@res $$val ne $res[0]
Truth table0%0/3
A and B
A= @resB= $$val ne $res[0]
ABresult
0X0
100
111
256 0 push @result, $val;
257 }
258 0 default {
259 0 push @result, $val;
260 }
261 }
262 }
263 }
264 0 _store_cache($cache, $orig_ref, @result);
265 } else {
266 0 push @result, _get_cache($cache, $_);
267 }
268 } else {
269 0 @result = $callback->($_);
270 }
271 0 @result;
272 } @_
273 }
274  
275 0 ▶sub cut { die bless [@_], 'Data::Dmap::Cut' }
Subroutine cut: not called CC=1 SCAR=6.9
Pod documented
276  
277 # Stub that inserts empty map cache
278 0 ▶sub dmap(&@) { _dmap({}, @_) }
Subroutine dmap: not called CC=1 SCAR=6.9
Pod documented
279  
280 =head1 AUTHOR
281  
282 Michael Zedeler, C<< <michael@zedeler.dk> >>
283  
284 =head1 BUGS
285  
286 If you find a bug, please consider helping to fix the bug by doing this:
287  
288 =over
289  
290 =item * Fork C<Data::Dmap> from L<http://github.com/mzedeler/Data-Dmap>
291  
292 =item * Write a test case in the C<t> directory, commit and push it.
293  
294 =item * Fix the bug or (if you don't know how to fix it), report the bug
295  
296 =back
297  
298 Bugs and feature requests can be reported through the web interface at
299 L<http://github.com/mzedeler/Data-Dmap/issues>. I may not be notified, so send
300 me a mail too.
301  
302 =head1 SUPPORT
303  
304 You can find documentation for this module with the perldoc command.
305  
306 perldoc Data::Dmap
307  
308 You can also look for information at:
309  
310 =over 4
311  
312 =item * The github issue tracker
313  
314 L<http://github.com/mzedeler/Data-Dmap/issues>
315  
316 =item * AnnoCPAN: Annotated CPAN documentation
317  
318 L<http://annocpan.org/dist/Data-Dmap>
319  
320 =item * CPAN Ratings
321  
322 L<http://cpanratings.perl.org/d/Data-Dmap>
323  
324 =item * Search CPAN
325  
326 L<http://search.cpan.org/dist/Data-Dmap/>
327  
328 =back
329  
330  
331 =head1 SEE ALSO
332  
333 L<Data::Rmap>, L<Data::Visitor>, L<Data::Transformer>, L<Data::Visitor>, L<Data::Walk>.
334  
335 =head1 TODO
336  
337 =over
338  
339 =item Some kind of option making it possible to traverse objects with L<Class::MOP>
340 metaclasses, so we can avoid breaking encapsulation.
341  
342 =item Options to provide more information about the current node to the
343 callback handler, such as path, depth and data types. Should not affect
344 performance if not used.
345  
346 =back
347  
348 =head1 LICENSE AND COPYRIGHT
349  
350 Copyright 2010 Michael Zedeler.
351  
352 This program is free software; you can redistribute it and/or modify it
353 under the terms of either: the GNU General Public License as published
354 by the Free Software Foundation; or the Artistic License.
355  
356 See http://dev.perl.org/licenses/ for more information.
357  
358  
359 =cut
360  
361 2 1; # End of Data::Dmap
↑ Summary