| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package BusyBird::SafeData; |
|
2
|
18
|
|
|
18
|
|
15749
|
use strict; |
|
|
18
|
|
|
|
|
24
|
|
|
|
18
|
|
|
|
|
561
|
|
|
3
|
18
|
|
|
18
|
|
73
|
use warnings; |
|
|
18
|
|
|
|
|
19
|
|
|
|
18
|
|
|
|
|
412
|
|
|
4
|
18
|
|
|
18
|
|
65
|
use Exporter qw(import); |
|
|
18
|
|
|
|
|
24
|
|
|
|
18
|
|
|
|
|
400
|
|
|
5
|
18
|
|
|
18
|
|
9015
|
use Data::Diver (); |
|
|
18
|
|
|
|
|
13690
|
|
|
|
18
|
|
|
|
|
3181
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our @EXPORT_OK = qw(safed); |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
|
10
|
1609
|
|
|
1609
|
1
|
2010
|
my ($class, $data) = @_; |
|
11
|
1609
|
|
|
|
|
4754
|
return bless \$data, $class; |
|
12
|
|
|
|
|
|
|
} |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub safed { |
|
15
|
1609
|
|
|
1609
|
1
|
6350
|
return __PACKAGE__->new(@_); |
|
16
|
|
|
|
|
|
|
} |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub original { |
|
19
|
95
|
|
|
95
|
1
|
7455
|
return ${shift()}; |
|
|
95
|
|
|
|
|
292
|
|
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub val { |
|
23
|
3478
|
|
|
3478
|
1
|
47827
|
my ($self, @path) = @_; |
|
24
|
3478
|
|
|
|
|
4394
|
return scalar(Data::Diver::Dive($$self, map { "$_" } @path)); |
|
|
5489
|
|
|
|
|
11877
|
|
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub array { |
|
28
|
81
|
|
|
81
|
1
|
3834
|
my ($self, @path) = @_; |
|
29
|
81
|
|
|
|
|
121
|
my $val = $self->val(@path); |
|
30
|
81
|
100
|
|
|
|
2123
|
if(ref($val) eq 'ARRAY') { |
|
31
|
6
|
|
|
|
|
21
|
return @$val; |
|
32
|
|
|
|
|
|
|
}else { |
|
33
|
75
|
|
|
|
|
207
|
return (); |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
1; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=pod |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 NAME |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
BusyBird::SafeData - a wrapper of a complex data structure to access its internals safely |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
use BusyBird::SafeData qw(safed); |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $data = { |
|
50
|
|
|
|
|
|
|
foo => { |
|
51
|
|
|
|
|
|
|
bar => [ |
|
52
|
|
|
|
|
|
|
0, 1, 2 |
|
53
|
|
|
|
|
|
|
], |
|
54
|
|
|
|
|
|
|
buzz => { hoge => 100 } |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
}; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $sd = safed($data); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$sd->original; ## => $data |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$sd->val("foo", "bar", 1); ## => 1 |
|
63
|
|
|
|
|
|
|
$sd->val("foo", "buzz", "FOO"); ## => undef |
|
64
|
|
|
|
|
|
|
$sd->val("foo", "quux", "hoge"); ## => undef (and no autovivification) |
|
65
|
|
|
|
|
|
|
$sd->val("foo", "bar", "FOO"); ## => undef (and no exception thrown) |
|
66
|
|
|
|
|
|
|
$sd->val("foo", "buzz", "hoge", "FOO"); ## => undef (and no exception thrown) |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
$sd->array("foo", "bar"); ## => (0, 1, 2) |
|
69
|
|
|
|
|
|
|
$sd->array("foo", "buzz"); ## => () |
|
70
|
|
|
|
|
|
|
$sd->array("foo", "bar", 1); ## => () |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
L is a wrapper around a complex data structure to provide a safe way to access its internal data. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 EXPORTABLE FUNCTIONS |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
The following function is exported only by request. |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 $sd = safed($data) |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Same as C<< BusyBird::SafeData->new($data). >> |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 CLASS METHODS |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 $sd = BusyBird::SafeData->new($data) |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
The constructor. |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
C<$data> is any scalar. C<$sd> wraps the given C<$data>. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 OBJECT METHODS |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 $data = $sd->original() |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Returns the original C<$data> that C<$sd> wraps. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 $val = $sd->val(@path) |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Return the value specified by C<@path> from C<$sd>. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
C<@path> is a list of indices/keys from the root of the C<$sd> down to the value you want. |
|
103
|
|
|
|
|
|
|
If it cannot traverse C<@path> completely, it returns C. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
This method never autovivifies anything in C<$sd>. |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 @vals = $sd->array(@path) |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Return the list of values in the array-ref specified by C<@path>. |
|
110
|
|
|
|
|
|
|
If it cannot traverse C<@path> completely, it returns an empty list. |
|
111
|
|
|
|
|
|
|
If the value at the C<@path> is not an array-ref, it returns an empty list. |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 AUTHOR |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Toshio Ito C<< >> |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |