blib/lib/Data/FetchPath.pm

filter: Statementstmt 100% 59 / 59 Branchbran 85.0% 17 / 20 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 97.2% 105 / 108 CC 17 SCAR 19.5
CC
17
Cov
97%
_hash_path
9.0
_array_path
9.0
BEGIN
1.0
CRAP 7.0
↑ Summary
1 package Data::FetchPath;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @EXPORT_OK = ('path');
8  
9 =head1 NAME
10  
11 Data::FetchPath - "eval"able paths to your complex data values
12  
13 =head1 VERSION
14  
15 Version 0.02
16  
17 =cut
18  
19 3 our $VERSION = '0.02';
20  
21 =head1 SYNOPSIS
22  
23 Quick summary of what the module does.
24  
25 use Data::FetchPath 'path';
26 use Test::Most;
27  
28 my $data = {
29 foo => 3,
30 bar => [qw/ this that 3 /],
31 3 => undef,
32 baz => {
33 trois => 3,
34 quatre => [qw/ 1 2 3 4 /],
35 }
36 };
37 my $target_value = 3;
38 ok $paths = path( $data, $target_value ), 'Fetching paths for matching data should succeed';
39 my @expected = sort qw(
40 {bar}[2]
41 {baz}{trois}
42 {baz}{quatre}[2]
43 {foo}
44 );
45 eq_or_diff $path, \@expected,
46 '... and it should return all paths to data values found';
47 for ( 0 .. $#expected ) {
48 my $found_value = eval "\$data->$expected[$_]";
49 is $found_value, $target_value,
50 '... and all values should match the value you looked for';
51 }
52 }
53  
54 =head1 EXPORT
55  
56 =head1 FUNCTIONS
57  
58 =head2 C<path>
59  
60 Exported on demand via:
61  
62 use Data::FetchPath 'path';
63 my $paths = path($data_structure, $value);
64 my $paths = path($data_structure, $regex);
65  
66 Passed a data structure and either a scalar value or a regex
67 (C<qr/foo.*bar/>), this function will return an array reference to the paths
68 to said value. Each path is suitable for using C<eval> against said data
69 structure:
70  
71 my %data = (
72 one => 'uno',
73 two => 'dos',
74 three => 'tres',
75 );
76 # find values with the letter 'o' in them
77 my $paths = path(\%data, qr/o/);
78 foreach my $path (@$data) {
79 print eval "\$data$path\n";
80 }
81 __END__
82 uno
83 dos
84  
85 Currently the data structure must be an array or hash reference. The value
86 must be a scalar or a regular expression.
87  
88 =cut
89  
90 3 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 3 my %path = (
93 ARRAY => \&_array_path,
94 HASH => \&_hash_path,
95 );
96  
97 7 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
98 7 my ( $data, $search_term ) = @_;
99 7 my $type = reftype $data;
100 7 my $find_paths = $path{$type};
101 7 return $find_paths->( $data, $search_term, { $data => 1 } );
102 }
103  
104 11 ▶sub _array_path {
Subroutine _array_path: called CC=9 SCAR=22.0
105 11 my ( $data, $search_term, $seen ) = @_;
106 11 my @paths;
107 11 foreach my $i ( 0 .. $#$data ) {
108 40 my $item = $data->[$i];
109 40 my $type = reftype $item;
110 40 my $current_index = "[$i]";
111 40 my $ref = ref $search_term;
112 40 ▶ if ( !$type ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 36 4 40
elsif (my $find_paths = $path{$type}) { } 4 0 4
113 36 ▶ if ( !$ref && $item eq $search_term ) { # XXX
Branch: 2 branches
BranchTrueFalseTotal
if (not $ref and $item eq $search_term) { } 11 25 36
elsif ('Regexp' eq $ref and $item =~ /$search_term/) { } 2 41 43
Condition100%3/3
not $ref and $item eq $search_term
!ll&&!rl&&r
71811
MC/DC100%2/2
not $ref and $item eq $search_term
not $ref $item eq $search_term
Truth table100%3/3
not(A) and B
A= not $refB= $item eq $search_term
ABresult
0X0
100
111
114 11 push @paths => $current_index;
115 }
116 ▶ elsif ( 'Regexp' eq $ref && $item =~ $search_term ) {
Condition100%3/3
"Regexp" eq $ref and $item =~ /$search_term/
!ll&&!rl&&r
1852
MC/DC100%2/2
"Regexp" eq $ref and $item =~ /$search_term/
"Regexp" eq $ref $item =~ /$search_term/
Truth table100%3/3
A and B
A= "Regexp" eq $refB= $item =~ /$search_term/
ABresult
0X0
100
111
117 2 push @paths => $current_index;
118 }
119 }
120 elsif ( my $find_paths = $path{$type} ) {
121 4 ▶ unless ( $seen->{$item} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen->{$item}) 3 1 4
122 3 $seen->{$item} = 1;
123 my @current_paths =
124 5 map { "$current_index$_" }
125 3 @{ $find_paths->( $item, $search_term, $seen ) };
126 3 push @paths => @current_paths;
127 }
128 }
129 }
130 11 return \@paths;
131 }
132  
133 5 ▶sub _hash_path {
Subroutine _hash_path: called CC=9 SCAR=22.0
134 5 my ( $data, $search_term, $seen ) = @_;
135 5 my @paths;
136 5 foreach my $key ( keys %$data ) {
137 15 my $item = $data->{$key};
138 15 my $type = reftype $item;
139 15 my $current_key = "{$key}";
140 15 my $ref = ref $search_term;
141 15 ▶ if ( !$type ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 9 6 15
elsif (my $find_paths = $path{$type}) { } 6 0 6
142 9 ▶ if ( !$ref && $item eq $search_term ) { # XXX
Branch: 2 branches
BranchTrueFalseTotal
if (not $ref and $item eq $search_term) { } 3 6 9
elsif ('Regexp' eq $ref and $item =~ /$search_term/) { } 1 7 8
Condition100%3/3
not $ref and $item eq $search_term
!ll&&!rl&&r
423
MC/DC100%2/2
not $ref and $item eq $search_term
not $ref $item eq $search_term
Truth table100%3/3
not(A) and B
A= not $refB= $item eq $search_term
ABresult
0X0
100
111
143 3 push @paths => $current_key;
144 }
145 ▶ elsif ( 'Regexp' eq $ref && $item =~ $search_term ) {
Condition100%3/3
"Regexp" eq $ref and $item =~ /$search_term/
!ll&&!rl&&r
231
MC/DC100%2/2
"Regexp" eq $ref and $item =~ /$search_term/
"Regexp" eq $ref $item =~ /$search_term/
Truth table100%3/3
A and B
A= "Regexp" eq $refB= $item =~ /$search_term/
ABresult
0X0
100
111
146 1 push @paths => $current_key;
147 }
148 }
149 elsif ( my $find_paths = $path{$type} ) {
150 6 ▶ unless ( $seen->{$item} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen->{$item}) 6 0 6
151 6 $seen->{$item} = 1;
152 my @current_paths =
153 7 map { "$current_key$_" }
154 6 @{ $find_paths->( $item, $search_term, $seen ) };
155 6 push @paths => @current_paths;
156 }
157 }
158 }
159 5 return \@paths;
160 }
161  
162 =head1 AUTHOR
163  
164 Curtis "Ovid" Poe, C<< <ovid at cpan.org> >>
165  
166 =head1 BUGS
167  
168 Please report any bugs or feature requests to C<bug-data-fetchpath at rt.cpan.org>, or through
169 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-FetchPath>. I will be notified, and then you'll
170 automatically be notified of progress on your bug as I make changes.
171  
172  
173  
174  
175 =head1 SUPPORT
176  
177 You can find documentation for this module with the perldoc command.
178  
179 perldoc Data::FetchPath
180  
181  
182 You can also look for information at:
183  
184 =over 4
185  
186 =item * RT: CPAN's request tracker
187  
188 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-FetchPath>
189  
190 =item * AnnoCPAN: Annotated CPAN documentation
191  
192 L<http://annocpan.org/dist/Data-FetchPath>
193  
194 =item * CPAN Ratings
195  
196 L<http://cpanratings.perl.org/d/Data-FetchPath>
197  
198 =item * Search CPAN
199  
200 L<http://search.cpan.org/dist/Data-FetchPath>
201  
202 =back
203  
204  
205 =head1 ACKNOWLEDGEMENTS
206  
207  
208 =head1 COPYRIGHT & LICENSE
209  
210 Copyright 2008 Curtis "Ovid" Poe, all rights reserved.
211  
212 This program is free software; you can redistribute it and/or modify it
213 under the same terms as Perl itself.
214  
215  
216 =cut
217  
218 3 1; # End of Data::FetchPath
↑ Summary