7 use Module::Pluggable::Object ();
13 Config::Any - Load configuration from different file formats, transparently
17 This document describes Config::Any version 0.10
23 my $cfg = Config::Any->load_stems({stems => \@filepath_stems, ... });
25 my $cfg = Config::Any->load_files({files => \@filepaths, ... });
28 my ($filename, $config) = %$_;
29 $class->config($config);
30 warn "loaded config from file: $filename";
35 L<Config::Any|Config::Any> provides a facility for Perl applications and libraries
36 to load configuration data from multiple different file formats. It supports XML, YAML,
37 JSON, Apache-style configuration, Windows INI files, and even Perl code.
39 The rationale for this module is as follows: Perl programs are deployed on many different
40 platforms and integrated with many different systems. Systems administrators and end
41 users may prefer different configuration formats than the developers. The flexibility
42 inherent in a multiple format configuration loader allows different users to make
43 different choices, without generating extra work for the developers. As a developer
44 you only need to learn a single interface to be able to use the power of different
45 configuration formats.
51 =head2 load_files( \%args )
53 Config::Any->load_files( { files => \@files } );
54 Config::Any->load_files( { files => \@files, filter => \&filter } );
55 Config::Any->load_files( { files => \@files, use_ext => 1 } );
57 C<load_files()> attempts to load configuration from the list of files passed in
58 the C<files> parameter, if the file exists.
60 If the C<filter> parameter is set, it is used as a callback to modify the configuration
61 data before it is returned. It will be passed a single hash-reference parameter which
62 it should modify in-place.
64 If the C<use_ext> parameter is defined, the loader will attempt to parse the file
65 extension from each filename and will skip the file unless it matches a standard
66 extension for the loading plugins. Only plugins whose standard extensions match the
67 file extension will be used. For efficiency reasons, its use is encouraged, but
68 be aware that you will lose flexibility -- for example, a file called C<myapp.cfg>
69 containing YAML data will not be offered to the YAML plugin, whereas C<myapp.yml>
70 or C<myapp.yaml> would be.
72 C<load_files()> also supports a 'force_plugins' parameter, whose value should be an
73 arrayref of plugin names like C<Config::Any::INI>. Its intended use is to allow the use
74 of a non-standard file extension while forcing it to be offered to a particular parser.
75 It is not compatible with 'use_ext'.
77 You can supply a C<driver_args> hashref to pass special options to a particular
78 parser object. Example:
80 Config::Any->load_files( { files => \@files, driver_args => {
81 General => { -LowerCaseNames => 1 }
87 my ( $class, $args ) = @_;
89 unless ( $args && exists $args->{ files } ) {
90 warn "No files specified!";
94 return $class->_load( $args );
97 =head2 load_stems( \%args )
99 Config::Any->load_stems( { stems => \@stems } );
100 Config::Any->load_stems( { stems => \@stems, filter => \&filter } );
101 Config::Any->load_stems( { stems => \@stems, use_ext => 1 } );
103 C<load_stems()> attempts to load configuration from a list of files which it generates
104 by combining the filename stems list passed in the C<stems> parameter with the
105 potential filename extensions from each loader, which you can check with the
106 C<extensions()> classmethod described below. Once this list of possible filenames is
107 built it is treated exactly as in C<load_files()> above, as which it takes the same
108 parameters. Please read the C<load_files()> documentation before using this method.
113 my ( $class, $args ) = @_;
115 unless ( $args && exists $args->{ stems } ) {
116 warn "No stems specified!";
120 my $stems = delete $args->{ stems };
122 for my $s ( @$stems ) {
123 for my $ext ( $class->extensions ) {
124 push @files, "$s.$ext";
128 $args->{ files } = \@files;
129 return $class->_load( $args );
133 my ( $class, $args ) = @_;
134 croak "_load requires a arrayref of file paths" unless $args->{ files };
136 my $force = defined $args->{ force_plugins };
137 if ( !$force and !defined $args->{ use_ext } ) {
139 "use_ext argument was not explicitly set, as of 0.09, this is true by default";
140 $args->{ use_ext } = 1;
143 # figure out what plugins we're using
144 my @plugins = $force ? @{ $args->{ force_plugins } } : $class->plugins;
146 # map extensions if we have to
147 my ( %extension_lut, $extension_re );
148 my $use_ext_lut = !$force && $args->{ use_ext };
149 if ( $use_ext_lut ) {
150 for my $plugin ( @plugins ) {
151 $extension_lut{ $_ } = $plugin for $plugin->extensions;
154 $extension_re = join( '|', keys %extension_lut );
157 # map args to plugins
158 my $base_class = __PACKAGE__;
160 for my $plugin ( @plugins ) {
161 $plugin =~ m{^$base_class\::(.+)};
162 $loader_args{ $plugin } = $args->{ driver_args }->{ $1 } || {};
167 for my $filename ( @{ $args->{ files } } ) {
169 # don't even bother if it's not there
170 next unless -f $filename;
172 my @try_plugins = @plugins;
174 if ( $use_ext_lut ) {
175 $filename =~ m{\.($extension_re)\z};
177 @try_plugins = $extension_lut{ $1 };
180 for my $loader ( @try_plugins ) {
181 next unless $loader->is_supported;
183 = eval { $loader->load( $filename, $loader_args{ $loader } ); };
185 # fatal error if we used extension matching
186 croak "Error parsing file: $filename" if $@ and $use_ext_lut;
187 next if $@ or !@configs;
189 # post-process config with a filter callback
190 if ( $args->{ filter } ) {
191 $args->{ filter }->( $_ ) for @configs;
195 { $filename => @configs == 1 ? $configs[ 0 ] : \@configs };
205 The C<finder()> classmethod returns the
206 L<Module::Pluggable::Object|Module::Pluggable::Object>
207 object which is used to load the plugins. See the documentation for that module for
214 my $finder = Module::Pluggable::Object->new(
215 search_path => [ __PACKAGE__ ],
223 The C<plugins()> classmethod returns the names of configuration loading plugins as
224 found by L<Module::Pluggable::Object|Module::Pluggable::Object>.
230 return $class->finder->plugins;
235 The C<extensions()> classmethod returns the possible file extensions which can be loaded
236 by C<load_stems()> and C<load_files()>. This may be useful if you set the C<use_ext>
237 parameter to those methods.
243 my @ext = map { $_->extensions } $class->plugins;
244 return wantarray ? @ext : \@ext;
251 =item C<No files specified!> or C<No stems specified!>
253 The C<load_files()> and C<load_stems()> methods will issue this warning if
254 called with an empty list of files/stems to load.
256 =item C<_load requires a arrayref of file paths>
258 This fatal error will be thrown by the internal C<_load> method. It should not occur
259 but is specified here for completeness. If your code dies with this error, please
260 email a failing test case to the authors below.
264 =head1 CONFIGURATION AND ENVIRONMENT
266 Config::Any requires no configuration files or environment variables.
270 L<Module::Pluggable|Module::Pluggable>
272 And at least one of the following:
273 L<Config::General|Config::General>
274 L<Config::Tiny|Config::Tiny>
277 L<JSON::Syck|JSON::Syck>
278 L<YAML::Syck|YAML::Syck>
279 L<XML::Simple|XML::Simple>
281 =head1 INCOMPATIBILITIES
285 =head1 BUGS AND LIMITATIONS
287 No bugs have been reported.
289 Please report any bugs or feature requests to
290 C<bug-config-any@rt.cpan.org>, or through the web interface at
291 L<http://rt.cpan.org>.
295 Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
299 This module was based on the original
300 L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader>
301 module by Brian Cassidy C<< <bricas@cpan.org> >>.
303 With ideas and support from Matt S Trout C<< <mst@shadowcatsystems.co.uk> >>.
305 Further enhancements suggested by Evan Kaufman C<< <evank@cpan.org> >>.
307 =head1 LICENCE AND COPYRIGHT
309 Copyright (c) 2006, Portugal Telecom C<< http://www.sapo.pt/ >>. All rights reserved.
310 Portions copyright 2007, Joel Bernstein C<< <rataxis@cpan.org> >>.
312 This module is free software; you can redistribute it and/or
313 modify it under the same terms as Perl itself. See L<perlartistic>.
315 =head1 DISCLAIMER OF WARRANTY
317 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
318 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
319 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
320 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
321 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
322 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
323 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
324 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
325 NECESSARY SERVICING, REPAIR, OR CORRECTION.
327 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
328 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
329 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
330 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
331 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
332 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
333 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
334 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
335 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
340 L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader>
341 -- now a wrapper around this module.