From: Brian Cassidy Date: Tue, 13 Nov 2007 15:40:44 +0000 (+0000) Subject: added is_supported() to see what plugins we can use X-Git-Tag: v0.09_01~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=72628dc786ef43d546023d6f17a86c3f5edeb21a;p=p5sagit%2FConfig-Any.git added is_supported() to see what plugins we can use --- diff --git a/Changes b/Changes index 1b5e0e9..840e1c6 100644 --- a/Changes +++ b/Changes @@ -6,7 +6,9 @@ Revision history for Config-Any - when use_ext is on, if a parser throws an error, we throw an error - fix case where use_ext is defined and false, but was behaving like use_ext => 1 - - allow loaders to return multiple documents as an array. + - allow loaders to return multiple documents as an array + - each plugin now has an is_supported() which helps us figure out if + the right modules are available 0.08 Thu Aug 23 2007 - pass config options to each parser diff --git a/lib/Config/Any.pm b/lib/Config/Any.pm index 708936a..022fcd1 100644 --- a/lib/Config/Any.pm +++ b/lib/Config/Any.pm @@ -133,19 +133,21 @@ sub _load { my ( $class, $args ) = @_; croak "_load requires a arrayref of file paths" unless $args->{ files }; - if( !defined $args->{ use_ext } ) { - warn "use_ext argument was not explicitly set, as of 0.09, this is true by default"; + if ( !defined $args->{ use_ext } ) { + warn + "use_ext argument was not explicitly set, as of 0.09, this is true by default"; $args->{ use_ext } = 1; } # figure out what plugins we're using - my $force = defined $args->{ force_plugins }; - my @plugins = $force ? @{ $args->{ force_plugins } } : $class->plugins; + my $force = defined $args->{ force_plugins }; + my @plugins = grep { $_->is_supported } + ( $force ? @{ $args->{ force_plugins } } : $class->plugins ); # map extensions if we have to - my( %extension_lut, $extension_re ); + my ( %extension_lut, $extension_re ); my $use_ext_lut = !$force && $args->{ use_ext }; - if( $use_ext_lut ) { + if ( $use_ext_lut ) { for my $plugin ( @plugins ) { $extension_lut{ $_ } = $plugin for $plugin->extensions; } @@ -164,19 +166,21 @@ sub _load { my @results; for my $filename ( @{ $args->{ files } } ) { + # don't even bother if it's not there next unless -f $filename; my @try_plugins = @plugins; - if( $use_ext_lut ) { + if ( $use_ext_lut ) { $filename =~ m{\.($extension_re)\z}; next unless $1; @try_plugins = $extension_lut{ $1 }; } for my $loader ( @try_plugins ) { - my @configs = eval { $loader->load( $filename, $loader_args{ $loader } ); }; + my @configs + = eval { $loader->load( $filename, $loader_args{ $loader } ); }; # fatal error if we used extension matching croak "Error parsing file: $filename" if $@ and $use_ext_lut; @@ -187,7 +191,8 @@ sub _load { $args->{ filter }->( $_ ) for @configs; } - push @results, { $filename => @configs == 1 ? $configs[ 0 ] : \@configs }; + push @results, + { $filename => @configs == 1 ? $configs[ 0 ] : \@configs }; last; } } diff --git a/lib/Config/Any/General.pm b/lib/Config/Any/General.pm index bae5fff..6aceb98 100644 --- a/lib/Config/Any/General.pm +++ b/lib/Config/Any/General.pm @@ -68,6 +68,17 @@ sub _test_perl { return defined $is_perl_src; } +=head2 is_supported( ) + +Returns true if L is available. + +=cut + +sub is_supported { + eval { require Config::General; }; + return $@ ? 0 : 1; +} + =head1 AUTHOR Brian Cassidy Ebricas@cpan.orgE diff --git a/lib/Config/Any/INI.pm b/lib/Config/Any/INI.pm index 1d0575e..d281e60 100644 --- a/lib/Config/Any/INI.pm +++ b/lib/Config/Any/INI.pm @@ -65,6 +65,17 @@ sub load { return $out; } +=head2 is_supported( ) + +Returns true if L is available. + +=cut + +sub is_supported { + eval { require Config::Tiny; }; + return $@ ? 0 : 1; +} + =head1 PACKAGE VARIABLES =over 4 diff --git a/lib/Config/Any/JSON.pm b/lib/Config/Any/JSON.pm index 18b201f..3213128 100644 --- a/lib/Config/Any/JSON.pm +++ b/lib/Config/Any/JSON.pm @@ -57,6 +57,19 @@ sub load { } } +=head2 is_supported( ) + +Returns true if either L or L is available. + +=cut + +sub is_supported { + eval { require JSON::Syck; }; + return 1 unless $@; + eval { require JSON; }; + return $@ ? 0 : 1; +} + =head1 AUTHOR Brian Cassidy Ebricas@cpan.orgE diff --git a/lib/Config/Any/Perl.pm b/lib/Config/Any/Perl.pm index ed3752b..6a2e330 100644 --- a/lib/Config/Any/Perl.pm +++ b/lib/Config/Any/Perl.pm @@ -54,6 +54,16 @@ sub load { return $content; } +=head2 is_supported( ) + +Returns true. + +=cut + +sub is_supported { + return 1; +} + =head1 AUTHOR Brian Cassidy Ebricas@cpan.orgE diff --git a/lib/Config/Any/XML.pm b/lib/Config/Any/XML.pm index 9c49443..c9b979f 100644 --- a/lib/Config/Any/XML.pm +++ b/lib/Config/Any/XML.pm @@ -73,6 +73,17 @@ sub _coerce { $out; } +=head2 is_supported( ) + +Returns true if L is available. + +=cut + +sub is_supported { + eval { require XML::Simple; }; + return $@ ? 0 : 1; +} + =head1 AUTHORS Brian Cassidy Ebricas@cpan.orgE diff --git a/lib/Config/Any/YAML.pm b/lib/Config/Any/YAML.pm index 594fecb..4a4b965 100644 --- a/lib/Config/Any/YAML.pm +++ b/lib/Config/Any/YAML.pm @@ -54,6 +54,19 @@ sub load { } } +=head2 is_supported( ) + +Returns true if either L or L is available. + +=cut + +sub is_supported { + eval { require YAML::Syck; }; + return 1 unless $@; + eval { require YAML; }; + return $@ ? 0 : 1; +} + =head1 AUTHOR Brian Cassidy Ebricas@cpan.orgE diff --git a/t/10-branches.t b/t/10-branches.t index b70d726..24c6209 100644 --- a/t/10-branches.t +++ b/t/10-branches.t @@ -6,7 +6,7 @@ use_ok( 'Config::Any' ); my @warnings; local $SIG{ __WARN__ } = sub { push @warnings, @_ }; - Config::Any->load_files( ); + Config::Any->load_files(); like( shift @warnings, qr/^No files specified!/, @@ -20,7 +20,7 @@ use_ok( 'Config::Any' ); "load_files expects files" ); - Config::Any->load_stems( ); + Config::Any->load_stems(); like( shift @warnings, qr/^No stems specified!/, diff --git a/t/62-multi.t b/t/62-multi.t index b8b9d7e..6c9e1e9 100644 --- a/t/62-multi.t +++ b/t/62-multi.t @@ -23,6 +23,11 @@ SKIP: { is( @results, 2, '2 documents' ); is_deeply( \@results, \@expect, 'structures ok' ); - my $return = Config::Any->load_files( { use_ext => 1, files => [ $file ] } ); - is_deeply( $return, [ { $file => \@expect } ], 'config-any structures ok' ); + my $return + = Config::Any->load_files( { use_ext => 1, files => [ $file ] } ); + is_deeply( + $return, + [ { $file => \@expect } ], + 'config-any structures ok' + ); }