Corrected patching behavior
[onis.git] / lib / Onis / Data / Persistent / Storable.pm
1 package Onis::Data::Persistent::Storable;
2
3 use strict;
4 use warnings;
5
6 use Carp (qw(carp confess));
7 use Storable (qw(store retrieve));
8
9 use Onis::Config (qw(get_config));
10 use Onis::Data::Persistent::None (qw($TREE));
11
12 =head1 NAME
13
14 Onis::Data::Persistent::Storable - Storage backend using storable
15
16 =head1 DESCRIPTION
17
18 Simple storage backend that handles data in-memory. At the end of each session
19 the data is read from a storable-dump.
20
21 This module is basically a wrapper around L<Onis::Data::Persistent::None> that
22 gets the data from a file before and action is taken and writes it back to the
23 file after everything has been done.
24
25 =head1 CONFIGURATION OPTIONS
26
27 =over 4
28
29 =item B<storage_file>: "I<storage.dat>";
30
31 Sets the file storable will write it's data to.
32
33 =item B<storage_dir>: "I<var/>";
34
35 Sets the directory in which B<storage_file> can be found.
36
37 =back
38
39 =cut
40
41 our $StorageFile = get_config ('storage_file') || 'storage.dat';
42 our $StorageDir  = get_config ('storage_dir')  || 'var';
43
44 $StorageDir =~ s#/+$##;
45
46 if (!-d $StorageDir)
47 {
48         print STDERR $/, __FILE__, ':', <<ERROR;
49
50 ``storage_dir'' is set to ``$StorageDir'', but the directory doesn't exist or
51 isn't a directory. Please fix it..
52
53 ERROR
54         exit (1);
55 }
56
57 if (-f "$StorageDir/$StorageFile")
58 {
59         $TREE = retrieve ("$StorageDir/$StorageFile");
60 }
61
62 if ($::DEBUG & 0x0200)
63 {
64         require Data::Dumper;
65 }
66
67 @Onis::Data::Persistent::Storable::ISA = ('Onis::Data::Persistent::None');
68
69 return (1);
70
71 END
72 {
73         store ($TREE, "$StorageDir/$StorageFile");
74 }
75
76 =head1 AUTHOR
77
78 Florian octo Forster, E<lt>octo at verplant.orgE<gt>
79
80 =cut