7 use Module::Pluggable::Object ();
13 Config::Any - Load configuration from different file formats, transparently
19 my $cfg = Config::Any->load_stems({stems => \@filepath_stems, ... });
21 my $cfg = Config::Any->load_files({files => \@filepaths, ... });
24 my ($filename, $config) = %$_;
25 $class->config($config);
26 warn "loaded config from file: $filename";
31 L<Config::Any|Config::Any> provides a facility for Perl applications and libraries
32 to load configuration data from multiple different file formats. It supports XML, YAML,
33 JSON, Apache-style configuration, Windows INI files, and even Perl code.
35 The rationale for this module is as follows: Perl programs are deployed on many different
36 platforms and integrated with many different systems. Systems administrators and end
37 users may prefer different configuration formats than the developers. The flexibility
38 inherent in a multiple format configuration loader allows different users to make
39 different choices, without generating extra work for the developers. As a developer
40 you only need to learn a single interface to be able to use the power of different
41 configuration formats.
47 =head2 load_files( \%args )
49 Config::Any->load_files( { files => \@files } );
50 Config::Any->load_files( { files => \@files, filter => \&filter } );
51 Config::Any->load_files( { files => \@files, use_ext => 1 } );
53 C<load_files()> attempts to load configuration from the list of files passed in
54 the C<files> parameter, if the file exists.
56 If the C<filter> parameter is set, it is used as a callback to modify the configuration
57 data before it is returned. It will be passed a single hash-reference parameter which
58 it should modify in-place.
60 If the C<use_ext> parameter is defined, the loader will attempt to parse the file
61 extension from each filename and will skip the file unless it matches a standard
62 extension for the loading plugins. Only plugins whose standard extensions match the
63 file extension will be used. For efficiency reasons, its use is encouraged, but
64 be aware that you will lose flexibility -- for example, a file called C<myapp.cfg>
65 containing YAML data will not be offered to the YAML plugin, whereas C<myapp.yml>
66 or C<myapp.yaml> would be.
68 C<load_files()> also supports a 'force_plugins' parameter, whose value should be an
69 arrayref of plugin names like C<Config::Any::INI>. Its intended use is to allow the use
70 of a non-standard file extension while forcing it to be offered to a particular parser.
71 It is not compatible with 'use_ext'.
73 You can supply a C<driver_args> hashref to pass special options to a particular
74 parser object. Example:
76 Config::Any->load_files( { files => \@files, driver_args => {
77 General => { -LowerCaseNames => 1 }
83 my ( $class, $args ) = @_;
85 unless ( $args && exists $args->{ files } ) {
86 warn "No files specified!";
90 return $class->_load( $args );
93 =head2 load_stems( \%args )
95 Config::Any->load_stems( { stems => \@stems } );
96 Config::Any->load_stems( { stems => \@stems, filter => \&filter } );
97 Config::Any->load_stems( { stems => \@stems, use_ext => 1 } );
99 C<load_stems()> attempts to load configuration from a list of files which it generates
100 by combining the filename stems list passed in the C<stems> parameter with the
101 potential filename extensions from each loader, which you can check with the
102 C<extensions()> classmethod described below. Once this list of possible filenames is
103 built it is treated exactly as in C<load_files()> above, as which it takes the same
104 parameters. Please read the C<load_files()> documentation before using this method.
109 my ( $class, $args ) = @_;
111 unless ( $args && exists $args->{ stems } ) {
112 warn "No stems specified!";
116 my $stems = delete $args->{ stems };
118 for my $s ( @$stems ) {
119 for my $ext ( $class->extensions ) {
120 push @files, "$s.$ext";
124 $args->{ files } = \@files;
125 return $class->_load( $args );
129 my ( $class, $args ) = @_;
130 croak "_load requires a arrayref of file paths" unless $args->{ files };
132 my $force = defined $args->{ force_plugins };
133 if ( !$force and !defined $args->{ use_ext } ) {
135 "use_ext argument was not explicitly set, as of 0.09, this is true by default";
136 $args->{ use_ext } = 1;
139 # figure out what plugins we're using
140 my @plugins = $force ? @{ $args->{ force_plugins } } : $class->plugins;
142 # map extensions if we have to
143 my ( %extension_lut, $extension_re );
144 my $use_ext_lut = !$force && $args->{ use_ext };
145 if ( $use_ext_lut ) {
146 for my $plugin ( @plugins ) {
147 $extension_lut{ $_ } = $plugin for $plugin->extensions;
150 $extension_re = join( '|', keys %extension_lut );
153 # map args to plugins
154 my $base_class = __PACKAGE__;
156 for my $plugin ( @plugins ) {
157 $plugin =~ m{^$base_class\::(.+)};
158 $loader_args{ $plugin } = $args->{ driver_args }->{ $1 } || {};
163 for my $filename ( @{ $args->{ files } } ) {
165 # don't even bother if it's not there
166 next unless -f $filename;
168 my @try_plugins = @plugins;
170 if ( $use_ext_lut ) {
171 $filename =~ m{\.($extension_re)\z};
173 @try_plugins = $extension_lut{ $1 };
176 for my $loader ( @try_plugins ) {
177 next unless $loader->is_supported;
179 = eval { $loader->load( $filename, $loader_args{ $loader } ); };
181 # fatal error if we used extension matching
182 croak "Error parsing $filename: $@" if $@ and $use_ext_lut;
183 next if $@ or !@configs;
185 # post-process config with a filter callback
186 if ( $args->{ filter } ) {
187 $args->{ filter }->( $_ ) for @configs;
191 { $filename => @configs == 1 ? $configs[ 0 ] : \@configs };
201 The C<finder()> classmethod returns the
202 L<Module::Pluggable::Object|Module::Pluggable::Object>
203 object which is used to load the plugins. See the documentation for that module for
210 my $finder = Module::Pluggable::Object->new(
211 search_path => [ __PACKAGE__ ],
219 The C<plugins()> classmethod returns the names of configuration loading plugins as
220 found by L<Module::Pluggable::Object|Module::Pluggable::Object>.
226 return $class->finder->plugins;
231 The C<extensions()> classmethod returns the possible file extensions which can be loaded
232 by C<load_stems()> and C<load_files()>. This may be useful if you set the C<use_ext>
233 parameter to those methods.
239 my @ext = map { $_->extensions } $class->plugins;
240 return wantarray ? @ext : \@ext;
247 =item C<No files specified!> or C<No stems specified!>
249 The C<load_files()> and C<load_stems()> methods will issue this warning if
250 called with an empty list of files/stems to load.
252 =item C<_load requires a arrayref of file paths>
254 This fatal error will be thrown by the internal C<_load> method. It should not occur
255 but is specified here for completeness. If your code dies with this error, please
256 email a failing test case to the authors below.
260 =head1 CONFIGURATION AND ENVIRONMENT
262 Config::Any requires no configuration files or environment variables.
266 L<Module::Pluggable|Module::Pluggable>
268 And at least one of the following:
269 L<Config::General|Config::General>
270 L<Config::Tiny|Config::Tiny>
273 L<JSON::Syck|JSON::Syck>
274 L<YAML::Syck|YAML::Syck>
275 L<XML::Simple|XML::Simple>
277 =head1 INCOMPATIBILITIES
281 =head1 BUGS AND LIMITATIONS
283 No bugs have been reported.
285 Please report any bugs or feature requests to
286 C<bug-config-any@rt.cpan.org>, or through the web interface at
287 L<http://rt.cpan.org>.
291 Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
295 This module was based on the original
296 L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader>
297 module by Brian Cassidy C<< <bricas@cpan.org> >>.
299 With ideas and support from Matt S Trout C<< <mst@shadowcatsystems.co.uk> >>.
301 Further enhancements suggested by Evan Kaufman C<< <evank@cpan.org> >>.
303 =head1 LICENCE AND COPYRIGHT
305 Copyright (c) 2006, Portugal Telecom C<< http://www.sapo.pt/ >>. All rights reserved.
306 Portions copyright 2007, Joel Bernstein C<< <rataxis@cpan.org> >>.
308 This module is free software; you can redistribute it and/or
309 modify it under the same terms as Perl itself. See L<perlartistic>.
311 =head1 DISCLAIMER OF WARRANTY
313 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
314 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
315 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
316 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
317 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
318 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
319 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
320 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
321 NECESSARY SERVICING, REPAIR, OR CORRECTION.
323 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
324 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
325 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
326 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
327 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
328 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
329 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
330 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
331 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
336 L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader>
337 -- now a wrapper around this module.