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