line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Config-Model |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2005-2022 by Dominique Dumont. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software, licensed under: |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# The GNU Lesser General Public License, Version 2.1, February 1999 |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
package Config::Model::Backend::PerlFile 2.153; # TRIAL |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
34
|
use 5.10.1; |
|
2
|
|
|
|
|
8
|
|
13
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
165
|
|
14
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
27
|
|
|
2
|
|
|
|
|
41
|
|
15
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
82
|
|
16
|
2
|
|
|
2
|
|
15
|
use Config::Model::Exception; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
76
|
|
17
|
2
|
|
|
2
|
|
13
|
use File::Path; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
125
|
|
18
|
2
|
|
|
2
|
|
11
|
use Log::Log4perl qw(get_logger :levels); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
17
|
|
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
2
|
|
331
|
use base qw/Config::Model::Backend::Any/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
993
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $logger = get_logger("Backend::PerlFile"); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub read { |
25
|
12
|
|
|
12
|
1
|
25
|
my $self = shift; |
26
|
12
|
|
|
|
|
95
|
my %args = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# args is: |
29
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
30
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
31
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
32
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
33
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
34
|
|
|
|
|
|
|
# check => yes|no|skip |
35
|
|
|
|
|
|
|
|
36
|
12
|
|
|
|
|
35
|
my $file_path = $args{file_path}; |
37
|
12
|
100
|
|
|
|
55
|
return 0 unless -r $file_path; |
38
|
9
|
50
|
|
|
|
228
|
$file_path = "./$file_path" unless $file_path =~ m!^\.?/!; |
39
|
9
|
|
|
|
|
163
|
$logger->info("Read Perl data from $file_path"); |
40
|
|
|
|
|
|
|
|
41
|
9
|
|
50
|
|
|
3469
|
my $pdata = do $file_path || die "Cannot open $file_path:$?"; |
42
|
9
|
|
|
|
|
86
|
$self->node->load_data($pdata); |
43
|
9
|
|
|
|
|
62
|
return 1; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub write { |
47
|
5
|
|
|
5
|
1
|
14
|
my $self = shift; |
48
|
5
|
|
|
|
|
27
|
my %args = @_; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# args is: |
51
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
52
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
53
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
54
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
55
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
56
|
|
|
|
|
|
|
# check => yes|no|skip |
57
|
|
|
|
|
|
|
|
58
|
5
|
|
|
|
|
14
|
my $file_path = $args{file_path}; |
59
|
5
|
|
|
|
|
33
|
$logger->info("Write perl data to $file_path"); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $p_data = $self->node->dump_as_data( |
62
|
|
|
|
|
|
|
skip_auto_write => 'perl_file', |
63
|
|
|
|
|
|
|
check => $args{check} |
64
|
5
|
|
|
|
|
122
|
); |
65
|
5
|
|
|
|
|
59
|
my $dumper = Data::Dumper->new( [$p_data] ); |
66
|
5
|
|
|
|
|
245
|
$dumper->Terse(1); |
67
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
66
|
$args{file_path}->spew_utf8( $dumper->Dump, ";\n" ); |
69
|
|
|
|
|
|
|
|
70
|
5
|
|
|
|
|
5159
|
return 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# ABSTRACT: Read and write config as a Perl data structure |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
__END__ |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=pod |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=encoding UTF-8 |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 NAME |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Config::Model::Backend::PerlFile - Read and write config as a Perl data structure |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 VERSION |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
version 2.153 |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SYNOPSIS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
use Config::Model ; |
94
|
|
|
|
|
|
|
use Data::Dumper ; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# define configuration tree object |
97
|
|
|
|
|
|
|
my $model = Config::Model->new ; |
98
|
|
|
|
|
|
|
$model ->create_config_class ( |
99
|
|
|
|
|
|
|
name => "MyClass", |
100
|
|
|
|
|
|
|
element => [ |
101
|
|
|
|
|
|
|
[qw/foo bar/] => { |
102
|
|
|
|
|
|
|
type => 'leaf', |
103
|
|
|
|
|
|
|
value_type => 'string' |
104
|
|
|
|
|
|
|
}, |
105
|
|
|
|
|
|
|
baz => { |
106
|
|
|
|
|
|
|
type => 'hash', |
107
|
|
|
|
|
|
|
index_type => 'string' , |
108
|
|
|
|
|
|
|
cargo => { |
109
|
|
|
|
|
|
|
type => 'leaf', |
110
|
|
|
|
|
|
|
value_type => 'string', |
111
|
|
|
|
|
|
|
}, |
112
|
|
|
|
|
|
|
}, |
113
|
|
|
|
|
|
|
], |
114
|
|
|
|
|
|
|
rw_config => { |
115
|
|
|
|
|
|
|
backend => 'perl_file' , |
116
|
|
|
|
|
|
|
config_dir => '/tmp', |
117
|
|
|
|
|
|
|
file => 'foo.pl', |
118
|
|
|
|
|
|
|
auto_create => 1, |
119
|
|
|
|
|
|
|
}, |
120
|
|
|
|
|
|
|
) ; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $inst = $model->instance(root_class_name => 'MyClass' ); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $root = $inst->config_root ; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $steps = 'foo=yada bar="bla bla" baz:en=hello |
127
|
|
|
|
|
|
|
baz:fr=bonjour baz:hr="dobar dan"'; |
128
|
|
|
|
|
|
|
$root->load( steps => $steps ) ; |
129
|
|
|
|
|
|
|
$inst->write_back ; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Now, C</tmp/foo.pl> contains: |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
{ |
134
|
|
|
|
|
|
|
bar => 'bla bla', |
135
|
|
|
|
|
|
|
baz => { |
136
|
|
|
|
|
|
|
en => 'hello', |
137
|
|
|
|
|
|
|
fr => 'bonjour', |
138
|
|
|
|
|
|
|
hr => 'dobar dan' |
139
|
|
|
|
|
|
|
}, |
140
|
|
|
|
|
|
|
foo => 'yada' |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This module is used directly by L<Config::Model> to read or write the |
146
|
|
|
|
|
|
|
content of a configuration tree written with Perl syntax in |
147
|
|
|
|
|
|
|
C<Config::Model> configuration tree. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Note: |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=over 4 |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item * |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Undefined values are skipped for list element. I.e. if a |
156
|
|
|
|
|
|
|
list element contains C<('a',undef,'b')>, the data structure |
157
|
|
|
|
|
|
|
contains C<'a','b'>. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item * |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Perl file is not created (and may be deleted) when no data is to be |
162
|
|
|
|
|
|
|
written. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 backend parameter |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 config_dir |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Mandoatory parameter to specify where is the Perl configuration file. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 new |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Inherited from L<Config::Model::Backend::Any>. The constructor is |
177
|
|
|
|
|
|
|
called by L<Config::Model::BackendMgr>. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 read |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Of all parameters passed to this read call-back, only C<ifile_path> is |
182
|
|
|
|
|
|
|
used. This parameter must be L<IO::File> object already opened for |
183
|
|
|
|
|
|
|
read. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
It can also be undef. In which case C<read> returns 0. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
When a file is read, C<read> returns 1. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 write |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Of all parameters passed to this write call-back, only C<file_path> is |
192
|
|
|
|
|
|
|
used. This parameter must be a L<Path::Tiny> object. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
C<write> returns 1. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 AUTHOR |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Dominique Dumont, (ddumont at cpan dot org) |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 SEE ALSO |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
L<Config::Model>, |
203
|
|
|
|
|
|
|
L<Config::Model::BackendMgr>, |
204
|
|
|
|
|
|
|
L<Config::Model::Backend::Any>, |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 AUTHOR |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Dominique Dumont |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
This software is Copyright (c) 2005-2022 by Dominique Dumont. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This is free software, licensed under: |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
The GNU Lesser General Public License, Version 2.1, February 1999 |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |