line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::SimpleConfig; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$MooseX::SimpleConfig::VERSION = '0.10'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# git description: v0.09-15-g258236b |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
BEGIN { |
8
|
4
|
|
|
4
|
|
1675274
|
$MooseX::SimpleConfig::AUTHORITY = 'cpan:STEVAN'; |
9
|
|
|
|
|
|
|
} |
10
|
|
|
|
|
|
|
# ABSTRACT: A Moose role for setting attributes from a simple configfile |
11
|
|
|
|
|
|
|
|
12
|
4
|
|
|
4
|
|
2916
|
use Moose::Role; |
|
4
|
|
|
|
|
446179
|
|
|
4
|
|
|
|
|
24
|
|
13
|
|
|
|
|
|
|
with 'MooseX::ConfigFromFile'; |
14
|
|
|
|
|
|
|
|
15
|
4
|
|
|
4
|
|
23099
|
use Config::Any 0.13 (); |
|
4
|
|
|
|
|
37203
|
|
|
4
|
|
|
|
|
771
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub get_config_from_file { |
18
|
0
|
|
|
0
|
|
|
my ($class, $file) = @_; |
19
|
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
$file = $file->() if ref $file eq 'CODE'; |
21
|
0
|
0
|
|
|
|
|
my $files_ref = ref $file eq 'ARRAY' ? $file : [$file]; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my $can_config_any_args = $class->can('config_any_args'); |
24
|
0
|
0
|
|
|
|
|
my $extra_args = $can_config_any_args ? |
25
|
|
|
|
|
|
|
$can_config_any_args->($class, $file) : {}; |
26
|
|
|
|
|
|
|
; |
27
|
0
|
|
|
|
|
|
my $raw_cfany = Config::Any->load_files({ |
28
|
|
|
|
|
|
|
%$extra_args, |
29
|
|
|
|
|
|
|
use_ext => 1, |
30
|
|
|
|
|
|
|
files => $files_ref, |
31
|
|
|
|
|
|
|
flatten_to_hash => 1, |
32
|
|
|
|
|
|
|
} ); |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my %raw_config; |
35
|
0
|
|
|
|
|
|
foreach my $file_tested ( reverse @{$files_ref} ) { |
|
0
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
|
if ( ! exists $raw_cfany->{$file_tested} ) { |
37
|
0
|
|
|
|
|
|
warn qq{Specified configfile '$file_tested' does not exist, } . |
38
|
|
|
|
|
|
|
qq{is empty, or is not readable\n}; |
39
|
0
|
|
|
|
|
|
next; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $cfany_hash = $raw_cfany->{$file_tested}; |
43
|
0
|
0
|
0
|
|
|
|
die "configfile must represent a hash structure in file: $file_tested" |
|
|
|
0
|
|
|
|
|
44
|
|
|
|
|
|
|
unless $cfany_hash && ref $cfany_hash && ref $cfany_hash eq 'HASH'; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
%raw_config = ( %raw_config, %{$cfany_hash} ); |
|
0
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
\%raw_config; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
4
|
|
|
4
|
|
33
|
no Moose::Role; 1; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
40
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
__END__ |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=pod |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=encoding UTF-8 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=for :stopwords Brandon L. Black <blblack@gmail.com> Alex Doran Yuval Kogman Zbigniew |
61
|
|
|
|
|
|
|
Lukasiak lestrrat Howarth Alexander Hartmaier L Karen Etheridge Tomas |
62
|
|
|
|
|
|
|
configfile |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 VERSION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
version 0.10 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
## A YAML configfile named /etc/my_app.yaml: |
75
|
|
|
|
|
|
|
foo: bar |
76
|
|
|
|
|
|
|
baz: 123 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
## In your class |
79
|
|
|
|
|
|
|
package My::App; |
80
|
|
|
|
|
|
|
use Moose; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
with 'MooseX::SimpleConfig'; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
has 'foo' => (is => 'ro', isa => 'Str', required => 1); |
85
|
|
|
|
|
|
|
has 'baz' => (is => 'rw', isa => 'Int', required => 1); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# ... rest of the class here |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
## in your script |
90
|
|
|
|
|
|
|
#!/usr/bin/perl |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
use My::App; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml'); |
95
|
|
|
|
|
|
|
# ... rest of the script here |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
#################### |
98
|
|
|
|
|
|
|
###### combined with MooseX::Getopt: |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
## In your class |
101
|
|
|
|
|
|
|
package My::App; |
102
|
|
|
|
|
|
|
use Moose; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
with 'MooseX::SimpleConfig'; |
105
|
|
|
|
|
|
|
with 'MooseX::Getopt'; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
has 'foo' => (is => 'ro', isa => 'Str', required => 1); |
108
|
|
|
|
|
|
|
has 'baz' => (is => 'rw', isa => 'Int', required => 1); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# ... rest of the class here |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
## in your script |
113
|
|
|
|
|
|
|
#!/usr/bin/perl |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
use My::App; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my $app = My::App->new_with_options(); |
118
|
|
|
|
|
|
|
# ... rest of the script here |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
## on the command line |
121
|
|
|
|
|
|
|
% perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123 |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 DESCRIPTION |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
This role loads simple configfiles to set object attributes. It |
126
|
|
|
|
|
|
|
is based on the abstract role L<MooseX::ConfigFromFile>, and uses |
127
|
|
|
|
|
|
|
L<Config::Any> to load your configfile. L<Config::Any> will in |
128
|
|
|
|
|
|
|
turn support any of a variety of different config formats, detected |
129
|
|
|
|
|
|
|
by the file extension. See L<Config::Any> for more details about |
130
|
|
|
|
|
|
|
supported formats. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
To pass additional arguments to L<Config::Any> you must provide a |
133
|
|
|
|
|
|
|
C<config_any_args()> method, for example: |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub config_any_args { |
136
|
|
|
|
|
|
|
return { |
137
|
|
|
|
|
|
|
driver_args => { General => { '-InterPolateVars' => 1 } } |
138
|
|
|
|
|
|
|
}; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this |
142
|
|
|
|
|
|
|
module is automatically supported by the L<MooseX::Getopt> role as |
143
|
|
|
|
|
|
|
well, which allows specifying C<-configfile> on the command line. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 configfile |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Provided by the base role L<MooseX::ConfigFromFile>. You can |
150
|
|
|
|
|
|
|
provide a default configfile pathname like so: |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
has '+configfile' => ( default => '/etc/myapp.yaml' ); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
You can pass an array of filenames if you want, but as usual the array |
155
|
|
|
|
|
|
|
has to be wrapped in a sub ref. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
has '+configfile' => ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } ); |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Config files are trivially merged at the top level, with the right-hand files taking precedence. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 CLASS METHODS |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 new_with_config |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Provided by the base role L<MooseX::ConfigFromFile>. Acts just like |
166
|
|
|
|
|
|
|
regular C<new()>, but also accepts an argument C<configfile> to specify |
167
|
|
|
|
|
|
|
the configfile from which to load other attributes. Explicit arguments |
168
|
|
|
|
|
|
|
to C<new_with_config> will override anything loaded from the configfile. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 get_config_from_file |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Called internally by either C<new_with_config> or L<MooseX::Getopt>'s |
173
|
|
|
|
|
|
|
C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 AUTHOR |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Brandon L. Black <blblack@gmail.com> |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
This software is copyright (c) 2007 by Brandon L. Black <blblack@gmail.com>. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
184
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=over 4 |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item * |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Alex Howarth <alex.howarth@gmail.com> |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item * |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Alexander Hartmaier <alex.hartmaier@gmail.com> |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item * |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Brandon L Black <blblack@gmail.com> |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item * |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Karen Etheridge <ether@cpan.org> |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item * |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Tomas Doran <bobtfish@bobtfish.net> |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item * |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Yuval Kogman <nothingmuch@woobling.org> |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item * |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Zbigniew Lukasiak <zby@cpan.org> |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item * |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
lestrrat <lestrrat+github@gmail.com> |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=back |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=cut |