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