blib/lib/Data/Type/Digger.pm

filter: Statementstmt 100% 51 / 51 Branchbran 86.3% 19 / 22 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 96.6% 87 / 90 CC 18 SCAR 23.3
CC
18
Cov
96%
_dig
15.0
dig
4.0
BEGIN
1.0
CRAP 10.3
↑ Summary
1 package Data::Type::Digger;
2  
3 =head1 NAME
4  
5 Data::Type::Digger - digging types from data structures
6  
7 =head1 SYNOPSIS
8  
9 use Data::Type::Digger;
10  
11 my $in_data = { ... };
12  
13 my $out_data = dig( $in_data, do_scalar => { uc @_ } );
14  
15  
16 =head1 DESCRIPTION
17  
18 B<dig> helps you to deal with deep data structores.
19  
20 Instead of other modules this allow more clearly separation of
21 processing for different types of nodes into different sub's.
22  
23 This can be useful, if the procesing code is different for different types of nodes,
24 or if nodes have their own methods to apply or something other like this.
25  
26 If you looking for more simple and type-independent tool, then you may look on
27 some similar packages. For example: Data::Rmap, Data::Dmap, Data::Traverse.
28  
29 Instead, if you need to process something like this:
30 dig( $in_data, do_my_unique_class => { shift->unique_class_method() }, do_some_other_class => ... );
31 this module will be more useful.
32 Also this module provide depth limitation, unblessing and cloning with passing just a simple param
33  
34 =head1 METHODS
35  
36 =head2 C<dig>
37  
38 Perform recursive digging required types from data structure
39  
40 my $out_data = dig( $in_data, %params );
41  
42 in_data = source structure, required
43  
44 Params: # all param keys are optional
45 do_all => coderef, function called for all nodes
46 do_hash => coderef, function called for all hashref nodes
47 do_array => coderef, function called for all arrayref nodes
48 do_scalar => coderef, function called for all scalar nodes
49 do_type => coderef, function called for all nodes with ref = 'type'
50 unbless => 0 || 1, turn all blessed objects into simple hashrefs
51 clone => 1, make all actions on cloned structure and save the source data
52 max_deep => -1, int -1, undef (not limited) || int (limited to INT) depth of work
53 max_deep_cut => 0 || 1, save or cut the data deeper then max_deep
54  
55 coderef
56 assumes two params:
57 node - value of current node
58 key - name or index of parent_node (if parent node ref is hash or array)
59  
60 returns:
61 new value of node
62  
63  
64 =head1 SUPPORT AND DOCUMENTATION
65  
66 After installing, you can find documentation for this module with the
67 perldoc command.
68  
69 perldoc Data::Type::Digger
70  
71 You can also look for information at:
72  
73 RT, CPAN's request tracker (report bugs here)
74 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Type-Digger
75  
76 AnnoCPAN, Annotated CPAN documentation
77 http://annocpan.org/dist/Data-Type-Digger
78  
79 CPAN Ratings
80 http://cpanratings.perl.org/d/Data-Type-Digger
81  
82 Search CPAN
83 http://search.cpan.org/dist/Data-Type-Digger/
84  
85  
86 =head1 AUTHOR
87  
88 ANTONC <antonc@cpan.org>
89  
90 =head1 LICENSE
91  
92 This program is free software; you can redistribute it and/or modify it
93 under the terms of the the Artistic License (2.0). You may obtain a
94 copy of the full license at:
95  
96 L<http://www.perlfoundation.org/artistic_license_2_0>
97  
98 =cut
99  
100 2 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶use Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 2 our $VERSION = '0.06';
107  
108 2 our @EXPORT_OK = qw/ dig /;
109  
110 2 our %_param;
111 2 our $_levels_to_stop;
112  
113 =head1 FUNCTIONS
114  
115 =head2 dig($data, %param)
116  
117 perform digging data and do some actions on values
118  
119 =cut
120  
121 14 ▶sub dig {
Subroutine dig: called CC=4 SCAR=13.9
Pod documented
122 14 my ( $data, %param ) = @_;
123  
124 14 ▶ die 'no data given' unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 14 14
125  
126 14 %_param = %param;
127  
128 # cloning data, if required
129 14 ▶ if ( $param{clone} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"clone"}) 1 13 14
130 1 $data = Clone::clone $data;
131 }
132  
133 # Countdown levels deep
134 14 ▶ $_levels_to_stop = $param{max_deep} || -1;
Condition100%2/2
$param{'max_deep'} || -1
l!l
212
MC/DC100%1/1
$param{'max_deep'} || -1
$param{'max_deep'}
Truth table100%2/2
A
A= $param{'max_deep'}
Aresult
11
00
135  
136 14 _dig( $data );
137 };
138  
139  
140 # Working with current node and make recursive call for all subnodes
141 78 ▶sub _dig {
Subroutine _dig: called CC=15 SCAR=27.1
142 78 my ( $data, $up_key ) = @_;
143  
144 # stop, if max deep reached
145 78 ▶ unless ( $_levels_to_stop-- ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Data::Type::Digger::_levels_to_stop--) 6 72 78
146 6 $_levels_to_stop++;
147 6 ▶ return $_param{max_deep_cut} ? undef : $data;
Branch: 1 branch
BranchTrueFalseTotal
$Data::Type::Digger::_param{'max_deep_cut'} ? : 4 2 6
148 };
149  
150 # Get a type of value, regardless of any blessing
151 72 ▶ my $ref = Scalar::Util::reftype( $data ) // '';
Condition100%2/2
builtin::reftype($data) // ""
l!l
3636
MC/DC100%1/1
builtin::reftype($data) // ""
builtin::reftype($data)
Truth table100%2/2
A
A= builtin::reftype($data)
Aresult
11
00
152  
153 72 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 9 63 72
154 9 for ( 0 .. @$data-1 ) {
155 25 $data->[$_] = _dig( $data->[$_], $_ );
156 };
157 }
158  
159 72 ▶ if ( $ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 27 45 72
160 27 for ( keys %$data ) {
161 39 $data->{$_} = _dig( $data->{$_}, $_ );
162 };
163 }
164  
165 # Trying to apply something useful for this node
166 186 my @do = map { s/\:+/_/g; 'do_'.lc }
167 72 ▶ keys %{{ map { $_ => 1 } $ref, ( ref $data || 'scalar' ), 'all' }};
Condition100%2/2
ref $data || 'scalar'
l!l
3636
MC/DC100%1/1
ref $data || 'scalar'
ref $data
Truth table100%2/2
A
A= ref $data
Aresult
11
00
168  
169 72 for ( @do ) {
170 186 ▶ next unless $_param{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $Data::Type::Digger::_param{$_} 150 36 186
171 36 $data = $_param{$_}->( $data, $up_key );
172 }
173  
174 72 $_levels_to_stop++;
175  
176 # If unblessing not required then do nothing
177 72 ▶ return $data unless $_param{unbless};
Branch: 1 branch
BranchTrueFalseTotal
unless $Data::Type::Digger::_param{"unbless"} 63 9 72
178  
179 # Unbless reference if we can
180 9 ▶ return { %$data } if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 6 3 9
181 3 ▶ return [ @$data ] if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 0 3 3
182  
183 # Don't know how to unbless this object
184 3 ▶ return undef if ref $data;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 3 3
185  
186 # Just a simple scalar
187 3 return $data;
188 };
189  
190 2 1;
↑ Summary