Version 0.32
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / IO / StorableFile.pm
CommitLineData
124c2ba5 1package MooseX::Storage::IO::StorableFile;
2use Moose::Role;
3
4use Storable ();
5
e44b5f54 6our $VERSION = '0.32';
124c2ba5 7our $AUTHORITY = 'cpan:STEVAN';
8
9requires 'pack';
10requires 'unpack';
11
12sub load {
bf33d7c7 13 my ( $class, $filename, @args ) = @_;
124c2ba5 14 # try thawing
bf33d7c7 15 return $class->thaw( Storable::retrieve($filename), @args )
ec725183 16 if $class->can('thaw');
124c2ba5 17 # otherwise just unpack
bf33d7c7 18 $class->unpack( Storable::retrieve($filename), @args );
124c2ba5 19}
20
21sub store {
bf33d7c7 22 my ( $self, $filename, @args ) = @_;
ec725183 23 Storable::nstore(
124c2ba5 24 # try freezing, otherwise just pack
ec725183 25 ($self->can('freeze') ? $self->freeze(@args) : $self->pack(@args)),
26 $filename
124c2ba5 27 );
28}
29
f82612bc 30no Moose::Role;
31
124c2ba5 321;
33
34__END__
35
36=pod
37
38=head1 NAME
39
4fa64e86 40MooseX::Storage::IO::StorableFile - An Storable File I/O role
124c2ba5 41
42=head1 SYNOPSIS
43
44 package Point;
45 use Moose;
46 use MooseX::Storage;
ec725183 47
124c2ba5 48 with Storage('io' => 'StorableFile');
ec725183 49
124c2ba5 50 has 'x' => (is => 'rw', isa => 'Int');
51 has 'y' => (is => 'rw', isa => 'Int');
ec725183 52
124c2ba5 53 1;
ec725183 54
124c2ba5 55 my $p = Point->new(x => 10, y => 10);
ec725183 56
57 ## methods to load/store a class
124c2ba5 58 ## on the file system
ec725183 59
124c2ba5 60 $p->store('my_point');
ec725183 61
124c2ba5 62 my $p2 = Point->load('my_point');
63
64=head1 DESCRIPTION
65
ec725183 66This module will C<load> and C<store> Moose classes using Storable. It
67uses C<Storable::nstore> by default so that it can be easily used
68across machines or just locally.
124c2ba5 69
ec725183 70One important thing to note is that this module does not mix well
71with the other Format modules. Since Storable serialized perl data
72structures in it's own format, those roles are lagely unnecessary.
124c2ba5 73
ec725183 74However, there is always the possibility that having a set of
75C<freeze/thaw> hooks can be useful, so because of that this module
124c2ba5 76will attempt to use C<freeze> or C<thaw> if that method is available.
ec725183 77Of course, you should be careful when doing this as it could lead to
124c2ba5 78all sorts of hairy issues. But you have been warned.
79
80=head1 METHODS
81
82=over 4
83
84=item B<load ($filename)>
85
86=item B<store ($filename)>
87
88=back
89
90=head2 Introspection
91
92=over 4
93
94=item B<meta>
95
96=back
97
98=head1 BUGS
99
ec725183 100All complex software has bugs lurking in it, and this module is no
124c2ba5 101exception. If you find a bug please either email me, or add the bug
102to cpan-RT.
103
104=head1 AUTHOR
105
106Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
107
108=head1 COPYRIGHT AND LICENSE
109
1f3074ea 110Copyright 2007-2008 by Infinity Interactive, Inc.
124c2ba5 111
112L<http://www.iinteractive.com>
113
114This library is free software; you can redistribute it and/or modify
115it under the same terms as Perl itself.
116
117=cut
118
119