line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: Set of functions for working with yaml config files |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package App::ygeo::yaml; |
4
|
|
|
|
|
|
|
$App::ygeo::yaml::VERSION = '0.01'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
63726
|
use YAML::Tiny; |
|
1
|
|
|
|
|
4215
|
|
|
1
|
|
|
|
|
286
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require Exporter; |
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(data_from_first_valid_cfg create_cfg keys_exists_no_empty); |
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'ALL' => [@EXPORT_OK] ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub data_from_first_valid_cfg { |
16
|
0
|
|
|
0
|
0
|
0
|
my ( $config_files, $required_keys ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
0
|
for my $file (@$config_files) { |
19
|
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
0
|
if ( -e $file ) { |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
0
|
my $cfg = YAML::Tiny->read($file)->[0]; |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
0
|
return $cfg if keys_exists_no_empty( $cfg, $required_keys ); |
25
|
0
|
|
|
|
|
0
|
next; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
0
|
return; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub keys_exists_no_empty { |
36
|
3
|
|
|
3
|
0
|
887
|
my ( $hash, $required_keys ) = @_; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
4
|
my $i = 0; |
39
|
|
|
|
|
|
|
|
40
|
3
|
|
|
|
|
4
|
for my $k (@$required_keys) { |
41
|
6
|
100
|
100
|
|
|
22
|
if ( defined $hash->{$k} && length $hash->{$k} ) { |
42
|
4
|
|
|
|
|
14
|
$i++; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
3
|
100
|
|
|
|
14
|
$i == scalar @$required_keys ? 1 : 0; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub create_cfg { |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
0
|
0
|
|
my ( $cfg, @required_keys ) = @_; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
open( FILE, ">", $cfg ) || die "cannot open file $cfg: " . $!; |
55
|
0
|
|
|
|
|
|
print FILE ""; |
56
|
0
|
|
|
|
|
|
close FILE; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $cfg_values = {}; |
59
|
0
|
|
|
|
|
|
for my $k (@required_keys) { |
60
|
0
|
|
|
|
|
|
print "$k: "; |
61
|
0
|
|
|
|
|
|
chomp( $cfg_values->{$k} = ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my $yaml = YAML::Tiny->new($cfg_values); |
65
|
0
|
|
|
|
|
|
$yaml->write($cfg); |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return $cfg_values; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |