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