File Coverage

blib/lib/Data/PathSimple.pm
Criterion Covered Total %
statement 55 56 98.2
branch 34 38 89.4
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 97 102 95.1


line stmt bran cond sub pod time code
1             package Data::PathSimple;
2              
3 2     2   151793 use strict;
  2         14  
  2         61  
4 2     2   10 use warnings;
  2         3  
  2         63  
5              
6 2     2   1063 use version 0.77;
  2         4352  
  2         14  
7             our $VERSION = qv("v1.0.3");
8              
9 2     2   203 use base 'Exporter';
  2         5  
  2         1563  
10             our @EXPORT_OK = qw{
11             get
12             set
13             };
14              
15             sub get {
16 494     494 1 4042296 my ( $root_ref, $root_path ) = @_;
17              
18 494 100       1478 return undef unless defined $root_path;
19 491         2108 $root_path =~ s/^\///;
20              
21 491         1962 my @root_parts = split '/', $root_path;
22 491         920 my $current_ref = $root_ref;
23              
24 491 100       1298 return undef unless @root_parts;
25              
26 488         1058 foreach my $current_part ( @root_parts ) {
27 1712 100       3810 if ( ref $current_ref eq 'HASH' ) {
    100          
28 854 100       2102 if ( exists $current_ref->{$current_part} ) {
29 852         1392 $current_ref = $current_ref->{$current_part};
30 852         1386 next;
31             }
32             }
33             elsif ( ref $current_ref eq 'ARRAY' ) {
34 856 100       2485 return undef if $current_part !~ /^\d+$/;
35              
36 854 100       1951 if ( exists $current_ref->[$current_part] ) {
37 852         1318 $current_ref = $current_ref->[$current_part];
38 852         1350 next;
39             }
40             }
41              
42 6         23 return undef;
43             }
44              
45 480         1379 return $current_ref;
46             }
47              
48             sub set {
49 508     508 1 3870232 my ( $root_ref, $root_path, $value ) = @_;
50              
51 508 100       1597 return undef unless defined $root_path;
52 502         2093 $root_path =~ s/^\///;
53              
54 502         1765 my @root_parts = split '/', $root_path;
55 502         902 my $current_ref = $root_ref;
56              
57 502 100       1364 return undef unless @root_parts;
58              
59 496         1608 for ( my $i = 0; $i < ( @root_parts - 1 ); $i++ ) {
60 1252         2427 my $current_part = $root_parts[ $i ];
61 1252         2007 my $next_part = $root_parts[ $i + 1 ];
62              
63 1252 100       3071 if ( ref $current_ref eq 'HASH' ) {
    100          
64 624 100       1665 if ( not ref $current_ref->{$current_part} ) {
65 12 50       47 $current_ref->{$current_part}
66             = $next_part =~ /^\d+$/
67             ? []
68             : {};
69             }
70              
71 624         926 $current_ref = $current_ref->{$current_part};
72 624         1408 next;
73             }
74             elsif ( ref $current_ref eq 'ARRAY' ) {
75 624 100       2037 return undef if $current_part !~ /^\d+$/;
76              
77 620 100       1607 if ( not ref $current_ref->[$current_part] ) {
78 8 50       43 $current_ref->[$current_part]
79             = $next_part =~ /^\d+$/
80             ? []
81             : {};
82             }
83              
84 620         844 $current_ref = $current_ref->[$current_part];
85 620         1582 next;
86             }
87              
88 4         13 return undef;
89             }
90              
91 488         980 my $last_part = pop @root_parts;
92              
93 488 100       1167 if ( ref $current_ref eq 'HASH' ) {
94 244         1062 return $current_ref->{$last_part} = $value;
95             }
96              
97 244 50       480 if ( ref $current_ref eq 'ARRAY' ) {
98 244 50       671 return undef if $last_part !~ /^\d+$/;
99 244         1150 return $current_ref->[$last_part] = $value;
100             }
101              
102 0           return undef;
103             }
104              
105             1;
106              
107             __END__