use inc::Module::Install 0.68;
-if( -e 'MANIFEST.SKIP' ) {
+if ( -e 'MANIFEST.SKIP' ) {
system( 'pod2text lib/Config/Any.pm > README' );
}
package Config::Any;
-# $Id: $
-use warnings;
+
use strict;
+use warnings;
+
use Carp;
use Module::Pluggable::Object ();
use English qw(-no_match_vars);
=cut
sub load_files {
- my ($class, $args) = @_;
+ my ( $class, $args ) = @_;
return unless defined $args;
- unless (exists $args->{files}) {
+ unless ( exists $args->{ files } ) {
warn "no files specified";
return;
}
- my %load_args = map { $_ => defined $args->{$_} ? $args->{$_} : undef }
+ my %load_args
+ = map { $_ => defined $args->{ $_ } ? $args->{ $_ } : undef }
qw(filter use_ext force_plugins driver_args);
- $load_args{files} = [ grep { -f $_ } @{$args->{files}} ];
- return $class->_load(\%load_args);
+ $load_args{ files } = [ grep { -f $_ } @{ $args->{ files } } ];
+ return $class->_load( \%load_args );
}
=head2 load_stems( )
=cut
sub load_stems {
- my ($class, $args) = @_;
+ my ( $class, $args ) = @_;
return unless defined $args;
- unless (exists $args->{stems}) {
+ unless ( exists $args->{ stems } ) {
warn "no stems specified";
return;
}
-
- my %load_args = map { $_ => defined $args->{$_} ? $args->{$_} : undef }
+
+ my %load_args
+ = map { $_ => defined $args->{ $_ } ? $args->{ $_ } : undef }
qw(filter use_ext force_plugins driver_args);
- my $filenames = $class->_stems_to_files($args->{stems});
- $load_args{files} = [ grep { -f $_ } @{$filenames} ];
- return $class->_load(\%load_args);
+ my $filenames = $class->_stems_to_files( $args->{ stems } );
+ $load_args{ files } = [ grep { -f $_ } @{ $filenames } ];
+ return $class->_load( \%load_args );
}
sub _stems_to_files {
- my ($class, $stems) = @_;
+ my ( $class, $stems ) = @_;
return unless defined $stems;
my @files;
- STEM:
- for my $s (@$stems) {
- EXT:
- for my $ext ($class->extensions) {
+STEM:
+ for my $s ( @$stems ) {
+ EXT:
+ for my $ext ( $class->extensions ) {
my $file = "$s.$ext";
next EXT unless -f $file;
push @files, $file;
\@files;
}
-sub _maphash (@) { map { $_ => 1 } @_ } # sugar
+sub _maphash (@) {
+ map { $_ => 1 } @_;
+} # sugar
# this is where we do the real work
# it's a private class-method because users should use the interface described
# in the POD.
sub _load {
- my ($class, $args) = @_;
- my ($files_ref, $filter_cb, $use_ext, $force_plugins_ref) =
- @{$args}{qw(files filter use_ext force_plugins)};
+ my ( $class, $args ) = @_;
+ my ( $files_ref, $filter_cb, $use_ext, $force_plugins_ref )
+ = @{ $args }{ qw(files filter use_ext force_plugins) };
croak "_load requires a arrayref of file paths" unless defined $files_ref;
- my %files = _maphash @$files_ref;
- my %force_plugins = _maphash @$force_plugins_ref;
- my $enforcing = keys %force_plugins ? 1 : 0;
+ my %files = _maphash @$files_ref;
+ my %force_plugins = _maphash @$force_plugins_ref;
+ my $enforcing = keys %force_plugins ? 1 : 0;
- my $final_configs = [];
- my $originally_loaded = {};
+ my $final_configs = [];
+ my $originally_loaded = {};
# perform a separate file loop for each loader
for my $loader ( $class->plugins ) {
- next if $enforcing && not defined $force_plugins{$loader};
+ next if $enforcing && not defined $force_plugins{ $loader };
last unless keys %files;
my %ext = _maphash $loader->extensions;
- my ($loader_class) = $loader =~ /::([^:]+)$/;
- my $driver_args = $args->{driver_args}{$loader_class} || {};
-
- FILE:
- for my $filename (keys %files) {
- # use file extension to decide whether this loader should try this file
- # use_ext => 1 hits this block
- if (defined $use_ext && !$enforcing) {
+ my ( $loader_class ) = $loader =~ /::([^:]+)$/;
+ my $driver_args = $args->{ driver_args }{ $loader_class } || {};
+
+ FILE:
+ for my $filename ( keys %files ) {
+
+ # use file extension to decide whether this loader should try this file
+ # use_ext => 1 hits this block
+ if ( defined $use_ext && !$enforcing ) {
my $matched_ext = 0;
- EXT:
- for my $e (keys %ext) {
- next EXT unless $filename =~ m{ \. $e \z }xms;
- next FILE unless exists $ext{$e};
+ EXT:
+ for my $e ( keys %ext ) {
+ next EXT unless $filename =~ m{ \. $e \z }xms;
+ next FILE unless exists $ext{ $e };
$matched_ext = 1;
}
}
my $config;
- eval {
- $config = $loader->load( $filename, $driver_args );
- };
+ eval { $config = $loader->load( $filename, $driver_args ); };
- next if $EVAL_ERROR; # if it croaked or warned, we can't use it
+ next if $EVAL_ERROR; # if it croaked or warned, we can't use it
next if !$config;
- delete $files{$filename};
+ delete $files{ $filename };
# post-process config with a filter callback, if we got one
$filter_cb->( $config ) if defined $filter_cb;
=cut
sub finder {
- my $class = shift;
+ my $class = shift;
my $finder = Module::Pluggable::Object->new(
search_path => [ __PACKAGE__ ],
require => 1
sub extensions {
my $class = shift;
my @ext = map { $_->extensions } $class->plugins;
- return wantarray ? @ext : [@ext];
+ return wantarray ? @ext : [ @ext ];
}
=head1 DIAGNOSTICS
my $args = shift || {};
# work around bug (?) in Config::General
-# return if $class->_test_perl($file);
+ # return if $class->_test_perl($file);
- $args->{-ConfigFile} = $file;
+ $args->{ -ConfigFile } = $file;
require Config::General;
my $configfile = Config::General->new( %$args );
my $config = { $configfile->getall };
-
+
return $config;
}
# developer.
sub _test_perl {
- my ($class, $file) = @_;
+ my ( $class, $file ) = @_;
my $is_perl_src;
eval { $is_perl_src = do "$file"; };
- delete $INC{$file}; # so we don't screw stuff later on
+ delete $INC{ $file }; # so we don't screw stuff later on
return defined $is_perl_src;
}
require Config::Tiny;
my $config = Config::Tiny->read( $file );
- my $main = delete $config->{ _ };
+ my $main = delete $config->{ _ };
my $out;
- $out->{$_} = $main->{$_} for keys %$main;
+ $out->{ $_ } = $main->{ $_ } for keys %$main;
- for my $k (keys %$config) {
+ for my $k ( keys %$config ) {
my @keys = split /\s+/, $k if $MAP_SECTION_SPACE_TO_NESTED_KEY;
- my $ref = $config->{$k};
+ my $ref = $config->{ $k };
- if (@keys > 1) {
- my ($a, $b) = @keys[0,1];
- $out->{$a}->{$b} = $ref;
- } else {
- $out->{$k} = $ref;
+ if ( @keys > 1 ) {
+ my ( $a, $b ) = @keys[ 0, 1 ];
+ $out->{ $a }->{ $b } = $ref;
+ }
+ else {
+ $out->{ $k } = $ref;
}
}
return $out;
close $fh;
eval { require JSON::Syck; };
- if( $@ ) {
+ if ( $@ ) {
require JSON;
return JSON::jsonToObj( $content );
}
my $file = shift;
my $content;
- unless( $content = $cache{ $file } ) {
+ unless ( $content = $cache{ $file } ) {
$content = eval { require $file };
$cache{ $file } = $content;
}
my $args = shift || {};
require XML::Simple;
- my $config = XML::Simple::XMLin(
- $file,
+ my $config = XML::Simple::XMLin(
+ $file,
ForceArray => [ qw( component model view controller ) ],
%$args
);
- return $class->_coerce($config);
+ return $class->_coerce( $config );
}
sub _coerce {
+
# coerce the XML-parsed config into the correct format
- my $class = shift;
+ my $class = shift;
my $config = shift;
my $out;
- for my $k (keys %$config) {
- my $ref = $config->{$k};
- my $name = ref $ref ? delete $ref->{name} : undef;
- if (defined $name) {
- $out->{$k}->{$name} = $ref;
- } else {
- $out->{$k} = $ref;
+ for my $k ( keys %$config ) {
+ my $ref = $config->{ $k };
+ my $name = ref $ref ? delete $ref->{ name } : undef;
+ if ( defined $name ) {
+ $out->{ $k }->{ $name } = $ref;
+ }
+ else {
+ $out->{ $k } = $ref;
}
}
$out;
my $file = shift;
eval { require YAML::Syck; };
- if( $@ ) {
+ if ( $@ ) {
require YAML;
return YAML::LoadFile( $file );
}
use Test::More tests => 6;
-BEGIN {
+BEGIN {
use_ok( 'Config::Any' );
use_ok( 'Config::Any::INI' );
use_ok( 'Config::Any::JSON' );
use Test::More tests => 9;
use Config::Any;
-ok ( ! Config::Any->load_files(), "load_files expects args" );
-ok ( ! Config::Any->load_stems(), "load_stems expects args" );
+ok( !Config::Any->load_files(), "load_files expects args" );
+ok( !Config::Any->load_stems(), "load_stems expects args" );
{
my @warnings;
- local $SIG{__WARN__} = sub { push @warnings, @_ };
- Config::Any->load_files({});
- like (shift @warnings, qr/^no files specified/, "load_files expects files");
- Config::Any->load_stems({});
- like (shift @warnings, qr/^no stems specified/, "load_stems expects stems");
+ local $SIG{ __WARN__ } = sub { push @warnings, @_ };
+ Config::Any->load_files( {} );
+ like(
+ shift @warnings,
+ qr/^no files specified/,
+ "load_files expects files"
+ );
+ Config::Any->load_stems( {} );
+ like(
+ shift @warnings,
+ qr/^no stems specified/,
+ "load_stems expects stems"
+ );
}
-my @files = glob("t/conf/conf.*");
+my @files = glob( "t/conf/conf.*" );
my $filter = sub { return };
-ok(Config::Any->load_files({files=>\@files, use_ext=>0}), "use_ext 0 works");
-ok(Config::Any->load_files({files=>\@files, use_ext=>1}), "use_ext 1 works");
+ok( Config::Any->load_files( { files => \@files, use_ext => 0 } ),
+ "use_ext 0 works" );
+ok( Config::Any->load_files( { files => \@files, use_ext => 1 } ),
+ "use_ext 1 works" );
-ok(Config::Any->load_files({files=>\@files, use_ext=>1, filter=>\&$filter}), "filter works");
-eval {Config::Any->load_files({files=>\@files, use_ext=>1, filter=>sub{die}}) };
-ok($@, "filter breaks");
+ok( Config::Any->load_files(
+ { files => \@files, use_ext => 1, filter => \&$filter }
+ ),
+ "filter works"
+);
+eval {
+ Config::Any->load_files(
+ { files => \@files,
+ use_ext => 1,
+ filter => sub { die }
+ }
+ );
+};
+ok( $@, "filter breaks" );
my @stems = qw(t/conf/conf);
-ok(Config::Any->load_stems({stems=>\@stems, use_ext=>1}), "load_stems with stems works");
+ok( Config::Any->load_stems( { stems => \@stems, use_ext => 1 } ),
+ "load_stems with stems works" );
use Config::Any::XML;
use Config::Any::YAML;
-
our %ext_map = (
conf => 'Config::Any::General',
ini => 'Config::Any::INI',
my $f = shift;
return unless $f;
- my ($ext) = $f =~ m{ \. ( [^\.]+ ) \z }xms;
- my $mod = $ext_map{$ext};
+ my ( $ext ) = $f =~ m{ \. ( [^\.]+ ) \z }xms;
+ my $mod = $ext_map{ $ext };
my $mod_load_result;
- eval { $mod_load_result = $mod->load( $f ); delete $INC{$f} if $ext eq 'pl' };
- return $@ ? (1,$mod) : (0,$mod);
+ eval {
+ $mod_load_result = $mod->load( $f );
+ delete $INC{ $f } if $ext eq 'pl';
+ };
+ return $@ ? ( 1, $mod ) : ( 0, $mod );
}
-for my $f (map { "t/conf/conf.$_" } keys %ext_map) {
- my ($skip,$mod) = load_parser_for($f);
- SKIP: {
+for my $f ( map { "t/conf/conf.$_" } keys %ext_map ) {
+ my ( $skip, $mod ) = load_parser_for( $f );
+SKIP: {
skip "File loading backend for $mod not found", 9 if $skip;
-
- ok(my $c_arr = Config::Any->load_files({files=>[$f], use_ext=>1}),
- "load_files with use_ext works [$f]");
- ok(my $c = $c_arr->[0], "load_files returns an arrayref");
-
- ok(ref $c, "load_files arrayref contains a ref");
+
+ ok( my $c_arr
+ = Config::Any->load_files(
+ { files => [ $f ], use_ext => 1 } ),
+ "load_files with use_ext works [$f]"
+ );
+ ok( my $c = $c_arr->[ 0 ], "load_files returns an arrayref" );
+
+ ok( ref $c, "load_files arrayref contains a ref" );
my $ref = blessed $c ? reftype $c : ref $c;
- is(substr($ref,0,4), "HASH", "hashref");
+ is( substr( $ref, 0, 4 ), "HASH", "hashref" );
+
+ my ( $name, $cfg ) = each %$c;
+ is( $name, $f, "filename matches" );
- my ($name, $cfg) = each %$c;
- is($name, $f, "filename matches");
-
my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg;
- is(substr($cfgref,0,4), "HASH", "hashref cfg");
+ is( substr( $cfgref, 0, 4 ), "HASH", "hashref cfg" );
- is( $cfg->{name}, 'TestApp', "appname parses" );
- is( $cfg->{Component}{ "Controller::Foo" }->{ foo }, 'bar',
- "component->cntrlr->foo = bar" );
- is( $cfg->{Model}{ "Model::Baz" }->{ qux }, 'xyzzy',
- "model->model::baz->qux = xyzzy" );
+ is( $cfg->{ name }, 'TestApp', "appname parses" );
+ is( $cfg->{ Component }{ "Controller::Foo" }->{ foo },
+ 'bar', "component->cntrlr->foo = bar" );
+ is( $cfg->{ Model }{ "Model::Baz" }->{ qux },
+ 'xyzzy', "model->model::baz->qux = xyzzy" );
}
}
is( $config->{ name }, 'TestApp' );
ok( exists $config->{ Component } );
- $config = eval { Config::Any::General->load( 't/conf/conf.conf', { -LowerCaseNames => 1 } ) };
+ $config = eval {
+ Config::Any::General->load( 't/conf/conf.conf',
+ { -LowerCaseNames => 1 } );
+ };
ok( exists $config->{ component } );
}
use Config::Any::INI;
-my $config = eval { Config::Any::INI->load( 't/conf/conf.ini' ) };
+my $config = eval { Config::Any::INI->load( 't/conf/conf.ini' ) };
my $simpleconfig = eval { Config::Any::INI->load( 't/conf/conf2.ini' ) };
SKIP: {
skip "Couldn't Load INI plugin", 6 if $@;
ok( $config, "loaded INI config #1" );
is( $config->{ name }, 'TestApp', "toplevel key lookup succeeded" );
- is( $config->{Component}->{Controller::Foo}->{foo}, 'bar', "nested hashref hack lookup succeeded");
-
+ is( $config->{ Component }->{ Controller::Foo }->{ foo },
+ 'bar', "nested hashref hack lookup succeeded" );
+
ok( $simpleconfig, "loaded INI config #1" );
is( $simpleconfig->{ name }, 'TestApp', "toplevel key lookup succeeded" );
- is( $simpleconfig->{Controller::Foo}->{foo}, 'bar', "nested hashref hack lookup succeeded" );
+ is( $simpleconfig->{ Controller::Foo }->{ foo },
+ 'bar', "nested hashref hack lookup succeeded" );
}
$Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY = 0;
SKIP: {
skip "Couldn't load INI plugin", 3 if $@;
ok( $unspaced_config, "loaded INI config #1 in no-map-space mode" );
- is( $unspaced_config->{name}, 'TestApp', "toplevel key lookup succeeded" );
- is( $unspaced_config->{'Component Controller::Foo'}->{foo}, 'bar', "unnested key lookup succeeded");
+ is( $unspaced_config->{ name },
+ 'TestApp', "toplevel key lookup succeeded" );
+ is( $unspaced_config->{ 'Component Controller::Foo' }->{ foo },
+ 'bar', "unnested key lookup succeeded" );
}
our $cfg_file = 't/conf/conf.foo';
-eval { Config::Any::INI->load($cfg_file); };
+eval { Config::Any::INI->load( $cfg_file ); };
SKIP: {
skip "File loading backend for INI not found", 10 if $@;
- ok( my $c_arr = Config::Any->load_files({
- files => [ $cfg_file ],
- force_plugins => [qw(Config::Any::INI)]
- }), "load file with parser forced" );
+ ok( my $c_arr = Config::Any->load_files(
+ { files => [ $cfg_file ],
+ force_plugins => [ qw(Config::Any::INI) ]
+ }
+ ),
+ "load file with parser forced"
+ );
- ok(my $c = $c_arr->[0], "load_files returns an arrayref");
-
- ok(ref $c, "load_files arrayref contains a ref");
- my $ref = blessed $c ? reftype $c : ref $c;
- is(substr($ref,0,4), "HASH", "hashref");
-
- my ($name, $cfg) = each %$c;
- is($name, $cfg_file, "filename matches");
-
- my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg;
- is(substr($cfgref,0,4), "HASH", "hashref cfg");
+ ok( my $c = $c_arr->[ 0 ], "load_files returns an arrayref" );
- is( $cfg->{name}, 'TestApp', "appname parses" );
- is( $cfg->{Component}{ "Controller::Foo" }->{ foo }, 'bar',
- "component->cntrlr->foo = bar" );
- is( $cfg->{Model}{ "Model::Baz" }->{ qux }, 'xyzzy',
- "model->model::baz->qux = xyzzy" );
+ ok( ref $c, "load_files arrayref contains a ref" );
+ my $ref = blessed $c ? reftype $c : ref $c;
+ is( substr( $ref, 0, 4 ), "HASH", "hashref" );
+ my ( $name, $cfg ) = each %$c;
+ is( $name, $cfg_file, "filename matches" );
- ok( my $c_arr_2 = Config::Any->load_files({
- files => [ $cfg_file ],
- force_plugins => [qw(Config::Any::INI)],
- use_ext => 1
- }), "load file with parser forced" );
+ my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg;
+ is( substr( $cfgref, 0, 4 ), "HASH", "hashref cfg" );
+
+ is( $cfg->{ name }, 'TestApp', "appname parses" );
+ is( $cfg->{ Component }{ "Controller::Foo" }->{ foo },
+ 'bar', "component->cntrlr->foo = bar" );
+ is( $cfg->{ Model }{ "Model::Baz" }->{ qux },
+ 'xyzzy', "model->model::baz->qux = xyzzy" );
+
+ ok( my $c_arr_2 = Config::Any->load_files(
+ { files => [ $cfg_file ],
+ force_plugins => [ qw(Config::Any::INI) ],
+ use_ext => 1
+ }
+ ),
+ "load file with parser forced"
+ );
}
-
-
-{
- name => 'TestApp',
- Component => {
- 'Controller::Foo' => {
- foo => 'bar'
- }
- },
- Model => {
- 'Model::Baz' => {
- qux => 'xyzzy'
- }
- }
+{ name => 'TestApp',
+ Component => { 'Controller::Foo' => { foo => 'bar' } },
+ Model => { 'Model::Baz' => { qux => 'xyzzy' } }
}
use Test::More;
eval "use Test::Pod::Coverage 1.04";
-plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@;
+plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage"
+ if $@;
all_pod_coverage_ok();