Fixes this bug:
[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 = 'var';
43 if (get_config ('storage_dir'))
44 {
45         $StorageDir = get_config ('storage_dir');
46 }
47 elsif ($ENV{'HOME'})
48 {
49         $StorageDir = $ENV{'HOME'} . '/.onis/data';
50 }
51 $StorageDir =~ s#/+$##;
52
53 if (!-d $StorageDir)
54 {
55         print STDERR $/, __FILE__, ':', <<ERROR;
56
57 ``storage_dir'' is set to ``$StorageDir'', but the directory doesn't exist or
58 isn't a directory. Please fix it..
59
60 ERROR
61         exit (1);
62 }
63
64 if (-f "$StorageDir/$StorageFile")
65 {
66         $TREE = retrieve ("$StorageDir/$StorageFile");
67 }
68
69 if ($::DEBUG & 0x0200)
70 {
71         require Data::Dumper;
72 }
73
74 @Onis::Data::Persistent::Storable::ISA = ('Onis::Data::Persistent::None');
75
76 return (1);
77
78 END
79 {
80         store ($TREE, "$StorageDir/$StorageFile");
81 }
82
83 =head1 AUTHOR
84
85 Florian octo Forster, E<lt>octo at verplant.orgE<gt>
86
87 =cut