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   42611 use strict;
  2         4  
  2         70  
4 2     2   9 use warnings;
  2         3  
  2         53  
5              
6 2     2   1682 use version 0.77;
  2         4619  
  2         14  
7             our $VERSION = qv("v1.0.2");
8              
9 2     2   171 use base 'Exporter';
  2         3  
  2         1391  
10             our @EXPORT_OK = qw{
11             get
12             set
13             };
14              
15             sub get {
16 494     494 1 4443039 my ( $root_ref, $root_path ) = @_;
17              
18 494 100       2701 return undef unless defined $root_path;
19 491         2102 $root_path =~ s/^\///;
20              
21 491         2922 my @root_parts = split '/', $root_path;
22 491         1292 my $current_ref = $root_ref;
23              
24 491 100       1934 return undef unless @root_parts;
25              
26 488         1044 foreach my $current_part ( @root_parts ) {
27 1712 100       5451 if ( ref $current_ref eq 'HASH' ) {
    100          
28 854 100       2493 if ( exists $current_ref->{$current_part} ) {
29 852         1276 $current_ref = $current_ref->{$current_part};
30 852         1376 next;
31             }
32             }
33             elsif ( ref $current_ref eq 'ARRAY' ) {
34 856 100       2812 return undef if $current_part !~ /^\d+$/;
35              
36 854 100       2715 if ( exists $current_ref->[$current_part] ) {
37 852         1313 $current_ref = $current_ref->[$current_part];
38 852         1713 next;
39             }
40             }
41              
42 6         21 return undef;
43             }
44              
45 480         1872 return $current_ref;
46             }
47              
48             sub set {
49 508     508 1 4579666 my ( $root_ref, $root_path, $value ) = @_;
50              
51 508 100       2407 return undef unless defined $root_path;
52 502         2786 $root_path =~ s/^\///;
53              
54 502         2979 my @root_parts = split '/', $root_path;
55 502         1103 my $current_ref = $root_ref;
56              
57 502 100       1466 return undef unless @root_parts;
58              
59 496         2517 for ( my $i = 0; $i < ( @root_parts - 1 ); $i++ ) {
60 1252         3155 my $current_part = $root_parts[ $i ];
61 1252         2073 my $next_part = $root_parts[ $i + 1 ];
62              
63 1252 100       4497 if ( ref $current_ref eq 'HASH' ) {
    100          
64 624 100       2459 if ( not ref $current_ref->{$current_part} ) {
65 12 50       55 $current_ref->{$current_part}
66             = $next_part =~ /^\d+$/
67             ? []
68             : {};
69             }
70              
71 624         933 $current_ref = $current_ref->{$current_part};
72 624         1983 next;
73             }
74             elsif ( ref $current_ref eq 'ARRAY' ) {
75 624 100       2629 return undef if $current_part !~ /^\d+$/;
76              
77 620 100       2426 if ( not ref $current_ref->[$current_part] ) {
78 8 50       62 $current_ref->[$current_part]
79             = $next_part =~ /^\d+$/
80             ? []
81             : {};
82             }
83              
84 620         1096 $current_ref = $current_ref->[$current_part];
85 620         2016 next;
86             }
87              
88 4         15 return undef;
89             }
90              
91 488         1402 my $last_part = pop @root_parts;
92              
93 488 100       2142 if ( ref $current_ref eq 'HASH' ) {
94 244         1314 return $current_ref->{$last_part} = $value;
95             }
96              
97 244 50       643 if ( ref $current_ref eq 'ARRAY' ) {
98 244 50       1173 return undef if $last_part !~ /^\d+$/;
99 244         1688 return $current_ref->[$last_part] = $value;
100             }
101              
102 0           return undef;
103             }
104              
105             1;
106              
107             __END__