From: Brian Cassidy Date: Thu, 8 Nov 2007 13:54:07 +0000 (+0000) Subject: perltidy X-Git-Tag: v0.09_01~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FConfig-Any.git;a=commitdiff_plain;h=92a04e78451078b33f75e7c44d247b024c27b4f7 perltidy --- diff --git a/Makefile.PL b/Makefile.PL index ac459ef..88d7dd3 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -1,6 +1,6 @@ use inc::Module::Install 0.68; -if( -e 'MANIFEST.SKIP' ) { +if ( -e 'MANIFEST.SKIP' ) { system( 'pod2text lib/Config/Any.pm > README' ); } diff --git a/lib/Config/Any.pm b/lib/Config/Any.pm index bc54d47..031a0e2 100644 --- a/lib/Config/Any.pm +++ b/lib/Config/Any.pm @@ -1,7 +1,8 @@ package Config::Any; -# $Id: $ -use warnings; + use strict; +use warnings; + use Carp; use Module::Pluggable::Object (); use English qw(-no_match_vars); @@ -84,17 +85,18 @@ parser object. Example: =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( ) @@ -113,30 +115,31 @@ parameters. Please read the C documentation before using this meth =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; @@ -146,43 +149,46 @@ sub _stems_to_files { \@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; } @@ -190,13 +196,11 @@ sub _load { } 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; @@ -217,7 +221,7 @@ more information. =cut sub finder { - my $class = shift; + my $class = shift; my $finder = Module::Pluggable::Object->new( search_path => [ __PACKAGE__ ], require => 1 @@ -248,7 +252,7 @@ parameter to those methods. sub extensions { my $class = shift; my @ext = map { $_->extensions } $class->plugins; - return wantarray ? @ext : [@ext]; + return wantarray ? @ext : [ @ext ]; } =head1 DIAGNOSTICS diff --git a/lib/Config/Any/General.pm b/lib/Config/Any/General.pm index 975bff6..0b39af2 100644 --- a/lib/Config/Any/General.pm +++ b/lib/Config/Any/General.pm @@ -43,14 +43,14 @@ sub load { 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; } @@ -61,10 +61,10 @@ sub load { # 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; } diff --git a/lib/Config/Any/INI.pm b/lib/Config/Any/INI.pm index a8ec52f..ea2abce 100644 --- a/lib/Config/Any/INI.pm +++ b/lib/Config/Any/INI.pm @@ -46,19 +46,20 @@ sub load { 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; diff --git a/lib/Config/Any/JSON.pm b/lib/Config/Any/JSON.pm index 922bb61..5e4edf3 100644 --- a/lib/Config/Any/JSON.pm +++ b/lib/Config/Any/JSON.pm @@ -48,7 +48,7 @@ sub load { close $fh; eval { require JSON::Syck; }; - if( $@ ) { + if ( $@ ) { require JSON; return JSON::jsonToObj( $content ); } diff --git a/lib/Config/Any/Perl.pm b/lib/Config/Any/Perl.pm index 1dd1499..5d8e0a5 100644 --- a/lib/Config/Any/Perl.pm +++ b/lib/Config/Any/Perl.pm @@ -46,7 +46,7 @@ sub load { my $file = shift; my $content; - unless( $content = $cache{ $file } ) { + unless ( $content = $cache{ $file } ) { $content = eval { require $file }; $cache{ $file } = $content; } diff --git a/lib/Config/Any/XML.pm b/lib/Config/Any/XML.pm index d5221c5..a5fc881 100644 --- a/lib/Config/Any/XML.pm +++ b/lib/Config/Any/XML.pm @@ -45,27 +45,29 @@ sub load { 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; diff --git a/lib/Config/Any/YAML.pm b/lib/Config/Any/YAML.pm index f5d680d..46e4d1e 100644 --- a/lib/Config/Any/YAML.pm +++ b/lib/Config/Any/YAML.pm @@ -42,7 +42,7 @@ sub load { my $file = shift; eval { require YAML::Syck; }; - if( $@ ) { + if ( $@ ) { require YAML; return YAML::LoadFile( $file ); } diff --git a/t/01-use.t b/t/01-use.t index 5e46259..5b8dc61 100644 --- a/t/01-use.t +++ b/t/01-use.t @@ -1,6 +1,6 @@ use Test::More tests => 6; -BEGIN { +BEGIN { use_ok( 'Config::Any' ); use_ok( 'Config::Any::INI' ); use_ok( 'Config::Any::JSON' ); diff --git a/t/10-branches.t b/t/10-branches.t index e74294b..adda42d 100644 --- a/t/10-branches.t +++ b/t/10-branches.t @@ -1,26 +1,48 @@ 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" ); diff --git a/t/20-parse.t b/t/20-parse.t index b75eab7..47d38e7 100644 --- a/t/20-parse.t +++ b/t/20-parse.t @@ -13,7 +13,6 @@ use Config::Any::Perl; use Config::Any::XML; use Config::Any::YAML; - our %ext_map = ( conf => 'Config::Any::General', ini => 'Config::Any::INI', @@ -27,37 +26,43 @@ sub load_parser_for { 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" ); } } diff --git a/t/50-general.t b/t/50-general.t index 34cc74c..e684bf2 100644 --- a/t/50-general.t +++ b/t/50-general.t @@ -10,7 +10,10 @@ SKIP: { 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 } ); } diff --git a/t/51-ini.t b/t/51-ini.t index dbeadc5..c7d6036 100644 --- a/t/51-ini.t +++ b/t/51-ini.t @@ -2,18 +2,20 @@ use Test::More tests => 9; 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; @@ -21,6 +23,8 @@ my $unspaced_config = eval { Config::Any::INI->load( 't/conf/conf.ini' ); }; 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" ); } diff --git a/t/61-features.t b/t/61-features.t index c3512aa..c1d983d 100644 --- a/t/61-features.t +++ b/t/61-features.t @@ -12,40 +12,43 @@ use Config::Any::INI; 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" + ); } - - diff --git a/t/conf/conf.pl b/t/conf/conf.pl index 3e243b1..be11d59 100644 --- a/t/conf/conf.pl +++ b/t/conf/conf.pl @@ -1,13 +1,4 @@ -{ - name => 'TestApp', - Component => { - 'Controller::Foo' => { - foo => 'bar' - } - }, - Model => { - 'Model::Baz' => { - qux => 'xyzzy' - } - } +{ name => 'TestApp', + Component => { 'Controller::Foo' => { foo => 'bar' } }, + Model => { 'Model::Baz' => { qux => 'xyzzy' } } } diff --git a/t/pod-coverage.t b/t/pod-coverage.t index 703f91d..2ee90fa 100644 --- a/t/pod-coverage.t +++ b/t/pod-coverage.t @@ -2,5 +2,6 @@ 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();