2 package MooseX::Storage;
5 use MooseX::Storage::Meta::Attribute::DoNotSerialize;
10 return if $pkg eq 'main';
13 || confess "This package can only be used in Moose based classes";
15 $pkg->meta->alias_method('Storage' => sub {
18 $params{'base'} ||= 'Basic';
21 ('MooseX::Storage::' . $params{'base'}),
25 # you don't have to have a format
26 # role, this just means you dont
27 # get anything other than pack/unpack
28 push @roles => 'MooseX::Storage::Format::' . $params{'format'}
29 if exists $params{'format'};
32 # if you do choose an IO role, then
33 # you *must* have a format role chosen
34 # since load/store require freeze/thaw
35 if (exists $params{'io'}) {
36 (exists $params{'format'})
37 || confess "You must specify a format role in order to use an IO role";
38 push @roles => 'MooseX::Storage::IO::' . $params{'io'};
41 Class::MOP::load_class($_)
42 || die "Could not load role (" . $_ . ") for package ($pkg)"
57 MooseX::Storage - An serialization framework for Moose classes
65 with Storage('format' => 'JSON', 'io' => 'File');
67 has 'x' => (is => 'rw', isa => 'Int');
68 has 'y' => (is => 'rw', isa => 'Int');
72 my $p = Point->new(x => 10, y => 10);
74 ## methods to pack/unpack an
75 ## object in perl data structures
77 # pack the class into a hash
78 $p->pack(); # { __CLASS__ => 'Point', x => 10, y => 10 }
80 # unpack the hash into a class
81 my $p2 = Point->unpack({ __CLASS__ => 'Point', x => 10, y => 10 });
83 ## methods to freeze/thaw into
84 ## a specified serialization format
85 ## (in this case JSON)
87 # pack the class into a JSON string
88 $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
90 # unpack the JSON string into a class
91 my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');
93 ## methods to load/store a class
96 $p->store('my_point.json');
98 my $p2 = Point->load('my_point.json');
102 MooseX::Storage is a serialization framework for Moose, it provides
103 a very flexible and highly pluggable way to serialize Moose classes
104 to a number of different formats and styles.
106 =head2 Levels of Serialization
108 There are 3 levels to the serialization, each of which builds upon
109 the other and each of which can be customized to the specific needs
116 The first (base) level is C<pack> and C<unpack>. In this level the
117 class is serialized into a Perl HASH reference, it is tagged with the
118 class name and each instance attribute is stored. Very simple.
120 This level is not optional, it is the bare minumum that
121 MooseX::Storage provides and all other levels build on top of this.
125 The second (format) level is C<freeze> and C<thaw>. In this level the
126 output of C<pack> is sent to C<freeze> or the output of C<thaw> is sent
127 to C<unpack>. This levels primary role is to convert to and from the
128 specific serialization format and Perl land.
130 This level is optional, if you don't want/need it, you don't have to
131 have it. You can just use C<pack>/C<unpack> instead.
135 The third (io) level is C<load> and C<store>. In this level we are reading
136 and writing data to file/network/database/etc.
138 This level is also optional, it does however require the C<format> level
139 to be present (at least the current state does).
143 =head2 How we serialize
145 There are always limits to any serialization framework, there are just
146 some things which are really difficult to serialize properly and some
147 things which cannot be serialized at all.
149 =head2 What can be serialized?
151 Currently only numbers, string, ARRAY refs, HASH refs and other
152 MooseX::Storage enabled objects are supported.
154 With Array and Hash references the first level down is inspected and
155 any objects found are serialized/deserialized for you. We do not do
156 this recusively by default, however this feature may become an
159 The specific serialize/deserialize routine is determined by the
160 Moose type constraint a specific attribute has. In most cases subtypes
161 of the supported types are handled correctly, and there is a facility
162 for adding handlers for custom types as well. This will get documented
163 eventually, but it is currently still in development.
165 =head2 What can not be serialized?
167 We do not support CODE references yet, but this support might be added
168 in using B::Deparse or some other deep magic.
170 Scalar refs are not supported, mostly because there is no way to know
171 if the value being referenced will be there when the object is inflated.
172 I highly doubt will be ever support this in a general sense, but it
173 would be possible to add this yourself for a small specific case.
175 Circular references are specifically disallowed, however if you break
176 the cycles yourself then re-assemble them later you can get around this.
177 The reason we disallow circular refs is because they are not always supported
178 in all formats we use, and they tend to be very tricky to do for all
179 possible cases. It is almost always something you want to have tight control
184 This is B<not> a persistence framework, changes to your object after
185 you load or store it will not be reflected in the stored class.
191 =item B<Storage (%options)>
193 This module will export the C<Storage> method will can be used to
194 load a specific set of MooseX::Storage roles to implement a specific
195 combination of features. It is meant to make things easier, but it
196 is by no means the only way. You can still compose your roles by
219 This module needs docs and probably a couple a Cookbook of some kind
220 as well. This is an early release, so that is my excuse for now :)
222 For the time being, please read the tests and feel free to email me
223 if you have any questions. This module can also be discussed on IRC
224 in the #moose channel on irc.perl.org.
228 All complex software has bugs lurking in it, and this module is no
229 exception. If you find a bug please either email me, or add the bug
234 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
236 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
238 =head1 COPYRIGHT AND LICENSE
240 Copyright 2007 by Infinity Interactive, Inc.
242 L<http://www.iinteractive.com>
244 This library is free software; you can redistribute it and/or modify
245 it under the same terms as Perl itself.