line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::ConfigFromFile; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
121449
|
use strict; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
83
|
|
4
|
2
|
|
|
2
|
|
16
|
use warnings FATAL => 'all'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
216
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.009'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my %loaded_configs; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub import |
11
|
|
|
|
|
|
|
{ |
12
|
10
|
|
|
10
|
|
26844
|
my (undef, %import_options) = @_; |
13
|
10
|
|
|
|
|
32
|
my $target = caller; |
14
|
10
|
|
|
|
|
26
|
my @target_isa; |
15
|
2
|
|
|
2
|
|
17
|
{ no strict 'refs'; @target_isa = @{"${target}::ISA"} }; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
990
|
|
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
19
|
|
|
10
|
|
|
|
|
48
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#don't add this to a role |
18
|
|
|
|
|
|
|
#ISA of a role is always empty ! |
19
|
|
|
|
|
|
|
## no critic qw/ProhibitStringyEval/ |
20
|
10
|
100
|
|
|
|
93
|
@target_isa or return; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $apply_modifiers = sub { |
23
|
9
|
100
|
|
9
|
|
93
|
return if $target->can('_initialize_from_config'); |
24
|
8
|
|
|
|
|
32
|
my $with = $target->can('with'); |
25
|
8
|
|
|
|
|
31
|
$with->('MooX::ConfigFromFile::Role'); |
26
|
8
|
100
|
|
|
|
41392
|
$import_options{config_sortedbyfilename} and $with->('MooX::ConfigFromFile::Role::SortedByFilename'); |
27
|
8
|
100
|
|
|
|
2377
|
$import_options{config_hashmergeloaded} and $with->('MooX::ConfigFromFile::Role::HashMergeLoaded'); |
28
|
9
|
|
|
|
|
48
|
}; |
29
|
9
|
|
|
|
|
28
|
$apply_modifiers->(); |
30
|
|
|
|
|
|
|
|
31
|
9
|
|
|
|
|
2646
|
my $around; |
32
|
|
|
|
|
|
|
defined $import_options{config_singleton} and $import_options{config_singleton} and do |
33
|
9
|
100
|
100
|
|
|
39
|
{ |
34
|
1
|
|
|
|
|
9
|
$around = $target->can('around'); |
35
|
|
|
|
|
|
|
$around->( |
36
|
|
|
|
|
|
|
_build_loaded_config => sub { |
37
|
3
|
|
|
3
|
|
70
|
my $orig = shift; |
38
|
3
|
|
|
|
|
6
|
my $class = shift; |
39
|
3
|
100
|
|
|
|
12
|
defined $loaded_configs{$class} or $loaded_configs{$class} = $class->$orig(@_); |
40
|
3
|
|
|
|
|
14
|
return $loaded_configs{$class}; |
41
|
|
|
|
|
|
|
} |
42
|
1
|
|
|
|
|
10
|
); |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
9
|
|
|
|
|
451
|
my %default_modifiers = ( |
46
|
|
|
|
|
|
|
config_prefix => '_build_config_prefix', |
47
|
|
|
|
|
|
|
config_prefixes => '_build_config_prefixes', |
48
|
|
|
|
|
|
|
config_identifier => '_build_config_identifier', |
49
|
|
|
|
|
|
|
config_prefix_map_separator => '_build_config_prefix_map_separator', |
50
|
|
|
|
|
|
|
config_extensions => '_build_config_extensions', |
51
|
|
|
|
|
|
|
config_dirs => '_build_config_dirs', |
52
|
|
|
|
|
|
|
config_files => '_build_config_files', |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
|
55
|
9
|
|
|
|
|
33
|
foreach my $opt_key (keys %default_modifiers) |
56
|
|
|
|
|
|
|
{ |
57
|
63
|
100
|
|
|
|
911
|
exists $import_options{$opt_key} or next; |
58
|
2
|
100
|
|
|
|
12
|
$around or $around = $target->can('around'); |
59
|
2
|
|
|
4
|
|
17
|
$around->($default_modifiers{$opt_key} => sub { $import_options{$opt_key} }); |
|
4
|
|
|
|
|
122
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
9
|
|
|
|
|
773
|
return; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 NAME |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
MooX::ConfigFromFile - Moo eXtension for initializing objects from config file |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=begin html |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=end html |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 SYNOPSIS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
package Role::Action; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
use Moo::Role; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
has operator => ( is => "ro" ); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
package Action; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
use Moo; |
87
|
|
|
|
|
|
|
use MooX::ConfigFromFile; # imports the MooX::ConfigFromFile::Role |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
with "Role::Action"; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub operate { return say shift->operator; } |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
package OtherAction; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
use Moo; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
with "Role::Action", "MooX::ConfigFromFile::Role"; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub operate { return warn shift->operator; } |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
package QuiteOtherOne; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
use Moo; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# consumes the MooX::ConfigFromFile::Role but load config only once |
106
|
|
|
|
|
|
|
use MooX::ConfigFromFile config_singleton => 1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
with "Role::Action"; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _build_config_prefix { "die" } |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub operate { return die shift->operator; } |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
package main; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
my $action = Action->new(); # tries to find a config file in config_dirs and loads it |
117
|
|
|
|
|
|
|
my $other = OtherAction->new( config_prefix => "warn" ); # use another config file |
118
|
|
|
|
|
|
|
my $quite_o = QuiteOtherOne->new(); # quite another way to have an individual config file |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 DESCRIPTION |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
This module is intended to easy load initialization values for attributes |
123
|
|
|
|
|
|
|
on object construction from an appropriate config file. The building is |
124
|
|
|
|
|
|
|
done in L - using MooX::ConfigFromFile ensures |
125
|
|
|
|
|
|
|
the role is applied. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
For easier usage, with 0.004, several options can be passed via I |
128
|
|
|
|
|
|
|
in default initializers for appropriate role attributes: |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=over 4 |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item C |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Default for L. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item C |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Default for L. Ensure when use |
139
|
|
|
|
|
|
|
this flag together with L to load C before |
140
|
|
|
|
|
|
|
C. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item C |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Default for L. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
package Foo; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# apply role MooX::ConfigFromFile::Role and override default for |
149
|
|
|
|
|
|
|
# attribute config_prefix_map_separator |
150
|
|
|
|
|
|
|
use MooX::ConfigFromFile config_prefix_map_separator => "~"; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
... |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item C |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Default for L. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item C |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Default for L. |
161
|
|
|
|
|
|
|
Same warning regarding modifying this attribute applies here: |
162
|
|
|
|
|
|
|
Possible, but use with caution! |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
package Foo; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
use MooX::ConfigFromFile config_dirs => [qw(/opt/foo/etc /home/alfred/area/foo/etc)]; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
... |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item C |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Default for L. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Reasonable when you want exactly one config file in development mode. |
175
|
|
|
|
|
|
|
For production code it is highly recommended to override the builder. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item C |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Flag adding a wrapper L<< around|Class::Method::Modifiers/around method(s) => sub { ... }; >> |
180
|
|
|
|
|
|
|
the I of L to ensure a |
181
|
|
|
|
|
|
|
config is loaded only once per class. The I restriction results |
182
|
|
|
|
|
|
|
from applicable modifiers per class (and singletons are per class). |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item C |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Default for L. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
package Foo; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# apply role MooX::ConfigFromFile::Role and override default for |
191
|
|
|
|
|
|
|
# attribute config_identifier - means to look e.g. in /etc/foo/ |
192
|
|
|
|
|
|
|
use MooX::ConfigFromFile config_identifier => "foo"; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
... |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item C |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Consumes role L directly after |
199
|
|
|
|
|
|
|
L has been consumed. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=back |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 AUTHOR |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Jens Rehsack, C<< >> |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 BUGS |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
210
|
|
|
|
|
|
|
C, or through the web interface at |
211
|
|
|
|
|
|
|
L. |
212
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress |
213
|
|
|
|
|
|
|
on your bug as I make changes. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 SUPPORT |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
perldoc MooX::ConfigFromFile |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
You can also look for information at: |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=over 4 |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
L |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
L |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item * CPAN Ratings |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
L |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item * Search CPAN |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
L |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=back |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Copyright 2013-2018 Jens Rehsack. |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
250
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
251
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
See L for more information. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
1; # End of MooX::ConfigFromFile |