Commit | Line | Data |
124c2ba5 |
1 | package MooseX::Storage::Format::Storable; |
2 | use Moose::Role; |
3 | |
4 | use Storable (); |
5 | |
124c2ba5 |
6 | requires 'pack'; |
7 | requires 'unpack'; |
8 | |
9 | sub thaw { |
10 | my ( $class, $stored, @args ) = @_; |
11 | $class->unpack( Storable::thaw($stored), @args ); |
12 | } |
13 | |
14 | sub freeze { |
15 | my ( $self, @args ) = @_; |
16 | Storable::nfreeze( $self->pack(@args) ); |
17 | } |
18 | |
f82612bc |
19 | no Moose::Role; |
20 | |
124c2ba5 |
21 | 1; |
22 | |
23 | __END__ |
24 | |
25 | =pod |
26 | |
27 | =head1 NAME |
28 | |
4fa64e86 |
29 | MooseX::Storage::Format::Storable - A Storable serialization role |
124c2ba5 |
30 | |
31 | =head1 SYNOPSIS |
32 | |
33 | package Point; |
34 | use Moose; |
35 | use MooseX::Storage; |
ec725183 |
36 | |
124c2ba5 |
37 | with Storage('format' => 'Storable'); |
ec725183 |
38 | |
124c2ba5 |
39 | has 'x' => (is => 'rw', isa => 'Int'); |
40 | has 'y' => (is => 'rw', isa => 'Int'); |
ec725183 |
41 | |
124c2ba5 |
42 | 1; |
ec725183 |
43 | |
124c2ba5 |
44 | my $p = Point->new(x => 10, y => 10); |
ec725183 |
45 | |
46 | ## methods to freeze/thaw into |
124c2ba5 |
47 | ## a specified serialization format |
ec725183 |
48 | |
124c2ba5 |
49 | # pack the class with Storable |
ec725183 |
50 | my $storable_data = $p->freeze(); |
51 | |
124c2ba5 |
52 | # unpack the storable data into the class |
ec725183 |
53 | my $p2 = Point->thaw($storable_data); |
124c2ba5 |
54 | |
55 | =head1 DESCRIPTION |
56 | |
ec725183 |
57 | This module will C<thaw> and C<freeze> Moose classes using Storable. It |
58 | uses C<Storable::nfreeze> by default so that it can be easily used |
59 | in IPC scenarios across machines or just locally. |
124c2ba5 |
60 | |
ec725183 |
61 | One important thing to note is that this module does not mix well |
62 | with the IO modules. The structures that C<freeze> and C<thaw> deal with |
63 | are Storable's memory representation, and (as far as I know) that |
64 | is not easily just written onto a file. If you want file based |
65 | serialization with Storable, the please look at the |
124c2ba5 |
66 | L<MooseX::Storage::IO::StorableFile> role instead. |
67 | |
68 | =head1 METHODS |
69 | |
70 | =over 4 |
71 | |
72 | =item B<freeze> |
73 | |
74 | =item B<thaw ($stored)> |
75 | |
76 | =back |
77 | |
78 | =head2 Introspection |
79 | |
80 | =over 4 |
81 | |
82 | =item B<meta> |
83 | |
84 | =back |
85 | |
86 | =head1 BUGS |
87 | |
ec725183 |
88 | All complex software has bugs lurking in it, and this module is no |
124c2ba5 |
89 | exception. If you find a bug please either email me, or add the bug |
90 | to cpan-RT. |
91 | |
92 | =head1 AUTHOR |
93 | |
94 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
95 | |
96 | =head1 COPYRIGHT AND LICENSE |
97 | |
1f3074ea |
98 | Copyright 2007-2008 by Infinity Interactive, Inc. |
124c2ba5 |
99 | |
100 | L<http://www.iinteractive.com> |
101 | |
102 | This library is free software; you can redistribute it and/or modify |
103 | it under the same terms as Perl itself. |
104 | |
105 | =cut |
106 | |
107 | |