MooseX::Storage::MaybeDeferred - A role for the less indecisive programmers
0.0.4
package Point; use Moose; use MooseX::Storage; with MooseX::Storage::MaybeDeferred => { default_format => 'JSON', default_io => 'File', }; has 'x' => (is => 'rw', isa => 'Int'); has 'y' => (is => 'rw', isa => 'Int'); 1; my $p = Point->new(); $p->freeze(); # or $p->freeze({format => 'Storable'}); ... $p->store($filename); $p->store($filename, {format => 'Storable', io => 'AtomicFile'}); ... my $another_point; $another_point = Point->load($filename); # or $another_point = Point->load($filename, {format => 'JSON', io => 'File'});
With the module MooseX::Storage you are hard coding the definition of the format and maybe io layer in the classes you want to serialize. Whenever the methods freeze or store are called, it is not possible to to change their behaviour. You always get what you have declared.
format
io
freeze
store
If you need to serialize into different formats you can use MooseX::Storage::Deferred. Now, whenever you call freeze or store you must provide parameters which define the format and the io layer.
This module should give you the benefits of both worlds. You need to provide the default_format and default_io layers in the definitions of the classes which you want to serialize. So classes that used to use MooseX::Storage should still behave as before. But if you need to serialize into a different format you have the flexibility of MooseX::Storage::Deferred. Now you can provide the format and io setting at runtime.
default_format
default_io
Thanks www.netdescribe.com.
Fixed tests so it is now able to run on Perl 5.8.x
Copyright 2018 Martin Barth.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install MooseX::Storage::MaybeDeferred, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Storage::MaybeDeferred
CPAN shell
perl -MCPAN -e shell install MooseX::Storage::MaybeDeferred
For more information on module installation, please visit the detailed CPAN module installation guide.