From: Rafael Kitover Date: Mon, 15 Feb 2010 12:17:50 +0000 (+0000) Subject: make perl loader check mtime for cache, prefer JSON::DWIW for JSON X-Git-Tag: v0.19~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=49ae658349471834b403cb322f11f97258fcd0ca;p=p5sagit%2FConfig-Any.git make perl loader check mtime for cache, prefer JSON::DWIW for JSON --- diff --git a/lib/Config/Any/JSON.pm b/lib/Config/Any/JSON.pm index ce29549..4200d44 100644 --- a/lib/Config/Any/JSON.pm +++ b/lib/Config/Any/JSON.pm @@ -49,9 +49,18 @@ sub load { my $content = do { local $/; <$fh> }; close $fh; + eval { require JSON::DWIW; }; + unless( $@ ) { + my $decoder = JSON::DWIW->new; + my ( $data, $error ) = $decoder->from_json( $content ); + die $error if $error; + return $data; + } + eval { require JSON::XS; }; unless( $@ ) { - return JSON::XS::decode_json( $content ); + my $decoder = JSON::XS->new->relaxed; + return $decoder->decode( $content ); } eval { require JSON::Syck; }; @@ -66,12 +75,12 @@ sub load { =head2 requires_any_of( ) -Specifies that this modules requires one of, L, L or -L in order to work. +Specifies that this modules requires one of, L, L, +L or L in order to work. =cut -sub requires_any_of { 'JSON::XS', 'JSON::Syck', 'JSON' } +sub requires_any_of { 'JSON::DWIW', 'JSON::XS', 'JSON::Syck', 'JSON' } =head1 AUTHOR @@ -92,11 +101,13 @@ it under the same terms as Perl itself. =item * L -=item * L +=item * L + +=item * L =item * L -=item * L +=item * L =back diff --git a/lib/Config/Any/Perl.pm b/lib/Config/Any/Perl.pm index fee9e4d..daf27f6 100644 --- a/lib/Config/Any/Perl.pm +++ b/lib/Config/Any/Perl.pm @@ -48,12 +48,22 @@ sub load { my $file = shift; my $content; - unless ( $content = $cache{ $file } ) { - $content = require $file; - $cache{ $file } = $content; + my $mtime = (stat($file))[9]; + + if ( (not exists $cache{ $file }) || $cache{ $file }{ mtime } < $mtime ) { + my $exception; + { + local $@; + $content = do $file; + $exception = $@; + } + die $exception if $exception; + + $cache{ $file }{ mtime } = $mtime; + $cache{ $file }{ content } = $content; } - return $content; + return $cache{ $file }{ content }; } =head1 AUTHOR