| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Class::DataStore; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Class::DataStore - Generic OO data storage/retrieval |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my %values = ( one => 1, two => 2 ); |
|
12
|
|
|
|
|
|
|
my $store = Class::DataStore->new( \%values ); |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# using get/set methods |
|
15
|
|
|
|
|
|
|
$store->set( 'three', 3 ); |
|
16
|
|
|
|
|
|
|
my $three = $store->get( 'three' ); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# using AUTOLOAD method |
|
19
|
|
|
|
|
|
|
$store->four( 4 ); |
|
20
|
|
|
|
|
|
|
my $four = $store->four; |
|
21
|
|
|
|
|
|
|
my @four = $store->four; # returns a list |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $exists = $store->exists( 'three' ); # $exists = 1 |
|
24
|
|
|
|
|
|
|
my $data_hashref = $store->dump; |
|
25
|
|
|
|
|
|
|
$store->clear; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Class::DataStore implements a simple storage system for object data. This data |
|
30
|
|
|
|
|
|
|
can be accessed via get/set methods and AUTOLOAD. AUTOLOAD calls are not added |
|
31
|
|
|
|
|
|
|
to the symbol table, so using get/set will be faster. Using AUTOLOAD also means |
|
32
|
|
|
|
|
|
|
that you will not be able to store data with a key that is already used by a |
|
33
|
|
|
|
|
|
|
instance method, such as "get" or "exists". |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This module was written originally as part of a website framework that was used |
|
36
|
|
|
|
|
|
|
for the Democratic National Committee website in 2004. Some of the |
|
37
|
|
|
|
|
|
|
implementations here, such as get() optionally returning a list if called in |
|
38
|
|
|
|
|
|
|
array context, reflect the way this module was originally used for building web |
|
39
|
|
|
|
|
|
|
applications. |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Class::DataStore is most useful when subclassed. To preserve the AUTOLOAD |
|
42
|
|
|
|
|
|
|
functionality, be sure to add the following when setting up the subclass: |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
use base 'Class::DataStore'; |
|
45
|
|
|
|
|
|
|
*AUTOLOAD = \&Class::DataStore::AUTOLOAD; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
This module is also a useful add-on for modules that need quick and simple data |
|
48
|
|
|
|
|
|
|
storage, e.g. to store configuration data: |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$self->{_config} = Class::Datastore->new( $config_data ); |
|
51
|
|
|
|
|
|
|
sub config { return $_[0]->{_config}; } |
|
52
|
|
|
|
|
|
|
my $server = $self->config->server; |
|
53
|
|
|
|
|
|
|
my $sender = $self->config->get( 'sender' ); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 METHODS |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
|
58
|
|
|
|
|
|
|
|
|
59
|
1
|
|
|
1
|
|
1089
|
use 5.006; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
39
|
|
|
60
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
33
|
|
|
61
|
1
|
|
|
1
|
|
15
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
400
|
|
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
our $AUTOLOAD; |
|
64
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=pod |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 new( $data_hashref ) |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The $data_hashref is stored in $self->{_data}. Returns the blessed object. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub new { |
|
75
|
1
|
|
|
1
|
1
|
370
|
my $class = shift; |
|
76
|
1
|
|
50
|
|
|
5
|
my $data = shift || {}; |
|
77
|
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
4
|
my $self = bless { _data => $data }, $class; |
|
79
|
1
|
|
|
|
|
3
|
return $self; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=pod |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 exists( $key ) |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Returns 1 if the $key exists in the $self->{_data} hashref. Otherwise, returns |
|
87
|
|
|
|
|
|
|
0. |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub exists { |
|
92
|
4
|
|
|
4
|
1
|
10
|
my $self = shift; |
|
93
|
4
|
|
|
|
|
6
|
my $key = shift; |
|
94
|
|
|
|
|
|
|
|
|
95
|
4
|
100
|
|
|
|
22
|
return 1 if exists $self->{_data}->{$key}; |
|
96
|
1
|
|
|
|
|
4
|
return 0; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=pod |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 get( $key ) |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Returns the value of $self->{_data}->{$key}, or undef. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
If the value is stored as an ARRAYREF, HASHREF or a scalar, and wantarray is |
|
106
|
|
|
|
|
|
|
true, the return value will be a list. Otherwise, the value will be returned |
|
107
|
|
|
|
|
|
|
unaltered. |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub get { |
|
112
|
23
|
|
|
23
|
1
|
2579
|
my $self = shift; |
|
113
|
23
|
|
|
|
|
54
|
my $key = shift; |
|
114
|
|
|
|
|
|
|
|
|
115
|
23
|
|
|
|
|
42
|
my $value = $self->{_data}->{$key}; |
|
116
|
|
|
|
|
|
|
|
|
117
|
23
|
100
|
|
|
|
68
|
if ( ref $value eq 'ARRAY' ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
118
|
3
|
100
|
|
|
|
16
|
return wantarray ? @$value : $value; |
|
119
|
|
|
|
|
|
|
} elsif ( ref $value eq 'HASH' ) { |
|
120
|
3
|
100
|
|
|
|
18
|
return wantarray ? %$value : $value; |
|
121
|
|
|
|
|
|
|
} elsif ( ref $value eq '' ) { |
|
122
|
17
|
100
|
|
|
|
83
|
return wantarray ? ( $value ) : $value; |
|
123
|
|
|
|
|
|
|
} else { |
|
124
|
0
|
|
|
|
|
0
|
return $value; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=pod |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 set( $key => $value ) |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Sets $self->{_data}->{$key} to $value, and returns $value. Values must be |
|
133
|
|
|
|
|
|
|
scalars, including, of course, references. |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub set { |
|
138
|
8
|
|
|
8
|
1
|
480
|
my $self = shift; |
|
139
|
8
|
|
|
|
|
10
|
my $key = shift; |
|
140
|
8
|
|
|
|
|
10
|
my $value = shift; |
|
141
|
|
|
|
|
|
|
|
|
142
|
8
|
|
|
|
|
18
|
$self->{_data}->{$key} = $value; |
|
143
|
8
|
|
|
|
|
26
|
return $value; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=pod |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 dump() |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Returns the $self->{_data} as hashref or hash, depending on the call. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub dump { |
|
156
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
157
|
|
|
|
|
|
|
|
|
158
|
0
|
0
|
|
|
|
0
|
return wantarray ? %{ $self->{_data} } : $self->{_data}; |
|
|
0
|
|
|
|
|
0
|
|
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=pod |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 clear() |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Deletes all the keys from $self->{_data}. Returns the number of keys deleted. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub clear { |
|
171
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
|
172
|
1
|
|
|
|
|
2
|
my $ct = 0; |
|
173
|
|
|
|
|
|
|
|
|
174
|
1
|
|
|
|
|
2
|
foreach my $key ( keys %{ $self->{_data} } ) { |
|
|
1
|
|
|
|
|
6
|
|
|
175
|
4
|
|
|
|
|
7
|
delete $self->{_data}->{$key}; |
|
176
|
4
|
|
|
|
|
7
|
$ct++; |
|
177
|
|
|
|
|
|
|
} |
|
178
|
1
|
|
|
|
|
5
|
return $ct; |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=pod |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 AUTOLOAD() |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Tries to determine $key from the method call. Returns $self->{_data}->{$key}, |
|
187
|
|
|
|
|
|
|
or undef. |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub AUTOLOAD { |
|
192
|
1
|
|
|
1
|
|
5
|
no strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
151
|
|
|
193
|
14
|
50
|
|
14
|
|
1345
|
if ( $AUTOLOAD =~ /(\w+)$/ ) { |
|
194
|
14
|
|
|
|
|
26
|
my $key = $1; |
|
195
|
14
|
|
|
|
|
21
|
my ( $self, $value ) = @_; |
|
196
|
14
|
100
|
|
|
|
25
|
if ( @_ == 2 ) { |
|
197
|
4
|
|
|
|
|
36
|
return $self->set( $key, $value ); |
|
198
|
|
|
|
|
|
|
} else { |
|
199
|
10
|
|
|
|
|
21
|
return $self->get( $key ); |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
} else { |
|
202
|
0
|
|
|
|
|
|
return; |
|
203
|
|
|
|
|
|
|
} |
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
|
|
206
|
0
|
|
|
0
|
|
|
sub DESTROY {} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=pod |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 AUTHOR |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Eric Folley, Eeric@folley.netE |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Copyright 2004-2005 by Eric Folley |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under |
|
219
|
|
|
|
|
|
|
the same terms as Perl itself. |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
1; |