| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Hash::Path; |
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
82086
|
use warnings; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
111
|
|
|
4
|
3
|
|
|
3
|
|
15
|
use strict; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
280
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
17
|
use base 'Exporter'; |
|
|
3
|
|
|
|
|
11
|
|
|
|
3
|
|
|
|
|
961
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw(hash_path); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub get { |
|
13
|
12
|
|
|
12
|
1
|
76
|
my ($class, $data_ref, @path) = @_; |
|
14
|
12
|
50
|
|
|
|
40
|
return $data_ref unless scalar @path; |
|
15
|
12
|
|
|
|
|
27
|
my $return_value = $data_ref->{ $path[0] }; |
|
16
|
12
|
|
|
|
|
38
|
for (1 .. (scalar @path - 1)) { |
|
17
|
26
|
|
|
|
|
74
|
$return_value = $return_value->{ $path[$_] }; |
|
18
|
|
|
|
|
|
|
} |
|
19
|
12
|
|
|
|
|
72
|
return $return_value; |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub hash_path { |
|
23
|
6
|
|
|
6
|
1
|
61
|
return __PACKAGE__->get(@_); |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
1; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=pod |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 NAME |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Hash::Path - A simple way to return a path of HoH |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 VERSION |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
0.02 |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
use Hash::Path; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $hash_ref = { |
|
43
|
|
|
|
|
|
|
key1 => { |
|
44
|
|
|
|
|
|
|
key2 => { |
|
45
|
|
|
|
|
|
|
key3 => 'value', |
|
46
|
|
|
|
|
|
|
}, |
|
47
|
|
|
|
|
|
|
}, |
|
48
|
|
|
|
|
|
|
}; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $wanted = Hash::Path->( $hash_ref, qw{key1 key2 key3} ); |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# $wanted contains 'value' |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
This module was written as proof of concept about how to find data inside a hash of hashes (HoH) with |
|
57
|
|
|
|
|
|
|
unknown structure. You can think that as hierarchical data like LDAP does, so our C could be the |
|
58
|
|
|
|
|
|
|
exactly the same as LDAP's C, but a bit simpler because we (at least at this moment, who knows) don't |
|
59
|
|
|
|
|
|
|
want to deal with that. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
This is a perfect companion for traversing L: |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
use Hash::Path; |
|
64
|
|
|
|
|
|
|
use YAML; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my ($hash_ref) = Load(<<'EOF'); |
|
67
|
|
|
|
|
|
|
--- |
|
68
|
|
|
|
|
|
|
name: john |
|
69
|
|
|
|
|
|
|
permissions: |
|
70
|
|
|
|
|
|
|
some-module: |
|
71
|
|
|
|
|
|
|
- read |
|
72
|
|
|
|
|
|
|
- write |
|
73
|
|
|
|
|
|
|
- execute |
|
74
|
|
|
|
|
|
|
another-module: |
|
75
|
|
|
|
|
|
|
- read |
|
76
|
|
|
|
|
|
|
EOF |
|
77
|
|
|
|
|
|
|
my $permissions = Hash::Path->get($hash_ref, qw(permissions some-module)); |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# $permissions contains [ 'read', 'write', 'execute' ] |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 API |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=over 4 |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item get |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
$scalar = Hash::Path->get($hash_ref, @path); |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This is the only available method. It traverses the hash reference using the supplied path array, |
|
90
|
|
|
|
|
|
|
returning the value as scalar value. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item hash_path |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
use Hash::Path qw(hash_path); |
|
95
|
|
|
|
|
|
|
$scalar = hash_path($hash_ref, @path); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Now you can export the C function to be a bit shorter. The |
|
98
|
|
|
|
|
|
|
parameters it takes are the same as C. |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=back |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Thanks to Arthur Axel "fREW" Schmidt "" for using this |
|
105
|
|
|
|
|
|
|
module and suggesting a better implementation for the C method. |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 AUTHOR |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Copyright (c) 2007, Igor Sutton Lopes "". All rights reserved. |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it under |
|
112
|
|
|
|
|
|
|
the same terms as Perl itself. |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |