line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CTK::ConfGenUtil; # $Id: ConfGenUtil.pm 222 2019-05-01 14:44:03Z minus $ |
2
|
3
|
|
|
3
|
|
77039
|
use strict; |
|
3
|
|
|
|
|
14
|
|
|
3
|
|
|
|
|
104
|
|
3
|
3
|
|
|
3
|
|
1375
|
use utf8; |
|
3
|
|
|
|
|
19
|
|
|
3
|
|
|
|
|
17
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=encoding utf-8 |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
CTK::ConfGenUtil - Config::General structure utility functions |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 2.68 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use CTK::ConfGenUtil; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
# Baz qux |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
# |
24
|
|
|
|
|
|
|
my $foo = node( $config, 'foo' ); # { bar => { baz => 'qux' } } |
25
|
|
|
|
|
|
|
my $bar = node( $config, 'foo', 'bar' ); # { baz => 'qux' } |
26
|
|
|
|
|
|
|
my $bar = node( $config, ['foo', 'bar'] ); # { baz => 'qux' } |
27
|
|
|
|
|
|
|
my $bar = node( $config, 'foo/bar' ); # { baz => 'qux' } |
28
|
|
|
|
|
|
|
my $baz = value( $config, 'foo/bar/baz' ); # qux |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Foo bar |
31
|
|
|
|
|
|
|
my $foo = value( $config, 'foo' ); # bar |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Foo 123 |
34
|
|
|
|
|
|
|
# Foo 456 |
35
|
|
|
|
|
|
|
# Foo 789 |
36
|
|
|
|
|
|
|
my $foo = array( $config, 'foo' ); # [123,456,789] |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# |
39
|
|
|
|
|
|
|
# Bar baz |
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
my $foo = hash( $config, 'foo' ); # { bar => 'baz' } |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# |
44
|
|
|
|
|
|
|
# |
45
|
|
|
|
|
|
|
# Baz blah-blah-blah |
46
|
|
|
|
|
|
|
# Qux 123 |
47
|
|
|
|
|
|
|
# Qux 456 |
48
|
|
|
|
|
|
|
# Qux 789 |
49
|
|
|
|
|
|
|
# |
50
|
|
|
|
|
|
|
# |
51
|
|
|
|
|
|
|
is_scalar( $foo ); |
52
|
|
|
|
|
|
|
print "Is scalar : ", is_scalar($config, 'foo/bar/baz') ? 'OK' : 'NO'; # OK |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
is_array( $foo ); |
55
|
|
|
|
|
|
|
print "Is array : ", is_array($config, 'foo/bar/qux') ? 'OK' : 'NO'; # OK |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
is_hash( $foo ); |
58
|
|
|
|
|
|
|
print "Is hash : ", is_hash($config, 'foo/bar') ? 'OK' : 'NO'; # OK |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 DESCRIPTION |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
This module based on L |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 FUNCTIONS |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Working sample: |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Baz blah-blah-blah |
71
|
|
|
|
|
|
|
Qux 123 |
72
|
|
|
|
|
|
|
Qux 456 |
73
|
|
|
|
|
|
|
Qux 789 |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=over 8 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item B |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
This method returns the found node of a given key. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $bar = node( $config, 'foo', 'bar' ); |
84
|
|
|
|
|
|
|
my $bar = node( $config, ['foo', 'bar'] ); |
85
|
|
|
|
|
|
|
my $bar = node( $config, 'foo/bar' ); |
86
|
|
|
|
|
|
|
my $bar = node( $config, ['foo/bar'] ); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $bar_hash = hash($bar); |
89
|
|
|
|
|
|
|
my $baz = value($bar, 'baz'); # blah-blah-blah |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item B |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
This method returns the scalar value of a given key. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $baz = value( $config, 'foo/bar/baz' ); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item B |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
This method returns a array reference (if it B one!) from the config which is referenced by |
100
|
|
|
|
|
|
|
"key". Given the sample config above you would get: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $qux = array( $config, 'foo/bar/qux' ); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item B |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This method returns a hash reference (if it B one!) from the config which is referenced by |
107
|
|
|
|
|
|
|
"key". Given the sample config above you would get: |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
my $bar = hash( $config, 'foo/bar' ); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item B, B |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
As seen above, you can access parts of your current config using hash, array or scalar |
114
|
|
|
|
|
|
|
functions. This function returns just true if the given key is scalar (regular value) |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
is_scalar( $baz ); |
117
|
|
|
|
|
|
|
is_scalar( $config, 'foo/bar/baz' ); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item B |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
As seen above, you can access parts of your current config using hash, array or scalar |
122
|
|
|
|
|
|
|
functions. This function returns just true if the given key is array (reference) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
is_array( $qux ); |
125
|
|
|
|
|
|
|
is_array( $config, 'foo/bar/qux' ); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item B |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
As seen above, you can access parts of your current config using hash, array or scalar |
130
|
|
|
|
|
|
|
functions. This function returns just true if the given key is hash (reference) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
is_hash( $bar ); |
133
|
|
|
|
|
|
|
is_hash( $config, 'foo/bar' ); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=back |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 HISTORY |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
See C file |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 TO DO |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
See C file |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 BUGS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
* none noted |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 SEE ALSO |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
L |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 AUTHOR |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 COPYRIGHT |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 LICENSE |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
164
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
See C file and L |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
169
|
|
|
|
|
|
|
|
170
|
3
|
|
|
3
|
|
177
|
use vars qw/$VERSION/; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
175
|
|
171
|
|
|
|
|
|
|
$VERSION = '2.68'; |
172
|
|
|
|
|
|
|
|
173
|
3
|
|
|
3
|
|
19
|
use base qw/Exporter/; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
2808
|
|
174
|
|
|
|
|
|
|
our @EXPORT = qw/ node value array hash is_value is_scalar is_array is_hash /; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub node { |
177
|
|
|
|
|
|
|
# |
178
|
|
|
|
|
|
|
# getnode( $config, [qw/foo bar baz/] ) |
179
|
|
|
|
|
|
|
# getnode( $config, qw/foo bar baz/ ) |
180
|
|
|
|
|
|
|
# getnode( $config, 'foo' ) |
181
|
|
|
|
|
|
|
# getnode( $config, 'foo', 'bar/baz' ) |
182
|
|
|
|
|
|
|
# |
183
|
1
|
|
50
|
1
|
1
|
4
|
my $cc = shift || {}; |
184
|
1
|
|
50
|
|
|
4
|
my $ar = shift || []; |
185
|
1
|
|
|
|
|
3
|
my %rcc = (); |
186
|
1
|
50
|
|
|
|
11
|
%rcc = %$cc if ref($cc) eq 'HASH'; |
187
|
1
|
|
|
|
|
3
|
my @arcc = (); |
188
|
1
|
50
|
|
|
|
4
|
@arcc = @$cc if ref($cc) eq 'ARRAY'; |
189
|
1
|
|
|
|
|
3
|
my @rar = (); |
190
|
1
|
50
|
|
|
|
5
|
if (ref($ar) eq 'ARRAY') { |
191
|
0
|
|
|
|
|
0
|
push @rar, split(/\//, $_) for (grep {$_} (@$ar)); |
|
0
|
|
|
|
|
0
|
|
192
|
|
|
|
|
|
|
} else { |
193
|
1
|
|
|
|
|
2
|
push @rar, split(/\//, $_) for (grep {$_} ($ar,@_)); |
|
1
|
|
|
|
|
8
|
|
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
1
|
|
|
|
|
3
|
my $tnode = \%rcc; |
197
|
1
|
|
|
|
|
2
|
my $laststat = 0; |
198
|
|
|
|
|
|
|
|
199
|
1
|
|
|
|
|
2
|
foreach my $k (@rar) { |
200
|
|
|
|
|
|
|
#debug $k; |
201
|
3
|
50
|
33
|
|
|
20
|
if ($tnode && (ref($tnode) eq 'HASH') && defined($tnode->{$k})) { |
|
|
|
33
|
|
|
|
|
202
|
3
|
|
|
|
|
13
|
$tnode = $tnode->{$k}; |
203
|
3
|
|
|
|
|
7
|
$laststat = 1; |
204
|
|
|
|
|
|
|
} else { |
205
|
|
|
|
|
|
|
#debug Dumper($tnode); |
206
|
0
|
|
|
|
|
0
|
$laststat = 0; |
207
|
0
|
|
|
|
|
0
|
next; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
} |
210
|
1
|
0
|
33
|
|
|
4
|
if (!$laststat && @arcc && defined($arcc[0])) { |
|
|
|
33
|
|
|
|
|
211
|
0
|
|
0
|
|
|
0
|
my $kk = pop(@rar) || ''; |
212
|
0
|
0
|
|
|
|
0
|
if ($kk) { |
213
|
0
|
|
|
|
|
0
|
foreach my $an (@arcc) { |
214
|
0
|
0
|
0
|
|
|
0
|
if ($an && (ref($an) eq 'HASH') && defined($an->{$kk})) { |
|
|
|
0
|
|
|
|
|
215
|
0
|
|
|
|
|
0
|
$tnode = $an->{$kk}; |
216
|
0
|
|
|
|
|
0
|
$laststat = 1; |
217
|
0
|
|
|
|
|
0
|
last; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
1
|
50
|
|
|
|
7
|
return $laststat ? $tnode : undef; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
sub value { |
226
|
7
|
|
|
7
|
1
|
105
|
my $node = shift; |
227
|
7
|
100
|
|
|
|
23
|
$node = node($node, @_) if defined($_[0]); |
228
|
7
|
100
|
100
|
|
|
78
|
if ($node && ref($node) eq 'ARRAY') { |
|
|
100
|
100
|
|
|
|
|
229
|
2
|
100
|
|
|
|
12
|
return exists($node->[0]) ? $node->[0] : undef; |
230
|
|
|
|
|
|
|
} elsif (defined($node) && !ref($node)) { |
231
|
3
|
|
|
|
|
17
|
return $node |
232
|
|
|
|
|
|
|
} else { |
233
|
|
|
|
|
|
|
return undef |
234
|
2
|
|
|
|
|
12
|
} |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
sub array { |
237
|
3
|
|
|
3
|
1
|
8
|
my $node = shift; |
238
|
3
|
50
|
|
|
|
10
|
$node = node($node, @_) if defined $_[0]; |
239
|
3
|
50
|
33
|
|
|
20
|
if ($node && ref($node) eq 'ARRAY') { |
|
|
100
|
66
|
|
|
|
|
240
|
0
|
|
|
|
|
0
|
return $node; |
241
|
|
|
|
|
|
|
} elsif (defined($node) && !ref($node)) { |
242
|
2
|
|
|
|
|
11
|
return [$node]; |
243
|
|
|
|
|
|
|
} else { |
244
|
1
|
|
|
|
|
5
|
return []; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
sub hash { |
248
|
0
|
|
0
|
0
|
1
|
|
my $node = shift || {}; |
249
|
0
|
0
|
|
|
|
|
$node = node($node, @_) if defined $_[0]; |
250
|
0
|
0
|
0
|
|
|
|
if ($node && ref($node) eq 'HASH') { |
251
|
0
|
|
|
|
|
|
return $node; |
252
|
|
|
|
|
|
|
} else { |
253
|
0
|
|
|
|
|
|
return {}; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
sub is_hash { |
257
|
0
|
|
|
0
|
1
|
|
my $node = shift; |
258
|
0
|
0
|
|
|
|
|
$node = node($node, @_) if defined($_[0]); |
259
|
0
|
0
|
0
|
|
|
|
return 1 if $node && ref($node) eq 'HASH'; |
260
|
0
|
|
|
|
|
|
return; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
sub is_array { |
263
|
0
|
|
|
0
|
1
|
|
my $node = shift; |
264
|
0
|
0
|
|
|
|
|
$node = node($node,@_) if defined($_[0]); |
265
|
0
|
0
|
0
|
|
|
|
return 1 if $node && ref($node) eq 'ARRAY'; |
266
|
0
|
|
|
|
|
|
return; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
sub is_value { |
269
|
0
|
|
|
0
|
1
|
|
my $node = shift; |
270
|
0
|
0
|
|
|
|
|
$node = node($node, @_) if defined($_[0]); |
271
|
0
|
0
|
0
|
|
|
|
return 1 if defined($node) && !ref($node); |
272
|
0
|
|
|
|
|
|
return; |
273
|
|
|
|
|
|
|
} |
274
|
0
|
|
|
0
|
1
|
|
sub is_scalar { goto &is_value } |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
1; |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
__END__ |