From: Karen Etheridge Date: Fri, 9 Aug 2013 19:18:57 +0000 (-0700) Subject: delay loading all optional prereqs until runtime X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Ftopic%2Fload_options_at_runtime;p=gitmo%2FMooseX-Storage.git delay loading all optional prereqs until runtime --- diff --git a/dist.ini b/dist.ini index add72b3..4771d3f 100644 --- a/dist.ini +++ b/dist.ini @@ -17,9 +17,6 @@ AutoMetaResources.repository.gitmo = 1 Test::MinimumVersion.max_target_perl = 5.008000 ; these appear in every file, so it's easier to just put it here Test::PodSpelling.stopwords = cpan -; these are all the modules that use optional prereqs, and will cause -; cpantesters to barf in the compile tests -Test::Compile.skip[] = ^MooseX::Storage::((Engine::)?IO::|Format::) [Prereqs / RuntimeRequires] Moose = 0.99 diff --git a/lib/MooseX/Storage/Engine/IO/AtomicFile.pm b/lib/MooseX/Storage/Engine/IO/AtomicFile.pm index c84c366..e48cabd 100644 --- a/lib/MooseX/Storage/Engine/IO/AtomicFile.pm +++ b/lib/MooseX/Storage/Engine/IO/AtomicFile.pm @@ -1,12 +1,11 @@ package MooseX::Storage::Engine::IO::AtomicFile; use Moose; -use IO::AtomicFile; - extends 'MooseX::Storage::Engine::IO::File'; sub store { my ($self, $data) = @_; + require IO::AtomicFile; my $fh = IO::AtomicFile->new($self->file, 'w') || confess "Unable to open file (" . $self->file . ") for storing : $!"; $fh->binmode(':utf8') if utf8::is_utf8($data); diff --git a/lib/MooseX/Storage/Engine/IO/File.pm b/lib/MooseX/Storage/Engine/IO/File.pm index e080d4c..7756ac3 100644 --- a/lib/MooseX/Storage/Engine/IO/File.pm +++ b/lib/MooseX/Storage/Engine/IO/File.pm @@ -1,8 +1,6 @@ package MooseX::Storage::Engine::IO::File; use Moose; -use IO::File; - has 'file' => ( is => 'ro', isa => 'Str', @@ -11,6 +9,8 @@ has 'file' => ( sub load { my ($self) = @_; + + require IO::File; my $fh = IO::File->new($self->file, 'r') || confess "Unable to open file (" . $self->file . ") for loading : $!"; return do { local $/; <$fh>; }; @@ -18,6 +18,8 @@ sub load { sub store { my ($self, $data) = @_; + + require IO::File; my $fh = IO::File->new($self->file, 'w') || confess "Unable to open file (" . $self->file . ") for storing : $!"; $fh->binmode(':utf8') if utf8::is_utf8($data); diff --git a/lib/MooseX/Storage/Format/JSON.pm b/lib/MooseX/Storage/Format/JSON.pm index c557872..13284c9 100644 --- a/lib/MooseX/Storage/Format/JSON.pm +++ b/lib/MooseX/Storage/Format/JSON.pm @@ -3,19 +3,23 @@ use Moose::Role; no warnings 'once'; -use JSON::Any; - requires 'pack'; requires 'unpack'; sub thaw { my ( $class, $json, @args ) = @_; + + require JSON::Any; + utf8::encode($json) if utf8::is_utf8($json); $class->unpack( JSON::Any->new->jsonToObj($json), @args ); } sub freeze { my ( $self, @args ) = @_; + + require JSON::Any; + my $json = JSON::Any->new(canonical => 1)->objToJson( $self->pack(@args) ); utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json); # if it's valid utf8 mark it as such return $json; diff --git a/lib/MooseX/Storage/Format/Storable.pm b/lib/MooseX/Storage/Format/Storable.pm index e5eb9af..c1ee936 100644 --- a/lib/MooseX/Storage/Format/Storable.pm +++ b/lib/MooseX/Storage/Format/Storable.pm @@ -1,18 +1,19 @@ package MooseX::Storage::Format::Storable; use Moose::Role; -use Storable (); - requires 'pack'; requires 'unpack'; sub thaw { my ( $class, $stored, @args ) = @_; + + require Storable; $class->unpack( Storable::thaw($stored), @args ); } sub freeze { my ( $self, @args ) = @_; + require Storable; Storable::nfreeze( $self->pack(@args) ); } diff --git a/lib/MooseX/Storage/Format/YAML.pm b/lib/MooseX/Storage/Format/YAML.pm index cec94d1..c80e2e2 100644 --- a/lib/MooseX/Storage/Format/YAML.pm +++ b/lib/MooseX/Storage/Format/YAML.pm @@ -5,18 +5,18 @@ use Moose::Role; # Tests break because tye YAML is invalid...? # -dcp -use YAML::Any qw(Load Dump); - requires 'pack'; requires 'unpack'; sub thaw { my ( $class, $yaml, @args ) = @_; - $class->unpack( Load($yaml), @args ); + require YAML::Any; + $class->unpack( YAML::Any::Load($yaml), @args ); } sub freeze { my ( $self, @args ) = @_; + require YAML::Any; Dump( $self->pack(@args) ); } diff --git a/lib/MooseX/Storage/IO/StorableFile.pm b/lib/MooseX/Storage/IO/StorableFile.pm index a42e3f6..d97f6a0 100644 --- a/lib/MooseX/Storage/IO/StorableFile.pm +++ b/lib/MooseX/Storage/IO/StorableFile.pm @@ -1,13 +1,14 @@ package MooseX::Storage::IO::StorableFile; use Moose::Role; -use Storable (); - requires 'pack'; requires 'unpack'; sub load { my ( $class, $filename, @args ) = @_; + + require Storable; + # try thawing return $class->thaw( Storable::retrieve($filename), @args ) if $class->can('thaw'); @@ -17,6 +18,9 @@ sub load { sub store { my ( $self, $filename, @args ) = @_; + + require Storable; + Storable::nstore( # try freezing, otherwise just pack ($self->can('freeze') ? $self->freeze(@args) : $self->pack(@args)),