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