| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Config::Merge; |
|
2
|
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
156183
|
use strict; |
|
|
5
|
|
|
|
|
16
|
|
|
|
5
|
|
|
|
|
249
|
|
|
4
|
5
|
|
|
5
|
|
30
|
use warnings FATAL => 'all', NONFATAL => 'redefine'; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
437
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
29
|
use File::Spec(); |
|
|
5
|
|
|
|
|
13
|
|
|
|
5
|
|
|
|
|
122
|
|
|
7
|
5
|
|
|
5
|
|
119
|
use File::Glob 'bsd_glob'; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
743
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
5
|
|
|
5
|
|
6490
|
use Storable(); |
|
|
5
|
|
|
|
|
22533
|
|
|
|
5
|
|
|
|
|
368
|
|
|
10
|
|
|
|
|
|
|
use overload ( |
|
11
|
|
|
|
|
|
|
'&{}' => sub { |
|
12
|
9
|
|
|
9
|
|
26
|
my $self = shift; |
|
13
|
9
|
|
|
9
|
|
37
|
return sub { $self->C(@_) } |
|
14
|
9
|
|
|
|
|
51
|
}, |
|
15
|
5
|
|
|
|
|
49
|
'fallback' => 1 |
|
16
|
5
|
|
|
5
|
|
12052
|
); |
|
|
5
|
|
|
|
|
6137
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
5
|
|
|
5
|
|
389
|
use vars qw($VERSION); |
|
|
5
|
|
|
|
|
11
|
|
|
|
5
|
|
|
|
|
12878
|
|
|
19
|
|
|
|
|
|
|
$VERSION = '1.04'; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 NAME |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Config::Merge - load a configuration directory tree containing |
|
24
|
|
|
|
|
|
|
YAML, JSON, XML, Perl, INI or Config::General files |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
OO style |
|
29
|
|
|
|
|
|
|
------------------------------------------------------- |
|
30
|
|
|
|
|
|
|
use Config::Merge(); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $config = Config::Merge->new('/path/to/config'); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
@hosts = $config->('db.hosts.session'); |
|
35
|
|
|
|
|
|
|
$hosts_ref = $config->('db.hosts.session'); |
|
36
|
|
|
|
|
|
|
@cloned_hosts = $config->clone('db.hosts.session'); |
|
37
|
|
|
|
|
|
|
------------------------------------------------------- |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
OR |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Functional style |
|
42
|
|
|
|
|
|
|
------------------------------------------------------- |
|
43
|
|
|
|
|
|
|
# On startup |
|
44
|
|
|
|
|
|
|
use Config::Merge('My::Config' => '/path/to/config'); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Then, in any module where you want to use the config |
|
48
|
|
|
|
|
|
|
package My::Module; |
|
49
|
|
|
|
|
|
|
use My::Config; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
@hosts = C('db.hosts.sesssion'); |
|
52
|
|
|
|
|
|
|
$hosts_ref = C('db.hosts.sesssion'); |
|
53
|
|
|
|
|
|
|
@cloned_hosts = My::Config::clone('db.hosts.session'); |
|
54
|
|
|
|
|
|
|
$config = My::Config::object; |
|
55
|
|
|
|
|
|
|
------------------------------------------------------- |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
ADVANCED USAGE |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
OO style |
|
60
|
|
|
|
|
|
|
------------------------------------------------------- |
|
61
|
|
|
|
|
|
|
my $config = Config::Merge->new( |
|
62
|
|
|
|
|
|
|
path => '/path/to/config', |
|
63
|
|
|
|
|
|
|
skip => sub {} | regex | {} , |
|
64
|
|
|
|
|
|
|
is_local => sub {} | regex | {} , |
|
65
|
|
|
|
|
|
|
load_as => sub {} | regex , |
|
66
|
|
|
|
|
|
|
sort => sub {} , |
|
67
|
|
|
|
|
|
|
debug => 1 | 0 |
|
68
|
|
|
|
|
|
|
); |
|
69
|
|
|
|
|
|
|
------------------------------------------------------- |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Functional style |
|
72
|
|
|
|
|
|
|
------------------------------------------------------- |
|
73
|
|
|
|
|
|
|
use Config::Merge( |
|
74
|
|
|
|
|
|
|
'My::Config' => '/path/to/config', |
|
75
|
|
|
|
|
|
|
{ |
|
76
|
|
|
|
|
|
|
skip => sub {} | regex | {} , |
|
77
|
|
|
|
|
|
|
is_local => sub {} | regex | {} , |
|
78
|
|
|
|
|
|
|
load_as => sub {} | regex , |
|
79
|
|
|
|
|
|
|
sort => sub {} , |
|
80
|
|
|
|
|
|
|
debug => 1 | 0 |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Also, you can subclass these: |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
package My::Config; |
|
87
|
|
|
|
|
|
|
sub skip { |
|
88
|
|
|
|
|
|
|
... |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
------------------------------------------------------- |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Config::Merge is a configuration module which has six goals: |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=over |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=item * Flexible storage |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Store all configuration in your format(s) of choice (YAML, JSON, INI, XML, Perl, |
|
102
|
|
|
|
|
|
|
Config::General / Apache-style config) broken down into individual files in |
|
103
|
|
|
|
|
|
|
a configuration directory tree, for easy maintenance. |
|
104
|
|
|
|
|
|
|
See L"CONFIG TREE LAYOUT"> |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item * Flexible access |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Provide a simple, easy to read, concise way of accessing the configuration |
|
109
|
|
|
|
|
|
|
values (similar to L). See L"ACCESSING CONFIG DATA"> |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item * Minimal maintenance |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Specify the location of the configuration files only once per |
|
114
|
|
|
|
|
|
|
application, so that it requires minimal effort to relocate. |
|
115
|
|
|
|
|
|
|
See L"USING Config::Merge"> |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item * Easy to alter development environment |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Provide a way for overriding configuration values on a development |
|
120
|
|
|
|
|
|
|
machine, so that differences between the dev environment and |
|
121
|
|
|
|
|
|
|
the live environment do not get copied over accidentally. |
|
122
|
|
|
|
|
|
|
See L"OVERRIDING CONFIG LOCALLY"> |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * Minimise memory use |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Load all config at startup so that (eg in the mod_perl environment) the |
|
127
|
|
|
|
|
|
|
data is shared between all child processes. See L"MINIMISING MEMORY USE"> |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=item * Flexible implementation |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
You may want to use a different schema for your configuration files, |
|
132
|
|
|
|
|
|
|
so you can pass in (or subclass) methods for determining how your |
|
133
|
|
|
|
|
|
|
files are merged. See L"ADVANCED USAGE">. |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=back |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 USING C |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
There are two ways to use C: |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=over |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item OO STYLE |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
use Config::Merge(); |
|
146
|
|
|
|
|
|
|
my $config = Config::Merge->new('/path/to/config'); |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
@hosts = $config->('db.hosts.session'); |
|
149
|
|
|
|
|
|
|
$hosts_ref = $config->('db.hosts.session'); |
|
150
|
|
|
|
|
|
|
@cloned_hosts = $config->clone('db.hosts.session'); |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Also, see L"ADVANCED USAGE">. |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item YOUR OWN CONFIG CLASS (functional style) |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
The following code: |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# On startup |
|
159
|
|
|
|
|
|
|
use Config::Merge('My::Config' => '/path/to/config'); |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=over |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item * |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
auto-generates the class C |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item * |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
loads the configuration data in C<'/path/to/config'> |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item * |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
creates the subs C, C |
|
174
|
|
|
|
|
|
|
and C. |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Then when you want your application to have access to your configuration data, |
|
179
|
|
|
|
|
|
|
you add this (eg in your class C): |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
package My::Module; |
|
182
|
|
|
|
|
|
|
use My::Config; # Note, no () |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This exports the sub C into your current package, which allows you to |
|
185
|
|
|
|
|
|
|
access your configuation data as follows: |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
@hosts = C('db.hosts.sesssion'); |
|
188
|
|
|
|
|
|
|
$hosts_ref = C('db.hosts.sesssion'); |
|
189
|
|
|
|
|
|
|
@cloned_hosts = My::Config::clone('db.hosts.session'); |
|
190
|
|
|
|
|
|
|
$config = My::Config::object; |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=back |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 CONFIG TREE LAYOUT |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Config::Merge reads the data from any number (and type) of config files |
|
197
|
|
|
|
|
|
|
stored in a directory tree. File names and directory names are used as keys in |
|
198
|
|
|
|
|
|
|
the configuration hash. |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
It uses file extensions to decide what type of data the file contains, so: |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
YAML : .yaml .yml |
|
203
|
|
|
|
|
|
|
JSON : .json .jsn |
|
204
|
|
|
|
|
|
|
XML : .xml |
|
205
|
|
|
|
|
|
|
INI : .ini |
|
206
|
|
|
|
|
|
|
Perl : .perl .pl |
|
207
|
|
|
|
|
|
|
Config::General : .conf .cnf |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
When loading your config data, Config::Merge starts at the directory |
|
210
|
|
|
|
|
|
|
specified at startup (see L"USING Config::Merge">) and looks |
|
211
|
|
|
|
|
|
|
through all the sub-directories for files ending in one of the above |
|
212
|
|
|
|
|
|
|
extensions. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
The name of the file or subdirectory is used as the first key. So: |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
global/ |
|
217
|
|
|
|
|
|
|
db.yaml: |
|
218
|
|
|
|
|
|
|
username : admin |
|
219
|
|
|
|
|
|
|
hosts: |
|
220
|
|
|
|
|
|
|
- host1 |
|
221
|
|
|
|
|
|
|
- host2 |
|
222
|
|
|
|
|
|
|
password: |
|
223
|
|
|
|
|
|
|
host1: password1 |
|
224
|
|
|
|
|
|
|
host2: password2 |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
would be loaded as : |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
$Config = { |
|
229
|
|
|
|
|
|
|
global => { |
|
230
|
|
|
|
|
|
|
db => { |
|
231
|
|
|
|
|
|
|
username => 'admin', |
|
232
|
|
|
|
|
|
|
password => { host1 => 'password1', host2 => 'password2'}, |
|
233
|
|
|
|
|
|
|
hosts => ['host1','host2'], |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
} |
|
236
|
|
|
|
|
|
|
} |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Subdirectories are processed before the current directory, so |
|
239
|
|
|
|
|
|
|
you can have a directory and a config file with the same name, |
|
240
|
|
|
|
|
|
|
and the values will be merged into a single hash, so for |
|
241
|
|
|
|
|
|
|
instance, you can have: |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
confdir: |
|
244
|
|
|
|
|
|
|
syndication/ |
|
245
|
|
|
|
|
|
|
--data_types/ |
|
246
|
|
|
|
|
|
|
--traffic.yaml |
|
247
|
|
|
|
|
|
|
--headlines.yaml |
|
248
|
|
|
|
|
|
|
--data_types.ini |
|
249
|
|
|
|
|
|
|
syndication.conf |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
The config items in syndication.conf will be added to (or overwrite) |
|
252
|
|
|
|
|
|
|
the items loaded into the syndication namespace via the subdirectory |
|
253
|
|
|
|
|
|
|
called syndication. |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=head1 OVERRIDING CONFIG LOCALLY |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
The situation often arises where it is necessary to specify |
|
258
|
|
|
|
|
|
|
different config values on different machines. For instance, |
|
259
|
|
|
|
|
|
|
the database host on a dev machine may be different from the host |
|
260
|
|
|
|
|
|
|
on the live application. Also, see L"ADVANCED USAGE"> which |
|
261
|
|
|
|
|
|
|
provides you with other means to merge local data. |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Instead of changing this data during dev and then having to remember |
|
264
|
|
|
|
|
|
|
to change it back before putting the new code live, we have a mechanism |
|
265
|
|
|
|
|
|
|
for overriding config locally in a C file and then, as long as |
|
266
|
|
|
|
|
|
|
that file never gets uploaded to live, you are protected. |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
You can put a file called C (where * is any of the recognised |
|
269
|
|
|
|
|
|
|
extensions) in any sub-directory, and |
|
270
|
|
|
|
|
|
|
the data in this file will be merged with the existing data. |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
Just make sure that the C files are never checked into your live |
|
273
|
|
|
|
|
|
|
code. |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
For instance, if we have: |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
confdir: |
|
278
|
|
|
|
|
|
|
db.yaml |
|
279
|
|
|
|
|
|
|
local.yaml |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
and db.yaml has : |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
connections: |
|
284
|
|
|
|
|
|
|
default_settings: |
|
285
|
|
|
|
|
|
|
host: localhost |
|
286
|
|
|
|
|
|
|
table: abc |
|
287
|
|
|
|
|
|
|
password: 123 |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
And in local.yaml: |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
db: |
|
292
|
|
|
|
|
|
|
connections: |
|
293
|
|
|
|
|
|
|
default_settings: |
|
294
|
|
|
|
|
|
|
password: 456 |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
the resulting configuration will look like this: |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
db: |
|
299
|
|
|
|
|
|
|
connections: |
|
300
|
|
|
|
|
|
|
default_settings: |
|
301
|
|
|
|
|
|
|
host: localhost |
|
302
|
|
|
|
|
|
|
table: abc |
|
303
|
|
|
|
|
|
|
password: 456 |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 ACCESSING CONFIG DATA |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
All configuration data is loaded into a single hash, eg: |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
$config = { |
|
310
|
|
|
|
|
|
|
db => { |
|
311
|
|
|
|
|
|
|
hosts => { |
|
312
|
|
|
|
|
|
|
session => ['host1','host2','host3'], |
|
313
|
|
|
|
|
|
|
images => ['host1','host2','host3'], |
|
314
|
|
|
|
|
|
|
etc... |
|
315
|
|
|
|
|
|
|
} |
|
316
|
|
|
|
|
|
|
} |
|
317
|
|
|
|
|
|
|
} |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
If you want to access it via standard Perl dereferences, you can just ask |
|
321
|
|
|
|
|
|
|
for the hash: |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
OO: |
|
324
|
|
|
|
|
|
|
$data_ref = $config->(); |
|
325
|
|
|
|
|
|
|
$hosts_ref = $data_ref->{db}{hosts}{session}; |
|
326
|
|
|
|
|
|
|
$host_1 = $data_ref->{db}{hosts}{session}[0]; |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
Functional: |
|
329
|
|
|
|
|
|
|
$data_ref = C(); |
|
330
|
|
|
|
|
|
|
$hosts_ref = $data_ref->{db}{hosts}{session}; |
|
331
|
|
|
|
|
|
|
$host_1 = $data_ref->{db}{hosts}{session}[0]; |
|
332
|
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
However, C also provides an easy to read dot-notation in the |
|
334
|
|
|
|
|
|
|
style of Template Toolkit: C<('key1.key2.keyn')>. |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
A key can be the key of a hash or the index of an array. The return value is |
|
337
|
|
|
|
|
|
|
context sensitive, so if called in list context, a hash ref or array ref will |
|
338
|
|
|
|
|
|
|
be dereferenced. |
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
OO: |
|
341
|
|
|
|
|
|
|
@hosts = $config->('db.hosts.session'); |
|
342
|
|
|
|
|
|
|
$hosts_ref = $config->('db.hosts.session'); |
|
343
|
|
|
|
|
|
|
$host_1 = $config->('db.hosts.session.0'); |
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
Functional: |
|
346
|
|
|
|
|
|
|
@hosts = C('db.hosts.session'); |
|
347
|
|
|
|
|
|
|
$hosts_ref = C('db.hosts.session'); |
|
348
|
|
|
|
|
|
|
$host_1 = C('db.hosts.session.0'); |
|
349
|
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
These lookups are memo'ised, so lookups are fast. |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
If the specified key is not found, then an error is thrown. |
|
353
|
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=head1 MINIMISING MEMORY USE |
|
355
|
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
The more configuration data you load, the more memory you use. In order to |
|
357
|
|
|
|
|
|
|
keep the memory use as low as possible for mod_perl (or other forking |
|
358
|
|
|
|
|
|
|
applications), the configuration data should be loaded at startup in the |
|
359
|
|
|
|
|
|
|
parent process. |
|
360
|
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
As long as the data is never changed by the children, the configuration hash |
|
362
|
|
|
|
|
|
|
will be stored in shared memory, rather than there being a separate copy in each |
|
363
|
|
|
|
|
|
|
child process. |
|
364
|
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
(See L) |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
=head1 METHODS |
|
368
|
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
=over |
|
370
|
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
=item C |
|
372
|
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
$conf = Config::Merge->new($config_dir); |
|
374
|
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
new() instantiates a config object, loads the config from |
|
376
|
|
|
|
|
|
|
the directory specified, and returns the object. |
|
377
|
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=cut |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
#=================================== |
|
381
|
|
|
|
|
|
|
sub new { |
|
382
|
|
|
|
|
|
|
#=================================== |
|
383
|
34
|
|
|
34
|
1
|
15124
|
my $proto = shift; |
|
384
|
34
|
|
33
|
|
|
201
|
my $class = ref $proto || $proto; |
|
385
|
|
|
|
|
|
|
|
|
386
|
34
|
|
|
|
|
59
|
my $self = {}; |
|
387
|
34
|
|
|
|
|
98
|
bless( $self, $class ); |
|
388
|
|
|
|
|
|
|
|
|
389
|
34
|
50
|
|
|
|
183
|
my $params |
|
|
|
100
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
= @_ > 1 ? {@_} |
|
391
|
|
|
|
|
|
|
: ref $_[0] eq 'HASH' ? shift() |
|
392
|
|
|
|
|
|
|
: { path => shift() }; |
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
# Emit debug messages |
|
395
|
34
|
100
|
|
|
|
398
|
$self->{debug} = $params->{debug} ? 1 : 0; |
|
396
|
|
|
|
|
|
|
|
|
397
|
34
|
50
|
66
|
|
|
149
|
die "Parameter 'sort' must be a coderef" |
|
398
|
|
|
|
|
|
|
if exists $params->{sort} && ref $params->{sort} ne 'CODE'; |
|
399
|
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
# Setup callbacks |
|
401
|
|
|
|
|
|
|
$self->_init_callback( $_, $params->{$_} ) |
|
402
|
34
|
|
|
|
|
224
|
foreach qw(skip is_local load_as sort); |
|
403
|
|
|
|
|
|
|
|
|
404
|
31
|
100
|
|
|
|
124
|
my $path = $params->{path} |
|
405
|
|
|
|
|
|
|
or die( "Configuration directory not specified when creating a new " |
|
406
|
|
|
|
|
|
|
. "'$class' object" ); |
|
407
|
|
|
|
|
|
|
|
|
408
|
30
|
100
|
66
|
|
|
1406
|
if ( $path && -d $path && -r _ ) { |
|
|
|
|
66
|
|
|
|
|
|
409
|
|
|
|
|
|
|
|
|
410
|
29
|
|
|
|
|
177
|
$path =~ s|/?$|/|; |
|
411
|
29
|
|
|
|
|
110
|
$self->{config_dir} = $path; |
|
412
|
29
|
|
|
|
|
96
|
$self->load_config(); |
|
413
|
|
|
|
|
|
|
|
|
414
|
24
|
|
|
|
|
327
|
return $self; |
|
415
|
|
|
|
|
|
|
} |
|
416
|
|
|
|
|
|
|
else { |
|
417
|
1
|
|
|
|
|
13
|
die( "Configuration directory '$path' not readable when creating a new " |
|
418
|
|
|
|
|
|
|
. "'$class' object" ); |
|
419
|
|
|
|
|
|
|
} |
|
420
|
0
|
|
|
|
|
0
|
return $self; |
|
421
|
|
|
|
|
|
|
} |
|
422
|
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
=item C |
|
424
|
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
$val = $config->C('key1.key2.keyn'); |
|
426
|
|
|
|
|
|
|
$val = $config->C('key1.key2.keyn',$hash_ref); |
|
427
|
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
C objects are overloaded so that this also works: |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
$val = $config->('key1.key2.keyn'); |
|
431
|
|
|
|
|
|
|
$val = $config->('key1.key2.keyn',$hash_ref); |
|
432
|
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
Or, if used in the functional style (see L"USING Config::Merge">): |
|
434
|
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
$val = C('key1.key2.keyn'); |
|
436
|
|
|
|
|
|
|
$val = C('key1.key2.keyn',$hash_ref); |
|
437
|
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
C etc can be keys in a hash, or indexes of an array. |
|
439
|
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
C returns everything from C down, |
|
441
|
|
|
|
|
|
|
so you can use the return value just as you would any normal Perl variable. |
|
442
|
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
The return values are context-sensitive, so if called |
|
444
|
|
|
|
|
|
|
in list context, an array ref or hash ref will be returned as lists. |
|
445
|
|
|
|
|
|
|
Scalar values, code refs, regexes and blessed objects will always be returned |
|
446
|
|
|
|
|
|
|
as themselves. |
|
447
|
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
So for example: |
|
449
|
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
$password = C('database.main.password'); |
|
451
|
|
|
|
|
|
|
$regex = C('database.main.password_regex'); |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
@countries = C('lists.countries'); |
|
454
|
|
|
|
|
|
|
$countries_array_ref = C('lists.countries'); |
|
455
|
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
etc |
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
If called with a hash ref as the second parameter, then that hash ref will be |
|
459
|
|
|
|
|
|
|
examined, rather than the C<$config> data. |
|
460
|
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
=cut |
|
462
|
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
#=================================== |
|
464
|
|
|
|
|
|
|
sub C { |
|
465
|
|
|
|
|
|
|
#=================================== |
|
466
|
96
|
|
|
96
|
1
|
714
|
my $self = shift; |
|
467
|
96
|
|
|
|
|
144
|
my $path = shift; |
|
468
|
96
|
100
|
|
|
|
262
|
$path = '' unless defined $path; |
|
469
|
|
|
|
|
|
|
|
|
470
|
96
|
|
|
|
|
121
|
my ( $config, @keys ); |
|
471
|
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
# If a private hash is passed in use that |
|
473
|
96
|
50
|
|
|
|
194
|
if (@_) { |
|
474
|
0
|
|
|
|
|
0
|
$config = $_[0]; |
|
475
|
0
|
|
|
|
|
0
|
@keys = split( /\./, $path ); |
|
476
|
0
|
|
|
|
|
0
|
$config = $self->_walk_path( $config, 'PRIVATE', \@keys ); |
|
477
|
|
|
|
|
|
|
} |
|
478
|
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
# Otherwise use the stored config data |
|
480
|
|
|
|
|
|
|
else { |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
# Have we previously memoised this? |
|
483
|
96
|
100
|
|
|
|
492
|
if ( exists $self->{_memo}->{$path} ) { |
|
484
|
16
|
|
|
|
|
25
|
$config = ${ $self->{_memo}->{$path} }; |
|
|
16
|
|
|
|
|
48
|
|
|
485
|
|
|
|
|
|
|
} |
|
486
|
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
# Not memoised, so get it manually |
|
488
|
|
|
|
|
|
|
else { |
|
489
|
80
|
|
|
|
|
145
|
$config = $self->{config}; |
|
490
|
80
|
|
|
|
|
634
|
(@keys) = split( /\./, $path ); |
|
491
|
80
|
|
|
|
|
278
|
$config = $self->_walk_path( $config, '', \@keys ); |
|
492
|
77
|
|
|
|
|
285
|
$self->{_memo}->{$path} = \$config; |
|
493
|
|
|
|
|
|
|
} |
|
494
|
|
|
|
|
|
|
} |
|
495
|
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
return |
|
497
|
0
|
|
|
|
|
0
|
wantarray && ref($config) eq 'HASH' ? %{$config} |
|
|
2
|
|
|
|
|
15
|
|
|
498
|
93
|
100
|
66
|
|
|
1132
|
: wantarray && ref($config) eq 'ARRAY' ? @{$config} |
|
|
|
50
|
100
|
|
|
|
|
|
499
|
|
|
|
|
|
|
: $config; |
|
500
|
|
|
|
|
|
|
} |
|
501
|
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
#=================================== |
|
503
|
|
|
|
|
|
|
sub _walk_path { |
|
504
|
|
|
|
|
|
|
#=================================== |
|
505
|
80
|
|
|
80
|
|
267
|
my $self = shift; |
|
506
|
80
|
|
|
|
|
229
|
my ( $config, $key_path, $keys ) = @_; |
|
507
|
|
|
|
|
|
|
|
|
508
|
80
|
|
|
|
|
149
|
foreach my $key (@$keys) { |
|
509
|
244
|
50
|
33
|
|
|
1212
|
next unless defined $key && length($key); |
|
510
|
244
|
100
|
66
|
|
|
1743
|
if ( ref $config eq 'ARRAY' |
|
|
|
100
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
511
|
|
|
|
|
|
|
&& $key =~ /^[0-9]+/ |
|
512
|
|
|
|
|
|
|
&& exists $config->[$key] ) |
|
513
|
|
|
|
|
|
|
{ |
|
514
|
11
|
|
|
|
|
26
|
$config = $config->[$key]; |
|
515
|
11
|
|
|
|
|
26
|
$key_path .= '.' . $key; |
|
516
|
11
|
|
|
|
|
31
|
next; |
|
517
|
|
|
|
|
|
|
} |
|
518
|
|
|
|
|
|
|
elsif ( ref $config eq 'HASH' && exists $config->{$key} ) { |
|
519
|
230
|
|
|
|
|
517
|
$config = $config->{$key}; |
|
520
|
230
|
|
|
|
|
536
|
$key_path = $self->_join_key_path( $key_path, $key ); |
|
521
|
230
|
|
|
|
|
472
|
next; |
|
522
|
|
|
|
|
|
|
} |
|
523
|
3
|
|
|
|
|
30
|
die("Invalid key '$key' specified for '$key_path'\n"); |
|
524
|
|
|
|
|
|
|
} |
|
525
|
77
|
|
|
|
|
170
|
return $config; |
|
526
|
|
|
|
|
|
|
} |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
=item C |
|
529
|
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
This works exactly the same way as L"C()"> but it performs a |
|
531
|
|
|
|
|
|
|
deep clone of the data before returning it. |
|
532
|
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
This means that the returned data can be changed without |
|
534
|
|
|
|
|
|
|
affecting the data stored in the $conf object; |
|
535
|
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
The data is deep cloned, using Storable, so the bigger the data, the more |
|
537
|
|
|
|
|
|
|
performance hit. That said, Storable's dclone is very fast. |
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=cut |
|
540
|
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
#=================================== |
|
542
|
|
|
|
|
|
|
sub clone { |
|
543
|
|
|
|
|
|
|
#=================================== |
|
544
|
3
|
|
|
3
|
1
|
6
|
my $self = shift; |
|
545
|
3
|
|
|
|
|
25
|
my $data = $self->Config::Merge::C(@_); |
|
546
|
3
|
|
|
|
|
612
|
return Storable::dclone($data); |
|
547
|
|
|
|
|
|
|
} |
|
548
|
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
my @Builtin_Merges = qw( |
|
550
|
|
|
|
|
|
|
Config::Any::YAML |
|
551
|
|
|
|
|
|
|
Config::Any::General |
|
552
|
|
|
|
|
|
|
Config::Any::XML |
|
553
|
|
|
|
|
|
|
Config::Any::INI |
|
554
|
|
|
|
|
|
|
Config::Any::JSON |
|
555
|
|
|
|
|
|
|
Config::Merge::Perl |
|
556
|
|
|
|
|
|
|
); |
|
557
|
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
my %Module_For_Ext = (); |
|
559
|
|
|
|
|
|
|
__PACKAGE__->register_loader($_) foreach @Builtin_Merges; |
|
560
|
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
=item C |
|
562
|
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
Config::Merge->register_loader( 'Config::Merge::XYZ'); |
|
564
|
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
Config::Merge->register_loader( 'Config::Merge::XYZ' => 'xyz','xxx'); |
|
566
|
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
By default, C uses the C |
|
568
|
|
|
|
|
|
|
plugins to support YAML, JSON, INI, XML, Perl and Config::General configuration |
|
569
|
|
|
|
|
|
|
files, using the standard file extensions to recognise the file type. (See |
|
570
|
|
|
|
|
|
|
L"CONFIG TREE LAYOUT">). |
|
571
|
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
If you would like to change the handler for an extension (eg, you want C<.conf> |
|
573
|
|
|
|
|
|
|
and C<.cnf> files to be treated as YAML), do the following: |
|
574
|
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
Config::Merge->register_loader ('Config::Any::YAML' => 'conf', 'cnf'); |
|
576
|
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
If you would like to add a new config style, then your module should have two |
|
578
|
|
|
|
|
|
|
methods: C (which returns a list of the extensions it handles), |
|
579
|
|
|
|
|
|
|
and C which accepts the name of the file to load, and returns |
|
580
|
|
|
|
|
|
|
a hash ref containing the data in the file. See L for details. |
|
581
|
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
Alternatively, you can specify the extensions when you load it: |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
Config::Merge->register_loader ('My::Merge' => 'conf', 'cnf'); |
|
585
|
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
=cut |
|
587
|
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
#=================================== |
|
589
|
|
|
|
|
|
|
sub register_loader { |
|
590
|
|
|
|
|
|
|
#=================================== |
|
591
|
32
|
|
|
32
|
1
|
1138
|
my $class = shift; |
|
592
|
32
|
100
|
|
|
|
101
|
my $loader = shift |
|
593
|
|
|
|
|
|
|
or die "No loader class passed to register_loader()"; |
|
594
|
31
|
100
|
|
|
|
1894
|
eval "require $loader" |
|
595
|
|
|
|
|
|
|
or die $@; |
|
596
|
30
|
50
|
|
|
|
28317
|
my @extensions = @_ ? @_ : $loader->extensions; |
|
597
|
30
|
|
|
|
|
144
|
foreach my $ext (@extensions) { |
|
598
|
50
|
|
|
|
|
317
|
$Module_For_Ext{ lc($ext) } = $loader; |
|
599
|
|
|
|
|
|
|
} |
|
600
|
30
|
|
|
|
|
180
|
return; |
|
601
|
|
|
|
|
|
|
} |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
=item C |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
$config->load_config(); |
|
606
|
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
Will reload the config files located in the directory specified at object |
|
608
|
|
|
|
|
|
|
creation (see L"new()">). |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
BEWARE : If you are using this in a mod_perl environment, you will lose the |
|
611
|
|
|
|
|
|
|
benefit of shared memory by calling this in a child process |
|
612
|
|
|
|
|
|
|
- each child will have its own copy of the data. |
|
613
|
|
|
|
|
|
|
See L. |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
Returns the config hash ref. |
|
616
|
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
=cut |
|
618
|
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
#=================================== |
|
620
|
|
|
|
|
|
|
sub load_config { |
|
621
|
|
|
|
|
|
|
#=================================== |
|
622
|
31
|
|
|
31
|
1
|
58
|
my $self = shift; |
|
623
|
31
|
|
|
|
|
88
|
$self->{_memo} = {}; |
|
624
|
31
|
|
|
|
|
85
|
$self->debug("Loading config data"); |
|
625
|
31
|
|
100
|
|
|
97
|
return $self->{config} = $self->_load_config() || {}; |
|
626
|
|
|
|
|
|
|
} |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
#=================================== |
|
629
|
|
|
|
|
|
|
sub _load_config { |
|
630
|
|
|
|
|
|
|
#=================================== |
|
631
|
67
|
|
|
67
|
|
91
|
my $self = shift; |
|
632
|
67
|
|
66
|
|
|
216
|
my $dir = shift || $self->{config_dir}; |
|
633
|
67
|
|
100
|
|
|
191
|
my $key_path = shift || ''; |
|
634
|
67
|
|
|
|
|
104
|
my $loading_local = shift; |
|
635
|
|
|
|
|
|
|
|
|
636
|
67
|
|
|
|
|
107
|
my $config = {}; |
|
637
|
|
|
|
|
|
|
|
|
638
|
67
|
|
|
|
|
83
|
my @local; |
|
639
|
|
|
|
|
|
|
|
|
640
|
67
|
|
|
|
|
9531
|
my $config_files = $self->{sort} |
|
641
|
|
|
|
|
|
|
->( $self, [ bsd_glob( File::Spec->catfile( $dir, '*' ) ) ] ); |
|
642
|
|
|
|
|
|
|
|
|
643
|
67
|
|
|
|
|
307
|
my $is_local = $self->{is_local}; |
|
644
|
67
|
|
|
|
|
347
|
$self->debug( '', "Entering dir: $dir", '-' x ( length($dir) + 14 ) ); |
|
645
|
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
CONFIG_FILE: |
|
647
|
67
|
|
|
|
|
139
|
foreach my $config_file (@$config_files) { |
|
648
|
173
|
|
|
|
|
206
|
my ( $data, $name, $curr_key_path, $loader ); |
|
649
|
|
|
|
|
|
|
|
|
650
|
173
|
|
|
|
|
3393
|
my $filename = ( File::Spec->splitpath($config_file) )[2]; |
|
651
|
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
# If it is a file |
|
653
|
173
|
100
|
|
|
|
5994
|
if ( -f $config_file ) { |
|
|
|
50
|
|
|
|
|
|
|
654
|
129
|
|
|
|
|
450
|
$self->debug(" Found file : $config_file"); |
|
655
|
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
# Must have an extension |
|
657
|
129
|
50
|
0
|
|
|
933
|
( $name, my $ext ) = ( $filename =~ /(.+)[.]([^.]+)/ ) |
|
658
|
|
|
|
|
|
|
or $self->debug(" ... No extension") && next CONFIG_FILE; |
|
659
|
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
# Must have an associated module |
|
661
|
129
|
50
|
0
|
|
|
499
|
$loader = $Module_For_Ext{ lc $ext } |
|
662
|
|
|
|
|
|
|
or $self->debug(" ... No loader") && next CONFIG_FILE; |
|
663
|
|
|
|
|
|
|
} |
|
664
|
|
|
|
|
|
|
elsif ( -d $config_file ) { |
|
665
|
44
|
|
|
|
|
156
|
$self->debug(" Found dir : $config_file"); |
|
666
|
44
|
|
|
|
|
295
|
$name = $filename; |
|
667
|
44
|
|
|
|
|
63
|
undef $loader; |
|
668
|
|
|
|
|
|
|
} |
|
669
|
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
# Anything else (eg symlink), skip |
|
671
|
|
|
|
|
|
|
else { |
|
672
|
0
|
|
|
|
|
0
|
next; |
|
673
|
|
|
|
|
|
|
} |
|
674
|
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
# If it is a local file/dir, process last |
|
676
|
173
|
100
|
66
|
|
|
583
|
if ( !$loading_local && $is_local->( $self, $name ) ) { |
|
677
|
22
|
|
|
|
|
61
|
$self->debug(" ... will merge later"); |
|
678
|
22
|
|
|
|
|
74
|
push @local, [ $loader, $config_file, $filename ]; |
|
679
|
22
|
|
|
|
|
77
|
next CONFIG_FILE; |
|
680
|
|
|
|
|
|
|
} |
|
681
|
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
# Find the key name from the filename |
|
683
|
151
|
|
|
|
|
515
|
$name = $self->_load_as( $key_path, $name, $loading_local ); |
|
684
|
150
|
100
|
|
|
|
336
|
next CONFIG_FILE if not defined $name; |
|
685
|
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
# loader = module name to load file, or undef for directory |
|
687
|
134
|
100
|
|
|
|
522
|
$data |
|
688
|
|
|
|
|
|
|
= $loader |
|
689
|
|
|
|
|
|
|
? $self->_load_config_file( $loader, $config_file ) |
|
690
|
|
|
|
|
|
|
: $self->_load_config( $config_file, |
|
691
|
|
|
|
|
|
|
$self->_join_key_path( $key_path, $name ), |
|
692
|
|
|
|
|
|
|
$loading_local ); |
|
693
|
|
|
|
|
|
|
|
|
694
|
133
|
50
|
|
|
|
292
|
next CONFIG_FILE unless defined $data; |
|
695
|
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
# Merge keys if already exists |
|
697
|
133
|
100
|
66
|
|
|
788
|
if ( exists $config->{$name} |
|
|
|
|
66
|
|
|
|
|
|
698
|
|
|
|
|
|
|
&& ref $config->{$name} eq 'HASH' |
|
699
|
|
|
|
|
|
|
&& ref $data eq 'HASH' ) |
|
700
|
|
|
|
|
|
|
{ |
|
701
|
10
|
|
|
|
|
132
|
$config->{$name}->{$_} = $data->{$_} foreach keys %$data; |
|
702
|
|
|
|
|
|
|
} |
|
703
|
|
|
|
|
|
|
else { |
|
704
|
123
|
|
|
|
|
424
|
$config->{$name} = $data; |
|
705
|
|
|
|
|
|
|
} |
|
706
|
|
|
|
|
|
|
} |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
# Merge local config into main config |
|
709
|
|
|
|
|
|
|
LOCAL_FILE: |
|
710
|
65
|
|
|
|
|
142
|
foreach my $local_file (@local) { |
|
711
|
22
|
|
|
|
|
51
|
my ( $loader, $config_file, $name ) = @$local_file; |
|
712
|
22
|
|
|
|
|
81
|
$self->debug(" Merging file $config_file"); |
|
713
|
22
|
|
|
|
|
215
|
$name = $self->_load_as( $key_path, $name, 1 ); |
|
714
|
|
|
|
|
|
|
next LOCAL_FILE |
|
715
|
22
|
100
|
|
|
|
76
|
unless defined $name; |
|
716
|
|
|
|
|
|
|
|
|
717
|
21
|
50
|
|
|
|
73
|
my $data |
|
718
|
|
|
|
|
|
|
= $loader |
|
719
|
|
|
|
|
|
|
? $self->_load_config_file( $loader, $config_file ) |
|
720
|
|
|
|
|
|
|
: $self->_load_config( $config_file, $key_path, 1 ); |
|
721
|
|
|
|
|
|
|
|
|
722
|
21
|
50
|
|
|
|
66
|
next LOCAL_FILE unless defined $data; |
|
723
|
|
|
|
|
|
|
|
|
724
|
21
|
100
|
|
|
|
115
|
$config = $self->_merge_hash( |
|
725
|
|
|
|
|
|
|
$config, $name |
|
726
|
|
|
|
|
|
|
? { $name => $data } |
|
727
|
|
|
|
|
|
|
: $data |
|
728
|
|
|
|
|
|
|
); |
|
729
|
|
|
|
|
|
|
} |
|
730
|
|
|
|
|
|
|
|
|
731
|
62
|
100
|
|
|
|
461
|
return keys %$config ? $config : undef; |
|
732
|
|
|
|
|
|
|
} |
|
733
|
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
#=================================== |
|
735
|
|
|
|
|
|
|
sub _load_as { |
|
736
|
|
|
|
|
|
|
#=================================== |
|
737
|
173
|
|
|
173
|
|
307
|
my ( $self, $key_path, $name, $loading_local ) = @_; |
|
738
|
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
# Find the key name from the filename |
|
740
|
173
|
|
|
|
|
456
|
$name = $self->{load_as}->( $self, $name, $loading_local ); |
|
741
|
173
|
100
|
|
|
|
509
|
unless ( defined $name ) { |
|
742
|
5
|
|
|
|
|
13
|
$self->debug(" ... Skipped by load_as()"); |
|
743
|
5
|
|
|
|
|
10
|
return; |
|
744
|
|
|
|
|
|
|
} |
|
745
|
|
|
|
|
|
|
|
|
746
|
168
|
100
|
100
|
|
|
702
|
die "load_as() cannot return '' when loading main config" |
|
747
|
|
|
|
|
|
|
if !$loading_local && $name eq ''; |
|
748
|
|
|
|
|
|
|
|
|
749
|
167
|
|
|
|
|
376
|
my $curr_key_path = $self->_join_key_path( $key_path, $name ); |
|
750
|
167
|
100
|
|
|
|
602
|
$self->debug( " ... loading at : " |
|
751
|
|
|
|
|
|
|
. ( length($curr_key_path) ? $curr_key_path : '.' ) ); |
|
752
|
|
|
|
|
|
|
|
|
753
|
167
|
100
|
|
|
|
702
|
if ( $self->{skip}->( $self, $curr_key_path ) ) { |
|
754
|
12
|
|
|
|
|
30
|
$self->debug(" ... skipped by skip()"); |
|
755
|
12
|
|
|
|
|
22
|
return; |
|
756
|
|
|
|
|
|
|
} |
|
757
|
155
|
|
|
|
|
406
|
return $name; |
|
758
|
|
|
|
|
|
|
} |
|
759
|
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
#=================================== |
|
761
|
|
|
|
|
|
|
sub _join_key_path { |
|
762
|
|
|
|
|
|
|
#=================================== |
|
763
|
433
|
|
|
433
|
|
1001
|
my ( $self, $key_path, $name ) = @_; |
|
764
|
433
|
100
|
|
|
|
1885
|
return $key_path . '.' . $name if length($key_path); |
|
765
|
192
|
|
|
|
|
469
|
return $name; |
|
766
|
|
|
|
|
|
|
} |
|
767
|
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
#=================================== |
|
769
|
|
|
|
|
|
|
sub _load_config_file { |
|
770
|
|
|
|
|
|
|
#=================================== |
|
771
|
119
|
|
|
119
|
|
168
|
my $self = shift; |
|
772
|
119
|
|
|
|
|
195
|
my ( $loader, $config_file ) = @_; |
|
773
|
119
|
|
|
|
|
347
|
$self->debug(" ... with : $loader"); |
|
774
|
119
|
|
|
|
|
139
|
my $data; |
|
775
|
119
|
|
|
|
|
146
|
eval { |
|
776
|
119
|
|
|
|
|
1542
|
my @data = $loader->load($config_file); |
|
777
|
118
|
50
|
|
|
|
8401
|
$data |
|
778
|
|
|
|
|
|
|
= @data > 1 |
|
779
|
|
|
|
|
|
|
? \@data |
|
780
|
|
|
|
|
|
|
: $data[0]; |
|
781
|
|
|
|
|
|
|
}; |
|
782
|
119
|
100
|
|
|
|
317
|
if ($@) { |
|
783
|
1
|
|
|
|
|
12
|
die( "Error loading config file $config_file:\n\n" . $@ ); |
|
784
|
|
|
|
|
|
|
} |
|
785
|
|
|
|
|
|
|
|
|
786
|
118
|
|
|
|
|
305
|
return $data; |
|
787
|
|
|
|
|
|
|
} |
|
788
|
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
=item C |
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
$config->clear_cache(); |
|
792
|
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
Config data is generally not supposed to be changed at runtime. However, if |
|
794
|
|
|
|
|
|
|
you do make changes, you may get inconsistent results, because lookups are |
|
795
|
|
|
|
|
|
|
cached. |
|
796
|
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
For instance: |
|
798
|
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
print $config->C('db.hosts.session'); # Caches this lookup |
|
800
|
|
|
|
|
|
|
> "host1 host2 host3" |
|
801
|
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
$data = $config->C('db.hosts'); |
|
803
|
|
|
|
|
|
|
$data->{session} = 123; |
|
804
|
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
print $config->C('db.hosts.session'); # uses cached value |
|
806
|
|
|
|
|
|
|
> "host1 host2 host3" |
|
807
|
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
$config->clear_cache(); |
|
809
|
|
|
|
|
|
|
print $config->C('db.hosts.session'); # uses actual value |
|
810
|
|
|
|
|
|
|
> "123" |
|
811
|
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
=cut |
|
813
|
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
#=================================== |
|
815
|
|
|
|
|
|
|
sub clear_cache { |
|
816
|
|
|
|
|
|
|
#=================================== |
|
817
|
3
|
|
|
3
|
1
|
8
|
my $self = shift; |
|
818
|
3
|
|
|
|
|
11
|
$self->{_memo} = {}; |
|
819
|
3
|
|
|
|
|
17
|
return; |
|
820
|
|
|
|
|
|
|
} |
|
821
|
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
=item C |
|
823
|
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
C will normally be called automatically when you |
|
825
|
|
|
|
|
|
|
C |
|
826
|
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
use Config::Merge(); |
|
828
|
|
|
|
|
|
|
Config::Merge->register_loader('My::Plugin' => 'ext'); |
|
829
|
|
|
|
|
|
|
Config::Merge->import('My::Config' => '/path/to/config/dir'); |
|
830
|
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
If called with two params: C<$config_class> and C<$config_dir>, it |
|
832
|
|
|
|
|
|
|
generates the new class (which inherits from Config::Merge) |
|
833
|
|
|
|
|
|
|
specified in C<$config_class>, creates a new |
|
834
|
|
|
|
|
|
|
object of that class and creates 4 subs: |
|
835
|
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
=over |
|
837
|
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
=item C |
|
839
|
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
As a function: |
|
841
|
|
|
|
|
|
|
C('keys...') |
|
842
|
|
|
|
|
|
|
|
|
843
|
|
|
|
|
|
|
is the equivalent of: |
|
844
|
|
|
|
|
|
|
$config->C('keys...'); |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
=item C |
|
847
|
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
As a function: |
|
849
|
|
|
|
|
|
|
clone('keys...') |
|
850
|
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
is the equivalent of: |
|
852
|
|
|
|
|
|
|
$config->clone('keys...'); |
|
853
|
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
=item C |
|
855
|
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
$config = My::Config->object(); |
|
857
|
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
Returns the C<$config> object, |
|
859
|
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
=item C |
|
861
|
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
When you use your generated config class, it exports the C sub into your |
|
863
|
|
|
|
|
|
|
package: |
|
864
|
|
|
|
|
|
|
|
|
865
|
|
|
|
|
|
|
use My::Config; |
|
866
|
|
|
|
|
|
|
$hosts = C('db.hosts.session'); |
|
867
|
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=back |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
=back |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
=cut |
|
873
|
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
#=================================== |
|
875
|
|
|
|
|
|
|
sub import { |
|
876
|
|
|
|
|
|
|
#=================================== |
|
877
|
6
|
|
|
6
|
|
752
|
my $caller_class = shift; |
|
878
|
6
|
|
|
|
|
17
|
my ( $class, $dir ) = @_; |
|
879
|
|
|
|
|
|
|
return |
|
880
|
6
|
100
|
|
|
|
61
|
unless defined $class; |
|
881
|
|
|
|
|
|
|
|
|
882
|
3
|
100
|
|
|
|
14
|
unless ( defined $dir ) { |
|
883
|
1
|
|
|
|
|
2
|
$dir = $class; |
|
884
|
1
|
|
|
|
|
3
|
$class = $caller_class; |
|
885
|
|
|
|
|
|
|
} |
|
886
|
3
|
100
|
|
|
|
14
|
if ( $class eq __PACKAGE__ ) { |
|
887
|
1
|
|
|
|
|
8
|
die <
|
|
888
|
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
USAGE : use $class ('Your::Config' => '/path/to/config/dir' ); |
|
890
|
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
USAGE |
|
892
|
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
} |
|
894
|
|
|
|
|
|
|
|
|
895
|
2
|
|
|
|
|
5
|
my $inc_path = $class; |
|
896
|
2
|
|
|
|
|
5
|
$inc_path =~ s{::}{/}g; |
|
897
|
2
|
|
|
|
|
5
|
$inc_path .= '.pm'; |
|
898
|
|
|
|
|
|
|
|
|
899
|
5
|
|
|
5
|
|
190
|
no strict 'refs'; |
|
|
5
|
|
|
|
|
11
|
|
|
|
5
|
|
|
|
|
11410
|
|
|
900
|
2
|
50
|
|
|
|
9
|
unless ( exists $INC{$inc_path} ) { |
|
901
|
2
|
|
|
|
|
8
|
@{ $class . '::ISA' } = ($caller_class); |
|
|
2
|
|
|
|
|
73
|
|
|
902
|
2
|
|
|
|
|
11
|
$INC{$inc_path} = 'Auto-inflated by ' . $caller_class; |
|
903
|
|
|
|
|
|
|
} |
|
904
|
|
|
|
|
|
|
|
|
905
|
2
|
50
|
|
|
|
14
|
my $params = @_ % 2 ? shift() : {@_}; |
|
906
|
2
|
|
|
|
|
6
|
$params->{path} = $dir; |
|
907
|
2
|
|
|
|
|
20
|
my $config = $class->new(%$params); |
|
908
|
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
# Export C, clone to the subclass |
|
910
|
2
|
|
|
|
|
16
|
*{ $class . "::C" } |
|
911
|
2
|
100
|
|
25
|
|
14
|
= sub { my $c = ref $_[0] ? shift : $config; return C( $c, @_ ) }; |
|
|
25
|
|
|
|
|
660
|
|
|
|
25
|
|
|
|
|
66
|
|
|
912
|
2
|
|
|
|
|
13
|
*{ $class . "::clone" } = sub { |
|
913
|
2
|
100
|
|
2
|
|
10
|
my $c = ref $_[0] ? shift : $config; |
|
914
|
2
|
|
|
|
|
9
|
return clone( $c, @_ ); |
|
915
|
2
|
|
|
|
|
11
|
}; |
|
916
|
2
|
|
|
3
|
|
10
|
*{ $class . "::object" } = sub { return $config }; |
|
|
2
|
|
|
|
|
20
|
|
|
|
3
|
|
|
|
|
20
|
|
|
917
|
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
# Create a new import sub in the subclass |
|
919
|
2
|
|
|
2
|
|
186
|
*{ $class . "::import" } = eval ' |
|
|
2
|
|
|
|
|
14
|
|
|
|
2
|
|
|
|
|
15
|
|
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
120
|
|
|
920
|
|
|
|
|
|
|
sub { |
|
921
|
|
|
|
|
|
|
my $callpkg = caller(0); |
|
922
|
|
|
|
|
|
|
no strict \'refs\'; |
|
923
|
|
|
|
|
|
|
*{$callpkg."::C"} = \&' . $class . '::C; |
|
924
|
|
|
|
|
|
|
}'; |
|
925
|
|
|
|
|
|
|
|
|
926
|
2
|
|
|
|
|
31
|
return; |
|
927
|
|
|
|
|
|
|
} |
|
928
|
|
|
|
|
|
|
|
|
929
|
|
|
|
|
|
|
#=================================== |
|
930
|
|
|
|
|
|
|
sub _merge_hash { |
|
931
|
|
|
|
|
|
|
#=================================== |
|
932
|
60
|
|
|
60
|
|
2292
|
my $self = shift; |
|
933
|
60
|
|
|
|
|
74
|
my $config = shift; |
|
934
|
60
|
|
|
|
|
141
|
my $local = shift; |
|
935
|
|
|
|
|
|
|
KEY: |
|
936
|
58
|
|
|
|
|
167
|
foreach my $key ( keys %$local ) { |
|
937
|
77
|
100
|
100
|
|
|
417
|
if ( ref $local->{$key} eq 'HASH' |
|
938
|
|
|
|
|
|
|
&& exists $config->{$key} ) |
|
939
|
|
|
|
|
|
|
{ |
|
940
|
50
|
100
|
|
|
|
150
|
if ( ref $config->{$key} eq 'HASH' ) { |
|
941
|
37
|
|
|
|
|
119
|
$self->debug(" ... entering hash : $key"); |
|
942
|
37
|
|
|
|
|
175
|
$config->{$key} |
|
943
|
|
|
|
|
|
|
= $self->_merge_hash( $config->{$key}, $local->{$key} ); |
|
944
|
34
|
|
|
|
|
83
|
next KEY; |
|
945
|
|
|
|
|
|
|
} |
|
946
|
13
|
100
|
66
|
|
|
200
|
if ( ref $config->{$key} eq 'ARRAY' |
|
|
|
|
66
|
|
|
|
|
|
947
|
|
|
|
|
|
|
&& exists $local->{$key}{'!'} |
|
948
|
|
|
|
|
|
|
&& ref $local->{$key}{'!'} eq 'HASH' ) |
|
949
|
|
|
|
|
|
|
{ |
|
950
|
9
|
|
|
|
|
28
|
$self->_merge_array( $key, $config, $local ); |
|
951
|
6
|
|
|
|
|
14
|
next KEY; |
|
952
|
|
|
|
|
|
|
} |
|
953
|
|
|
|
|
|
|
} |
|
954
|
31
|
|
|
|
|
94
|
$self->debug(" ... setting key : $key"); |
|
955
|
31
|
|
|
|
|
96
|
$config->{$key} = $local->{$key}; |
|
956
|
|
|
|
|
|
|
} |
|
957
|
52
|
|
|
|
|
137
|
$self->debug(" ... leaving hash"); |
|
958
|
52
|
|
|
|
|
171
|
return $config; |
|
959
|
|
|
|
|
|
|
} |
|
960
|
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
=head1 ADVANCED USAGE |
|
962
|
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
The items in the section allow you to customise how Config::Merge |
|
964
|
|
|
|
|
|
|
loads your data. You may never need them. |
|
965
|
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
You can: |
|
967
|
|
|
|
|
|
|
|
|
968
|
|
|
|
|
|
|
=over |
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
=item * |
|
971
|
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
Override array values |
|
973
|
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
=item * |
|
975
|
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
Skip the loading of parts of your config tree |
|
977
|
|
|
|
|
|
|
|
|
978
|
|
|
|
|
|
|
=item * |
|
979
|
|
|
|
|
|
|
|
|
980
|
|
|
|
|
|
|
Specify which files / dirs are local |
|
981
|
|
|
|
|
|
|
|
|
982
|
|
|
|
|
|
|
=item * |
|
983
|
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
Specify how to translate a file / dir name into a key |
|
985
|
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
=item * |
|
987
|
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
Change order in which files are loaded |
|
989
|
|
|
|
|
|
|
|
|
990
|
|
|
|
|
|
|
=item * |
|
991
|
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
See debug output |
|
993
|
|
|
|
|
|
|
|
|
994
|
|
|
|
|
|
|
=back |
|
995
|
|
|
|
|
|
|
|
|
996
|
|
|
|
|
|
|
=over |
|
997
|
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
=item Overriding array values |
|
999
|
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
Overriding hash values is easy, however arrays are more complex. |
|
1001
|
|
|
|
|
|
|
it may be simpler to copy and paste and edit the array you want to |
|
1002
|
|
|
|
|
|
|
change locally. |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
|
|
|
|
|
|
However, if your array is too long, and you want to make small changes, |
|
1005
|
|
|
|
|
|
|
then you can use the following: |
|
1006
|
|
|
|
|
|
|
|
|
1007
|
|
|
|
|
|
|
In the main config: |
|
1008
|
|
|
|
|
|
|
|
|
1009
|
|
|
|
|
|
|
{ |
|
1010
|
|
|
|
|
|
|
cron => [qw( job1 job2 job3 job4)] |
|
1011
|
|
|
|
|
|
|
} |
|
1012
|
|
|
|
|
|
|
|
|
1013
|
|
|
|
|
|
|
In the local file |
|
1014
|
|
|
|
|
|
|
|
|
1015
|
|
|
|
|
|
|
{ |
|
1016
|
|
|
|
|
|
|
cron => { |
|
1017
|
|
|
|
|
|
|
'3' => 'newjob4', # changes 'job4' -> 'newjob4' |
|
1018
|
|
|
|
|
|
|
|
|
1019
|
|
|
|
|
|
|
'!' => { # signals an array override |
|
1020
|
|
|
|
|
|
|
|
|
1021
|
|
|
|
|
|
|
'-' => [1], # deletes 'job2' |
|
1022
|
|
|
|
|
|
|
|
|
1023
|
|
|
|
|
|
|
'+' => ['job5'], # appends 'job5' |
|
1024
|
|
|
|
|
|
|
|
|
1025
|
|
|
|
|
|
|
OR '+' => { # inserts 'job3a' after 'job3' |
|
1026
|
|
|
|
|
|
|
2 => 'job3a' |
|
1027
|
|
|
|
|
|
|
} |
|
1028
|
|
|
|
|
|
|
} |
|
1029
|
|
|
|
|
|
|
} |
|
1030
|
|
|
|
|
|
|
|
|
1031
|
|
|
|
|
|
|
=over |
|
1032
|
|
|
|
|
|
|
|
|
1033
|
|
|
|
|
|
|
=item * |
|
1034
|
|
|
|
|
|
|
|
|
1035
|
|
|
|
|
|
|
The override has to be a hash, with at least this structure |
|
1036
|
|
|
|
|
|
|
C<< { '!' => {} } >> to signal an array override |
|
1037
|
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
=item * |
|
1039
|
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
Any other keys with integers are treated as indexes and |
|
1041
|
|
|
|
|
|
|
are used to change the value at that index in the original array |
|
1042
|
|
|
|
|
|
|
|
|
1043
|
|
|
|
|
|
|
=item * |
|
1044
|
|
|
|
|
|
|
|
|
1045
|
|
|
|
|
|
|
The C<'-'> key should contain an array ref, with the indexes of the |
|
1046
|
|
|
|
|
|
|
elements to remove from the array. |
|
1047
|
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
=item * |
|
1049
|
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
If the C<'+'> key contains an array ref, then its contents are appended |
|
1051
|
|
|
|
|
|
|
to the original array. |
|
1052
|
|
|
|
|
|
|
|
|
1053
|
|
|
|
|
|
|
=item * |
|
1054
|
|
|
|
|
|
|
|
|
1055
|
|
|
|
|
|
|
If the C<'+'> key contains a hash ref, then each value is inserted |
|
1056
|
|
|
|
|
|
|
into the original array at the index given in the key |
|
1057
|
|
|
|
|
|
|
|
|
1058
|
|
|
|
|
|
|
=item * |
|
1059
|
|
|
|
|
|
|
|
|
1060
|
|
|
|
|
|
|
Indexes are zero based, just as in Perl. |
|
1061
|
|
|
|
|
|
|
|
|
1062
|
|
|
|
|
|
|
=back |
|
1063
|
|
|
|
|
|
|
|
|
1064
|
|
|
|
|
|
|
=cut |
|
1065
|
|
|
|
|
|
|
|
|
1066
|
|
|
|
|
|
|
#=================================== |
|
1067
|
|
|
|
|
|
|
sub _merge_array { |
|
1068
|
|
|
|
|
|
|
#=================================== |
|
1069
|
9
|
|
|
9
|
|
19
|
my ( $self, $key, $config, $local ) = @_; |
|
1070
|
9
|
|
|
|
|
32
|
$self->debug(" ... merging array : $key"); |
|
1071
|
9
|
|
|
|
|
17
|
my $dest = $config->{$key}; |
|
1072
|
9
|
|
|
|
|
16
|
my $merge = $local->{$key}; |
|
1073
|
9
|
|
|
|
|
19
|
my $changes = delete $merge->{'!'}; |
|
1074
|
|
|
|
|
|
|
|
|
1075
|
|
|
|
|
|
|
# Changed elements |
|
1076
|
9
|
|
|
|
|
41
|
foreach my $index ( keys %$merge ) { |
|
1077
|
3
|
50
|
|
|
|
10
|
$index = '' if !defined $index; |
|
1078
|
3
|
100
|
|
|
|
37
|
die "Array override for key '$key' : '$index' is not an integer" |
|
1079
|
|
|
|
|
|
|
unless $index =~ /^\d+$/; |
|
1080
|
2
|
|
|
|
|
7
|
$dest->[$index] = $merge->{$index}; |
|
1081
|
2
|
|
|
|
|
9
|
$self->debug(" ... changing index : $index"); |
|
1082
|
|
|
|
|
|
|
} |
|
1083
|
|
|
|
|
|
|
|
|
1084
|
8
|
|
|
|
|
14
|
my %actions; |
|
1085
|
|
|
|
|
|
|
|
|
1086
|
|
|
|
|
|
|
# Deleted elements |
|
1087
|
8
|
|
100
|
|
|
33
|
my $remove = $changes->{'-'} || []; |
|
1088
|
8
|
100
|
|
|
|
41
|
die "Index delete for key '$key' : '-' is not an array ref" |
|
1089
|
|
|
|
|
|
|
unless ref $remove eq 'ARRAY'; |
|
1090
|
|
|
|
|
|
|
|
|
1091
|
7
|
|
|
|
|
17
|
foreach my $delete_index (@$remove) { |
|
1092
|
15
|
100
|
|
|
|
64
|
next unless $delete_index =~ /^\d+/; |
|
1093
|
14
|
100
|
|
|
|
61
|
$actions{$delete_index} = ['-'] |
|
1094
|
|
|
|
|
|
|
if $delete_index < @$dest; |
|
1095
|
|
|
|
|
|
|
} |
|
1096
|
|
|
|
|
|
|
|
|
1097
|
|
|
|
|
|
|
# Added elements |
|
1098
|
7
|
|
100
|
|
|
27
|
my $add = $changes->{'+'} || []; |
|
1099
|
|
|
|
|
|
|
|
|
1100
|
|
|
|
|
|
|
# Append |
|
1101
|
7
|
100
|
|
|
|
30
|
if ( ref $add eq 'ARRAY' ) { |
|
|
|
100
|
|
|
|
|
|
|
1102
|
3
|
100
|
|
|
|
10
|
if (@$add) { |
|
1103
|
2
|
|
|
|
|
8
|
push @$dest, @$add; |
|
1104
|
2
|
|
|
|
|
10
|
$self->debug( |
|
1105
|
|
|
|
|
|
|
' ... appending ' . ( scalar @$add ) . ' element(s)' ); |
|
1106
|
|
|
|
|
|
|
} |
|
1107
|
|
|
|
|
|
|
} |
|
1108
|
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
# Insert |
|
1110
|
|
|
|
|
|
|
elsif ( ref $add eq 'HASH' ) { |
|
1111
|
3
|
|
|
|
|
12
|
foreach my $add_index ( keys %$add ) { |
|
1112
|
9
|
100
|
|
|
|
41
|
next unless $add_index =~ /^\d+/; |
|
1113
|
8
|
100
|
100
|
|
|
56
|
$actions{$add_index} = [ |
|
1114
|
|
|
|
|
|
|
( exists $actions{$add_index} || $add_index >= @$dest ) |
|
1115
|
|
|
|
|
|
|
? '~' |
|
1116
|
|
|
|
|
|
|
: '+', |
|
1117
|
|
|
|
|
|
|
$add->{$add_index} |
|
1118
|
|
|
|
|
|
|
]; |
|
1119
|
|
|
|
|
|
|
} |
|
1120
|
|
|
|
|
|
|
|
|
1121
|
|
|
|
|
|
|
} |
|
1122
|
|
|
|
|
|
|
else { |
|
1123
|
1
|
|
|
|
|
23
|
die "Array add for key '$key' : '+' is not an array or hash ref"; |
|
1124
|
|
|
|
|
|
|
} |
|
1125
|
|
|
|
|
|
|
|
|
1126
|
6
|
|
|
|
|
27
|
foreach my $index ( sort { $b <=> $a } keys %actions ) { |
|
|
23
|
|
|
|
|
51
|
|
|
1127
|
18
|
|
|
|
|
19
|
my ( $action, $value ) = @{ $actions{$index} }; |
|
|
18
|
|
|
|
|
39
|
|
|
1128
|
18
|
100
|
|
|
|
42
|
if ( $action eq '-' ) { |
|
1129
|
10
|
|
|
|
|
21
|
splice( @$dest, $index, 1 ); |
|
1130
|
10
|
|
|
|
|
32
|
$self->debug(" ... deleting index : $index"); |
|
1131
|
10
|
|
|
|
|
22
|
next; |
|
1132
|
|
|
|
|
|
|
} |
|
1133
|
8
|
100
|
|
|
|
17
|
if ( $action eq '~' ) { |
|
1134
|
6
|
|
|
|
|
12
|
$dest->[$index] = $value; |
|
1135
|
6
|
|
|
|
|
18
|
$self->debug(" ... changing index : $index"); |
|
1136
|
6
|
|
|
|
|
16
|
next; |
|
1137
|
|
|
|
|
|
|
} |
|
1138
|
2
|
|
|
|
|
6
|
splice( @$dest, $index, 0, $value ); |
|
1139
|
2
|
|
|
|
|
7
|
$self->debug(" ... inserting index : $index"); |
|
1140
|
|
|
|
|
|
|
} |
|
1141
|
6
|
|
|
|
|
37
|
return; |
|
1142
|
|
|
|
|
|
|
} |
|
1143
|
|
|
|
|
|
|
|
|
1144
|
|
|
|
|
|
|
=item C |
|
1145
|
|
|
|
|
|
|
|
|
1146
|
|
|
|
|
|
|
$c = Config::Merge->new( |
|
1147
|
|
|
|
|
|
|
path => '/path/to/config', |
|
1148
|
|
|
|
|
|
|
skip => qr/regex/, |
|
1149
|
|
|
|
|
|
|
| [ qr/regex1/, qr/regex2/...] |
|
1150
|
|
|
|
|
|
|
| { name1 => 1, name2 => 2} |
|
1151
|
|
|
|
|
|
|
| sub {} |
|
1152
|
|
|
|
|
|
|
); |
|
1153
|
|
|
|
|
|
|
|
|
1154
|
|
|
|
|
|
|
C allows you to skip the loading of parts of your config |
|
1155
|
|
|
|
|
|
|
tree. For instance, if you don't need a list of cron jobs when running |
|
1156
|
|
|
|
|
|
|
your web server, you can skip it. |
|
1157
|
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
The decision is made based on the path to that value, eg 'app.db.hosts' |
|
1159
|
|
|
|
|
|
|
rather than on filenames. Also, the check is only performed for each |
|
1160
|
|
|
|
|
|
|
new directory or filename - it doesn't check the data within each file. |
|
1161
|
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
To use C, you can either subclass it, or pass in a parameter |
|
1163
|
|
|
|
|
|
|
to new: |
|
1164
|
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
=over |
|
1166
|
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
=item C or C<[qr/regex1/, qr/regex2]> |
|
1168
|
|
|
|
|
|
|
|
|
1169
|
|
|
|
|
|
|
Each regex will be checked against the key path, and if it matches |
|
1170
|
|
|
|
|
|
|
then the loading of that tree will be skipped |
|
1171
|
|
|
|
|
|
|
|
|
1172
|
|
|
|
|
|
|
=item C<< {key_path => 1} >> |
|
1173
|
|
|
|
|
|
|
|
|
1174
|
|
|
|
|
|
|
If the key path exists in the hash, then loading will be skipped |
|
1175
|
|
|
|
|
|
|
|
|
1176
|
|
|
|
|
|
|
=item C or subclassed C |
|
1177
|
|
|
|
|
|
|
|
|
1178
|
|
|
|
|
|
|
sub { |
|
1179
|
|
|
|
|
|
|
my ($self,$key_path) = @_; |
|
1180
|
|
|
|
|
|
|
...make decision... |
|
1181
|
|
|
|
|
|
|
return 1 | 0; |
|
1182
|
|
|
|
|
|
|
} |
|
1183
|
|
|
|
|
|
|
|
|
1184
|
|
|
|
|
|
|
=back |
|
1185
|
|
|
|
|
|
|
|
|
1186
|
|
|
|
|
|
|
=cut |
|
1187
|
|
|
|
|
|
|
|
|
1188
|
|
|
|
|
|
|
#=================================== |
|
1189
|
|
|
|
|
|
|
sub skip { |
|
1190
|
|
|
|
|
|
|
#=================================== |
|
1191
|
133
|
|
|
133
|
1
|
346
|
return; |
|
1192
|
|
|
|
|
|
|
} |
|
1193
|
|
|
|
|
|
|
|
|
1194
|
|
|
|
|
|
|
=item C |
|
1195
|
|
|
|
|
|
|
|
|
1196
|
|
|
|
|
|
|
$c = Config::Merge->new( |
|
1197
|
|
|
|
|
|
|
path => '/path/to/config', |
|
1198
|
|
|
|
|
|
|
is_local => qr/regex/, |
|
1199
|
|
|
|
|
|
|
| [ qr/regex1/, qr/regex2/...] |
|
1200
|
|
|
|
|
|
|
| { name1 => 1, name2 => 2} |
|
1201
|
|
|
|
|
|
|
| sub {} |
|
1202
|
|
|
|
|
|
|
); |
|
1203
|
|
|
|
|
|
|
|
|
1204
|
|
|
|
|
|
|
C indicates whether a file or dir should be considered |
|
1205
|
|
|
|
|
|
|
part of the main config (and thus loaded normally) or part of the |
|
1206
|
|
|
|
|
|
|
local config (and thus merged into the main config). |
|
1207
|
|
|
|
|
|
|
|
|
1208
|
|
|
|
|
|
|
The decision is made based on the name of the file / dir, without |
|
1209
|
|
|
|
|
|
|
any extension. |
|
1210
|
|
|
|
|
|
|
|
|
1211
|
|
|
|
|
|
|
To use C, you can either subclass it, or pass in a parameter |
|
1212
|
|
|
|
|
|
|
to new: |
|
1213
|
|
|
|
|
|
|
|
|
1214
|
|
|
|
|
|
|
=over |
|
1215
|
|
|
|
|
|
|
|
|
1216
|
|
|
|
|
|
|
=item C or C<[qr/regex1/, qr/regex2]> |
|
1217
|
|
|
|
|
|
|
|
|
1218
|
|
|
|
|
|
|
Each regex will be checked against the file/dir name, and if it matches |
|
1219
|
|
|
|
|
|
|
then that tree will be merged |
|
1220
|
|
|
|
|
|
|
|
|
1221
|
|
|
|
|
|
|
=item C<< {filename => 1, dirname => 1} >> |
|
1222
|
|
|
|
|
|
|
|
|
1223
|
|
|
|
|
|
|
If the file/dir name exists in the hash, then that tree will be merged |
|
1224
|
|
|
|
|
|
|
|
|
1225
|
|
|
|
|
|
|
=item C or subclassed C |
|
1226
|
|
|
|
|
|
|
|
|
1227
|
|
|
|
|
|
|
sub { |
|
1228
|
|
|
|
|
|
|
my ($self,$name) = @_; |
|
1229
|
|
|
|
|
|
|
...make decision... |
|
1230
|
|
|
|
|
|
|
return 1 | 0; |
|
1231
|
|
|
|
|
|
|
} |
|
1232
|
|
|
|
|
|
|
|
|
1233
|
|
|
|
|
|
|
=back |
|
1234
|
|
|
|
|
|
|
|
|
1235
|
|
|
|
|
|
|
See L"EXAMPLE USING is_local() AND load_as()">. |
|
1236
|
|
|
|
|
|
|
|
|
1237
|
|
|
|
|
|
|
=cut |
|
1238
|
|
|
|
|
|
|
|
|
1239
|
|
|
|
|
|
|
#=================================== |
|
1240
|
|
|
|
|
|
|
sub is_local { |
|
1241
|
|
|
|
|
|
|
#=================================== |
|
1242
|
148
|
|
|
148
|
1
|
255
|
my ( $self, $filename ) = @_; |
|
1243
|
148
|
|
|
|
|
684
|
return $filename =~ /^local\b/; |
|
1244
|
|
|
|
|
|
|
} |
|
1245
|
|
|
|
|
|
|
|
|
1246
|
|
|
|
|
|
|
=item C |
|
1247
|
|
|
|
|
|
|
|
|
1248
|
|
|
|
|
|
|
$c = Config::Merge->new( |
|
1249
|
|
|
|
|
|
|
path => '/path/to/config', |
|
1250
|
|
|
|
|
|
|
load_as => qr/(regex)/, |
|
1251
|
|
|
|
|
|
|
| sub {} |
|
1252
|
|
|
|
|
|
|
); |
|
1253
|
|
|
|
|
|
|
|
|
1254
|
|
|
|
|
|
|
C returns the name of the key to use when loading |
|
1255
|
|
|
|
|
|
|
the file / dir. By default, it returns the C<$name> for main |
|
1256
|
|
|
|
|
|
|
config files, or C<''> for local files. |
|
1257
|
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
The decision is made based on the name of the file / dir, without |
|
1259
|
|
|
|
|
|
|
any extension. |
|
1260
|
|
|
|
|
|
|
|
|
1261
|
|
|
|
|
|
|
If C returns an empty string, then each key in the file/tree |
|
1262
|
|
|
|
|
|
|
is merged separately. This is how the C files work by default. |
|
1263
|
|
|
|
|
|
|
See L"OVERRIDING CONFIG LOCALLY">. |
|
1264
|
|
|
|
|
|
|
|
|
1265
|
|
|
|
|
|
|
For instance: |
|
1266
|
|
|
|
|
|
|
|
|
1267
|
|
|
|
|
|
|
main.yaml: |
|
1268
|
|
|
|
|
|
|
key1: value |
|
1269
|
|
|
|
|
|
|
key2: value |
|
1270
|
|
|
|
|
|
|
|
|
1271
|
|
|
|
|
|
|
db.yaml: |
|
1272
|
|
|
|
|
|
|
key3: value |
|
1273
|
|
|
|
|
|
|
key4: value |
|
1274
|
|
|
|
|
|
|
|
|
1275
|
|
|
|
|
|
|
local.yaml: |
|
1276
|
|
|
|
|
|
|
main: |
|
1277
|
|
|
|
|
|
|
key1: new_value |
|
1278
|
|
|
|
|
|
|
db: |
|
1279
|
|
|
|
|
|
|
key4: new_value |
|
1280
|
|
|
|
|
|
|
|
|
1281
|
|
|
|
|
|
|
To use C, you can either subclass it, or pass in a parameter |
|
1282
|
|
|
|
|
|
|
to new: |
|
1283
|
|
|
|
|
|
|
|
|
1284
|
|
|
|
|
|
|
=over |
|
1285
|
|
|
|
|
|
|
|
|
1286
|
|
|
|
|
|
|
=item C |
|
1287
|
|
|
|
|
|
|
|
|
1288
|
|
|
|
|
|
|
The regex will be checked against the file/dir name, and if it matches |
|
1289
|
|
|
|
|
|
|
then it returns the string captured in the regex, otherwise it returns |
|
1290
|
|
|
|
|
|
|
the original name. |
|
1291
|
|
|
|
|
|
|
|
|
1292
|
|
|
|
|
|
|
=item C or subclassed C |
|
1293
|
|
|
|
|
|
|
|
|
1294
|
|
|
|
|
|
|
sub { |
|
1295
|
|
|
|
|
|
|
my ($self,$name,$is_local) = @_; |
|
1296
|
|
|
|
|
|
|
...make decision... |
|
1297
|
|
|
|
|
|
|
return 'string'; # string is used as the keyname |
|
1298
|
|
|
|
|
|
|
return ''; # acts like local.* (see above) |
|
1299
|
|
|
|
|
|
|
return undef; # don't load this file/dir |
|
1300
|
|
|
|
|
|
|
} |
|
1301
|
|
|
|
|
|
|
|
|
1302
|
|
|
|
|
|
|
=back |
|
1303
|
|
|
|
|
|
|
|
|
1304
|
|
|
|
|
|
|
Also, see L"EXAMPLE USING is_local() AND load_as()">. |
|
1305
|
|
|
|
|
|
|
|
|
1306
|
|
|
|
|
|
|
=cut |
|
1307
|
|
|
|
|
|
|
|
|
1308
|
|
|
|
|
|
|
#=================================== |
|
1309
|
|
|
|
|
|
|
sub load_as { |
|
1310
|
|
|
|
|
|
|
#=================================== |
|
1311
|
116
|
|
|
116
|
1
|
162
|
my ( $self, $filename, $local ) = @_; |
|
1312
|
116
|
100
|
|
|
|
317
|
return $local ? '' : $filename; |
|
1313
|
|
|
|
|
|
|
} |
|
1314
|
|
|
|
|
|
|
|
|
1315
|
|
|
|
|
|
|
my %callbacks = ( |
|
1316
|
|
|
|
|
|
|
CODE => \&_init_code_callback, |
|
1317
|
|
|
|
|
|
|
HASH => \&_init_hash_callback, |
|
1318
|
|
|
|
|
|
|
ARRAY => \&_init_array_callback, |
|
1319
|
|
|
|
|
|
|
); |
|
1320
|
|
|
|
|
|
|
|
|
1321
|
|
|
|
|
|
|
=item EXAMPLE USING C AND C |
|
1322
|
|
|
|
|
|
|
|
|
1323
|
|
|
|
|
|
|
For instance, instead of using C files, you may want to |
|
1324
|
|
|
|
|
|
|
keep versioned copies of local configs for different machines, and so use: |
|
1325
|
|
|
|
|
|
|
|
|
1326
|
|
|
|
|
|
|
app.yaml |
|
1327
|
|
|
|
|
|
|
app-(dev1.domain.com).yaml |
|
1328
|
|
|
|
|
|
|
app-(dev2.domain.com).yaml |
|
1329
|
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
You would implement this as follows: |
|
1331
|
|
|
|
|
|
|
|
|
1332
|
|
|
|
|
|
|
my $config = Config::Merge->new( |
|
1333
|
|
|
|
|
|
|
path => '/path/to/config', |
|
1334
|
|
|
|
|
|
|
|
|
1335
|
|
|
|
|
|
|
# If matches 'xxx-(yyy)' |
|
1336
|
|
|
|
|
|
|
is_local => sub { |
|
1337
|
|
|
|
|
|
|
my ( $self, $name ) = @_; |
|
1338
|
|
|
|
|
|
|
return $name=~/- [(] .+ [)]/x ? 1 : 0; |
|
1339
|
|
|
|
|
|
|
}, |
|
1340
|
|
|
|
|
|
|
|
|
1341
|
|
|
|
|
|
|
# If local and matches 'xxx-(hostname)', return xxx |
|
1342
|
|
|
|
|
|
|
load_as => sub { |
|
1343
|
|
|
|
|
|
|
my ( $self, $name, $is_local ) = @_; |
|
1344
|
|
|
|
|
|
|
if ($is_local) { |
|
1345
|
|
|
|
|
|
|
if ( $name=~/(.*) - [(] ($hostname) [)] /x ) { |
|
1346
|
|
|
|
|
|
|
return $1; |
|
1347
|
|
|
|
|
|
|
} |
|
1348
|
|
|
|
|
|
|
return undef; |
|
1349
|
|
|
|
|
|
|
} |
|
1350
|
|
|
|
|
|
|
return $name; |
|
1351
|
|
|
|
|
|
|
} |
|
1352
|
|
|
|
|
|
|
); |
|
1353
|
|
|
|
|
|
|
|
|
1354
|
|
|
|
|
|
|
See C for a working illustration. |
|
1355
|
|
|
|
|
|
|
|
|
1356
|
|
|
|
|
|
|
=item C |
|
1357
|
|
|
|
|
|
|
|
|
1358
|
|
|
|
|
|
|
$c = Config::Merge->new( |
|
1359
|
|
|
|
|
|
|
path => '/path/to/config', |
|
1360
|
|
|
|
|
|
|
sort => sub {} |
|
1361
|
|
|
|
|
|
|
); |
|
1362
|
|
|
|
|
|
|
|
|
1363
|
|
|
|
|
|
|
By default, directory entries are sorted alphabetically, with |
|
1364
|
|
|
|
|
|
|
directories before filenames. |
|
1365
|
|
|
|
|
|
|
|
|
1366
|
|
|
|
|
|
|
This would be the order for these directory entries: |
|
1367
|
|
|
|
|
|
|
|
|
1368
|
|
|
|
|
|
|
api/ |
|
1369
|
|
|
|
|
|
|
api-(dev1)/ |
|
1370
|
|
|
|
|
|
|
api.yaml |
|
1371
|
|
|
|
|
|
|
api-(dev1).yaml |
|
1372
|
|
|
|
|
|
|
|
|
1373
|
|
|
|
|
|
|
To override this, you can subclass C or pass it in as a |
|
1374
|
|
|
|
|
|
|
parameter to new: |
|
1375
|
|
|
|
|
|
|
|
|
1376
|
|
|
|
|
|
|
sub { |
|
1377
|
|
|
|
|
|
|
my ($self,$names_array_ref) = @_ |
|
1378
|
|
|
|
|
|
|
...sort... |
|
1379
|
|
|
|
|
|
|
return $names_array_ref; |
|
1380
|
|
|
|
|
|
|
} |
|
1381
|
|
|
|
|
|
|
|
|
1382
|
|
|
|
|
|
|
=cut |
|
1383
|
|
|
|
|
|
|
|
|
1384
|
|
|
|
|
|
|
#=================================== |
|
1385
|
|
|
|
|
|
|
sub sort { |
|
1386
|
|
|
|
|
|
|
#=================================== |
|
1387
|
60
|
|
|
60
|
1
|
117
|
my ( $self, $names ) = @_; |
|
1388
|
60
|
|
|
|
|
1298
|
s/[.]([^.]+$)/ .$1/ foreach @$names; |
|
1389
|
60
|
|
|
|
|
249
|
$names = [ sort { $a cmp $b } @$names ]; |
|
|
168
|
|
|
|
|
389
|
|
|
1390
|
60
|
|
|
|
|
977
|
s/ [.]([^.]+$)/.$1/ foreach @$names; |
|
1391
|
60
|
|
|
|
|
459
|
return $names; |
|
1392
|
|
|
|
|
|
|
} |
|
1393
|
|
|
|
|
|
|
|
|
1394
|
|
|
|
|
|
|
=item C |
|
1395
|
|
|
|
|
|
|
|
|
1396
|
|
|
|
|
|
|
my $config = Config::Merge->new( |
|
1397
|
|
|
|
|
|
|
path => '/path/to/config', |
|
1398
|
|
|
|
|
|
|
debug => 1 | 0 |
|
1399
|
|
|
|
|
|
|
); |
|
1400
|
|
|
|
|
|
|
|
|
1401
|
|
|
|
|
|
|
If C is true, then Config::Merge prints out an explanation |
|
1402
|
|
|
|
|
|
|
of what it is doing on STDERR. |
|
1403
|
|
|
|
|
|
|
|
|
1404
|
|
|
|
|
|
|
=back |
|
1405
|
|
|
|
|
|
|
|
|
1406
|
|
|
|
|
|
|
=cut |
|
1407
|
|
|
|
|
|
|
|
|
1408
|
|
|
|
|
|
|
#=================================== |
|
1409
|
|
|
|
|
|
|
sub debug { |
|
1410
|
|
|
|
|
|
|
#=================================== |
|
1411
|
901
|
|
|
901
|
1
|
1453
|
my $self = shift; |
|
1412
|
901
|
100
|
|
|
|
2341
|
print STDERR ( join( "\n", @_, '' ) ) |
|
1413
|
|
|
|
|
|
|
if $self->{debug}; |
|
1414
|
901
|
|
|
|
|
1180
|
return 1; |
|
1415
|
|
|
|
|
|
|
} |
|
1416
|
|
|
|
|
|
|
|
|
1417
|
|
|
|
|
|
|
#=================================== |
|
1418
|
|
|
|
|
|
|
sub _init_callback { |
|
1419
|
|
|
|
|
|
|
#=================================== |
|
1420
|
132
|
|
|
132
|
|
312
|
my ( $self, $callback, $check ) = @_; |
|
1421
|
|
|
|
|
|
|
|
|
1422
|
|
|
|
|
|
|
# If nothing set, use default or subclassed version |
|
1423
|
132
|
100
|
|
|
|
289
|
unless ($check) { |
|
1424
|
113
|
|
|
|
|
524
|
$self->{$callback} = $self->can($callback); |
|
1425
|
113
|
|
|
|
|
410
|
$self->debug("Using default or subclassed $callback()"); |
|
1426
|
113
|
|
|
|
|
536
|
return; |
|
1427
|
|
|
|
|
|
|
} |
|
1428
|
|
|
|
|
|
|
|
|
1429
|
19
|
100
|
|
|
|
84
|
$check = [$check] |
|
1430
|
|
|
|
|
|
|
unless exists $callbacks{ ref $check }; |
|
1431
|
|
|
|
|
|
|
|
|
1432
|
19
|
|
|
|
|
84
|
$self->debug( 'Using ' . ( ref $check ) . " handler for $callback()" ); |
|
1433
|
|
|
|
|
|
|
|
|
1434
|
19
|
|
|
|
|
76
|
$self->{$callback} = $callbacks{ ref $check }->( $check, $callback ); |
|
1435
|
16
|
|
|
|
|
73
|
return; |
|
1436
|
|
|
|
|
|
|
} |
|
1437
|
|
|
|
|
|
|
|
|
1438
|
|
|
|
|
|
|
#=================================== |
|
1439
|
|
|
|
|
|
|
sub _init_code_callback { |
|
1440
|
|
|
|
|
|
|
#=================================== |
|
1441
|
6
|
|
|
6
|
|
18
|
return $_[0]; |
|
1442
|
|
|
|
|
|
|
} |
|
1443
|
|
|
|
|
|
|
|
|
1444
|
|
|
|
|
|
|
#=================================== |
|
1445
|
|
|
|
|
|
|
sub _init_hash_callback { |
|
1446
|
|
|
|
|
|
|
#=================================== |
|
1447
|
3
|
|
|
3
|
|
9
|
my ( $check, $callback ) = @_; |
|
1448
|
3
|
100
|
|
|
|
21
|
die "load_as() cannot be a hashref" |
|
1449
|
|
|
|
|
|
|
if $callback eq 'load_as'; |
|
1450
|
|
|
|
|
|
|
return sub { |
|
1451
|
14
|
|
|
14
|
|
19
|
my $self = shift; |
|
1452
|
14
|
|
|
|
|
20
|
my $param = shift; |
|
1453
|
14
|
|
|
|
|
51
|
return exists $check->{$param}; |
|
1454
|
2
|
|
|
|
|
12
|
}; |
|
1455
|
|
|
|
|
|
|
} |
|
1456
|
|
|
|
|
|
|
|
|
1457
|
|
|
|
|
|
|
#=================================== |
|
1458
|
|
|
|
|
|
|
sub _init_array_callback { |
|
1459
|
|
|
|
|
|
|
#=================================== |
|
1460
|
10
|
|
|
10
|
|
16
|
my ( $check, $callback ) = @_; |
|
1461
|
10
|
100
|
|
|
|
32
|
if ( $callback eq 'load_as' ) { |
|
1462
|
4
|
100
|
|
|
|
23
|
die "load_as() must contain a single regex" |
|
1463
|
|
|
|
|
|
|
unless @$check == 1; |
|
1464
|
3
|
|
|
|
|
7
|
my $regex = $check->[0]; |
|
1465
|
|
|
|
|
|
|
return sub { |
|
1466
|
37
|
|
|
37
|
|
54
|
my $self = shift; |
|
1467
|
37
|
|
|
|
|
47
|
my $filename = shift; |
|
1468
|
37
|
100
|
|
|
|
215
|
return $filename =~ m/$regex/ |
|
1469
|
|
|
|
|
|
|
? $1 |
|
1470
|
|
|
|
|
|
|
: $filename; |
|
1471
|
3
|
|
|
|
|
18
|
}; |
|
1472
|
|
|
|
|
|
|
} |
|
1473
|
|
|
|
|
|
|
|
|
1474
|
6
|
|
|
|
|
15
|
foreach my $value (@$check) { |
|
1475
|
7
|
|
50
|
|
|
18
|
$value ||= ''; |
|
1476
|
7
|
100
|
|
|
|
33
|
die "'$value' is not a regular expression" |
|
1477
|
|
|
|
|
|
|
unless ref $value eq 'Regexp'; |
|
1478
|
|
|
|
|
|
|
} |
|
1479
|
|
|
|
|
|
|
return sub { |
|
1480
|
31
|
|
|
31
|
|
38
|
my $self = shift; |
|
1481
|
31
|
|
|
|
|
43
|
my $value = shift; |
|
1482
|
31
|
|
|
|
|
52
|
foreach my $regex (@$check) { |
|
1483
|
37
|
100
|
|
|
|
241
|
return 1 if $value =~ m/$regex/; |
|
1484
|
|
|
|
|
|
|
} |
|
1485
|
18
|
|
|
|
|
63
|
return 0; |
|
1486
|
5
|
|
|
|
|
34
|
}; |
|
1487
|
|
|
|
|
|
|
} |
|
1488
|
|
|
|
|
|
|
|
|
1489
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
1490
|
|
|
|
|
|
|
|
|
1491
|
|
|
|
|
|
|
L, L, L, |
|
1492
|
|
|
|
|
|
|
L, L, L, |
|
1493
|
|
|
|
|
|
|
L |
|
1494
|
|
|
|
|
|
|
|
|
1495
|
|
|
|
|
|
|
=head1 THANKS |
|
1496
|
|
|
|
|
|
|
|
|
1497
|
|
|
|
|
|
|
Thanks to Hasanuddin Tamir [HASANT] for vacating the Config::Merge namespace, |
|
1498
|
|
|
|
|
|
|
which allowed me to rename Config::Loader to the more meaningful Config::Merge. |
|
1499
|
|
|
|
|
|
|
|
|
1500
|
|
|
|
|
|
|
His version of Config::Merge can be found in |
|
1501
|
|
|
|
|
|
|
L. |
|
1502
|
|
|
|
|
|
|
|
|
1503
|
|
|
|
|
|
|
Thanks to Joel Bernstein and Brian Cassidy for the interface to the various |
|
1504
|
|
|
|
|
|
|
configuration modules. Also to Ewan Edwards for his suggestions about how |
|
1505
|
|
|
|
|
|
|
to make Config::Merge more flexible. |
|
1506
|
|
|
|
|
|
|
|
|
1507
|
|
|
|
|
|
|
=head1 BUGS |
|
1508
|
|
|
|
|
|
|
|
|
1509
|
|
|
|
|
|
|
No bugs have been reported. |
|
1510
|
|
|
|
|
|
|
|
|
1511
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
|
1512
|
|
|
|
|
|
|
L. |
|
1513
|
|
|
|
|
|
|
|
|
1514
|
|
|
|
|
|
|
=head1 AUTHOR |
|
1515
|
|
|
|
|
|
|
|
|
1516
|
|
|
|
|
|
|
Clinton Gormley, Eclinton@traveljury.comE |
|
1517
|
|
|
|
|
|
|
|
|
1518
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
1519
|
|
|
|
|
|
|
|
|
1520
|
|
|
|
|
|
|
Copyright (C) 2007-2010 by Clinton Gormley |
|
1521
|
|
|
|
|
|
|
|
|
1522
|
|
|
|
|
|
|
=cut |
|
1523
|
|
|
|
|
|
|
|
|
1524
|
|
|
|
|
|
|
=head1 LICENSE |
|
1525
|
|
|
|
|
|
|
|
|
1526
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
1527
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.7 or, |
|
1528
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
|
1529
|
|
|
|
|
|
|
|
|
1530
|
|
|
|
|
|
|
|
|
1531
|
|
|
|
|
|
|
=cut |
|
1532
|
|
|
|
|
|
|
|
|
1533
|
|
|
|
|
|
|
1 |
|
1534
|
|
|
|
|
|
|
|