blib/lib/Storable/Ref.pm untested

filter: Statementstmt 0.0% 0 / 23 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 44 CC 5 SCAR 28.7
CC
5
Cov
0%
new
30.0
savenow
2.0
DESTROY
2.0
CRAP 17.6
1 #!/usr/bin/perl
2 package Storable::Ref;
3 use strict;
4 use Storable;
5  
6 our $VERSION="1.1";
7  
8 =head1 NAME
9  
10 Storable::Ref - Persistent automatic variables vi Storable
11  
12 =head1 SYNOPSYS
13  
14 $ cat >tesy.pl
15 use Storable::Ref;
16 my $v=new Storable::Ref({a=>1, b=>1},"filename");
17 print "a=".($v->{a}++)." b=".($v->{b}*=2)."\n";
18 ^D
19 $ perl test.pl
20 a=1 b=2
21 $ for v in 1 2 3; do perl test.pl; done
22 a=2 b=4
23 a=3 b=8
24 a=4 b=16
25 rm filename
26 $ perl test.pl
27 a=1 b=2
28  
29 L<Storable::Ref> gets your reference to variable and remembers
30 filename associated with this variable. When you construct
31 this variable you provide to constructor default value
32 and fully qualified filename for storing this it.
33 If L<Storable::Ref> finds this file and it contains valid
34 L<Storable> object, it uses this data to initialize variable
35 with this data instead of default value.
36  
37 this functionality my be used for storing state of
38 interuptable scripts, or for saving important information
39 when script throws die or uncatched exceptions.
40  
41 =head1 CAUTION
42  
43 L<Storable::Ref> stores data in method DESTROY(), and if
44 thismethod isn't called, saving is not processed.
45 if yor perl was killed by sig9 or sig11 data will be lost.
46  
47 =cut
48  
49  
50 our $storage={};
51  
52 =head1 METHODS
53  
54 =head2 new($defaultvalue, $filepath)
55  
56 constructor rebless variable to L<Storable::Ref>.
57 So you can't use persistence on blessed variables, yet.
58  
59 =cut
60  
61 sub new {
62 my ($class, $var, $fname)=@_;
63 die "Variable must be a reference" unless ref $var;
64 die "File name must be presented" unless $fname and !ref $fname;
65 my $tmp;
66 $tmp=Storable::retrieve($fname) if -f $fname;
67 $var=$tmp if $tmp;
68 "$var"=~/\(0x([0-9a-f]+)\)/;
69 $storage->{$1}=$fname;
70 return bless $var;
71 }
72  
73 =head2 savenow()
74  
75 $variable->savenow() Implicitly stores data,
76 use it if you convinient to death without of
77 testating your stuff.
78  
79 =cut
80  
81 sub savenow {
82 my $self=shift;
83 "$self"=~/\(0x([0-9a-f]+)\)/;
84 Storable::nstore($self, $storage->{$1});
85 }
86  
87 =head2 DESTROY()
88  
89 This method called automaticaly, when perl intended to
90 delete variable and clear it memory.
91 Method recall in memory filename associated with this
92 variable and stores it to this file.
93 Works automatically when:
94 $variable=undef;
95 or variable leaves it scope, or when programm terminates,
96 but perl data structures is not destroyed.
97  
98  
99 =cut
100  
101  
102 sub DESTROY {
103 my $self=shift;
104 "$self"=~/\(0x([0-9a-f]+)\)/;
105 Storable::nstore($self, $storage->{$1});
106 }
107  
108 =head1 AUTHOR
109  
110 Vany Serezhkin <ivan@serezhkin.com> 2009 Yandex.
111  
112 =cut
113  
114 1;