line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Serializer::Raw; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1877
|
use warnings; |
|
1
|
|
|
|
|
17
|
|
|
1
|
|
|
|
|
38
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
5
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
62
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
871
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.02'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#Global cache of modules we've loaded |
11
|
|
|
|
|
|
|
my %_MODULES; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %_fields = ( |
14
|
|
|
|
|
|
|
serializer => 'Data::Dumper', |
15
|
|
|
|
|
|
|
options => {}, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
sub new { |
18
|
126
|
|
|
126
|
1
|
297073
|
my ($class, %args) = @_; |
19
|
126
|
|
|
|
|
414
|
my $dataref = {%_fields}; |
20
|
126
|
|
|
|
|
312
|
foreach my $field (keys %_fields) { |
21
|
252
|
100
|
|
|
|
676
|
$dataref->{$field} = $args{$field} if exists $args{$field}; |
22
|
|
|
|
|
|
|
} |
23
|
126
|
|
|
|
|
205
|
my $self = $dataref; |
24
|
126
|
|
|
|
|
214
|
bless $self, $class; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#initialize serializer |
27
|
126
|
|
|
|
|
306
|
$self->_serializer_obj(); |
28
|
|
|
|
|
|
|
|
29
|
126
|
|
|
|
|
341
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub serializer { |
33
|
0
|
|
|
0
|
1
|
0
|
my $self = (shift); |
34
|
0
|
|
|
|
|
0
|
my $return = $self->{serializer}; |
35
|
0
|
0
|
|
|
|
0
|
if (@_) { |
36
|
0
|
|
|
|
|
0
|
$self->{serializer} = (shift); |
37
|
|
|
|
|
|
|
#reinitialize serializer object |
38
|
0
|
|
|
|
|
0
|
$self->_serializer_obj(1); |
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
0
|
return $return; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub options { |
44
|
0
|
|
|
0
|
1
|
0
|
my $self = (shift); |
45
|
0
|
|
|
|
|
0
|
my $return = $self->{options}; |
46
|
0
|
0
|
|
|
|
0
|
if (@_) { |
47
|
0
|
|
|
|
|
0
|
$self->{options} = (shift); |
48
|
|
|
|
|
|
|
#reinitialize serializer object |
49
|
0
|
|
|
|
|
0
|
$self->_serializer_obj(1); |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
0
|
return $return; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _persistent_obj { |
55
|
0
|
|
|
0
|
|
0
|
my $self = (shift); |
56
|
0
|
0
|
|
|
|
0
|
return $self->{persistent_obj} if (exists $self->{persistent_obj}); |
57
|
0
|
|
|
|
|
0
|
$self->_module_loader('Data::Serializer::Persistent'); |
58
|
0
|
|
|
|
|
0
|
my $persistent_obj = { parent => $self }; |
59
|
0
|
|
|
|
|
0
|
bless $persistent_obj, "Data::Serializer::Persistent"; |
60
|
0
|
|
|
|
|
0
|
$self->{persistent_obj} = $persistent_obj; |
61
|
0
|
|
|
|
|
0
|
return $persistent_obj; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub store { |
66
|
0
|
|
|
0
|
1
|
0
|
my $self = (shift); |
67
|
0
|
|
|
|
|
0
|
my $persistent = $self->_persistent_obj(); |
68
|
0
|
|
|
|
|
0
|
$persistent->_store(@_); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub retrieve { |
72
|
0
|
|
|
0
|
1
|
0
|
my $self = (shift); |
73
|
0
|
|
|
|
|
0
|
my $persistent = $self->_persistent_obj(); |
74
|
0
|
|
|
|
|
0
|
$persistent->_retrieve(@_); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _module_loader { |
79
|
126
|
|
|
126
|
|
174
|
my $self = (shift); |
80
|
126
|
|
|
|
|
203
|
my $module_name = (shift); |
81
|
126
|
50
|
|
|
|
252
|
return if (exists $_MODULES{$module_name}); |
82
|
126
|
50
|
|
|
|
245
|
if (@_) { |
83
|
126
|
|
|
|
|
305
|
$module_name = (shift) . "::$module_name"; |
84
|
|
|
|
|
|
|
} |
85
|
126
|
|
|
|
|
190
|
my $package = $module_name; |
86
|
126
|
|
|
|
|
430
|
$package =~ s|::|/|g; |
87
|
126
|
|
|
|
|
213
|
$package .= ".pm"; |
88
|
126
|
|
|
|
|
190
|
eval { require $package }; |
|
126
|
|
|
|
|
8670
|
|
89
|
126
|
50
|
|
|
|
290
|
if ($@) { |
90
|
0
|
|
|
|
|
0
|
carp "Data::Serializer error: " . |
91
|
|
|
|
|
|
|
"Please make sure $package is a properly installed package.\n"; |
92
|
0
|
|
|
|
|
0
|
return undef; |
93
|
|
|
|
|
|
|
} |
94
|
126
|
|
|
|
|
261
|
$_MODULES{$module_name} = 1; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _serializer_obj { |
98
|
378
|
|
|
378
|
|
501
|
my $self = (shift); |
99
|
|
|
|
|
|
|
#if anything is passed in remove previous obj so we will regenerate it |
100
|
378
|
50
|
|
|
|
827
|
if (@_) { |
101
|
0
|
|
|
|
|
0
|
delete $self->{serializer_obj}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
#Return cached serializer object if it exists |
104
|
378
|
100
|
|
|
|
1127
|
return $self->{serializer_obj} if (exists $self->{serializer_obj}); |
105
|
|
|
|
|
|
|
|
106
|
126
|
|
|
|
|
195
|
my $method = $self->{serializer}; |
107
|
126
|
|
|
|
|
271
|
$self->_module_loader($method,"Data::Serializer"); #load in serializer module if necessary |
108
|
|
|
|
|
|
|
|
109
|
126
|
|
|
|
|
296
|
$self->{serializer_obj}->{options} = $self->{options}; |
110
|
126
|
|
|
|
|
325
|
bless $self->{serializer_obj}, "Data::Serializer::$method"; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub serialize { |
114
|
126
|
|
|
126
|
1
|
1929
|
my $self = (shift); |
115
|
126
|
|
|
|
|
256
|
my @input = @_; |
116
|
|
|
|
|
|
|
|
117
|
126
|
|
|
|
|
270
|
return $self->_serializer_obj->serialize(@input); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub deserialize { |
122
|
126
|
|
|
126
|
1
|
44345
|
my $self = (shift); |
123
|
126
|
|
|
|
|
204
|
my $input = (shift); |
124
|
|
|
|
|
|
|
|
125
|
126
|
|
|
|
|
216
|
return $self->_serializer_obj->deserialize($input); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; |
129
|
|
|
|
|
|
|
__END__ |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=pod |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 NAME |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Data::Serializer::Raw - Provides unified raw interface to perl serializers |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SYNOPSIS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
use Data::Serializer::Raw; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$obj = Data::Serializer::Raw->new(); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$obj = Data::Serializer::Raw->new(serializer => 'Storable'); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$serialized = $obj->serialize({a => [1,2,3],b => 5}); |
146
|
|
|
|
|
|
|
$deserialized = $obj->deserialize($serialized); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
print "$deserialized->{b}\n"; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 DESCRIPTION |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Provides a unified interface to the various serializing modules |
153
|
|
|
|
|
|
|
currently available. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This is a straight pass through to the underlying serializer, |
156
|
|
|
|
|
|
|
nothing else is done. (no encoding, encryption, compression, etc) |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 EXAMPLES |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=over 4 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item Please see L<Data::Serializer::Cookbook(3)> |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 METHODS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=over 4 |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item B<new> - constructor |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
$obj = Data::Serializer::Raw->new(); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
$obj = Data::Serializer::Raw->new( |
176
|
|
|
|
|
|
|
serializer => 'Data::Dumper', |
177
|
|
|
|
|
|
|
options => {}, |
178
|
|
|
|
|
|
|
); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
B<new> is the constructor object for Data::Serializer::Raw objects. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=over 4 |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
The default I<serializer> is C<Data::Dumper> |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
The default I<options> is C<{}> (pass nothing on to serializer) |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=back |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item B<serialize> - serialize reference |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
$serialized = $obj->serialize({a => [1,2,3],b => 5}); |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
This is a straight pass through to the underlying serializer, |
200
|
|
|
|
|
|
|
nothing else is done. (no encoding, encryption, compression, etc) |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item B<deserialize> - deserialize reference |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
$deserialized = $obj->deserialize($serialized); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
This is a straight pass through to the underlying serializer, |
207
|
|
|
|
|
|
|
nothing else is done. (no encoding, encryption, compression, etc) |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item B<serializer> - change the serializer |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Currently supports the following serializers: |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=over 4 |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item L<Bencode(3)> |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=item L<Convert::Bencode(3)> |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item L<Convert::Bencode_XS(3)> |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item L<Config::General(3)> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item L<Data::Denter(3)> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item L<Data::Dumper(3)> |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=item L<Data::Taxi(3)> |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item L<FreezeThaw(3)> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=item L<JSON(3)> |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item L<JSON::Syck(3)> |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item L<PHP::Serialization(3)> |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item L<Storable(3)> |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=item L<XML::Dumper(3)> |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=item L<XML::Simple(3)> |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=item L<YAML(3)> |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=item L<YAML::Syck(3)> |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=back |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Default is to use Data::Dumper. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Each serializer has its own caveat's about usage especially when dealing with |
252
|
|
|
|
|
|
|
cyclical data structures or CODE references. Please see the appropriate |
253
|
|
|
|
|
|
|
documentation in those modules for further information. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=item B<options> - pass options through to underlying serializer |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Currently is only supported by L<Config::General(3)>, and L<XML::Dumper(3)>. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
my $obj = Data::Serializer::Raw->new(serializer => 'Config::General', |
261
|
|
|
|
|
|
|
options => { |
262
|
|
|
|
|
|
|
-LowerCaseNames => 1, |
263
|
|
|
|
|
|
|
-UseApacheInclude => 1, |
264
|
|
|
|
|
|
|
-MergeDuplicateBlocks => 1, |
265
|
|
|
|
|
|
|
-AutoTrue => 1, |
266
|
|
|
|
|
|
|
-InterPolateVars => 1 |
267
|
|
|
|
|
|
|
}, |
268
|
|
|
|
|
|
|
) or die "$!\n"; |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
or |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
my $obj = Data::Serializer::Raw->new(serializer => 'XML::Dumper', |
273
|
|
|
|
|
|
|
options => { dtd => 1, } |
274
|
|
|
|
|
|
|
) or die "$!\n"; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=item B<store> - serialize data and write it to a file (or file handle) |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
$obj->store({a => [1,2,3],b => 5},$file, [$mode, $perm]); |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
or |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
$obj->store({a => [1,2,3],b => 5},$fh); |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Serializes the reference specified using the B<serialize> method |
286
|
|
|
|
|
|
|
and writes it out to the specified file or filehandle. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
If a file path is specified you may specify an optional mode and permission as the |
289
|
|
|
|
|
|
|
next two arguments. See L<IO::File> for examples. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Trips an exception if it is unable to write to the specified file. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=item B<retrieve> - read data from file (or file handle) and return it after deserialization |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
my $ref = $obj->retrieve($file); |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
or |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
my $ref = $obj->retrieve($fh); |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Reads first line of supplied file or filehandle and returns it deserialized. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=back |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=head1 AUTHOR |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
Neil Neely <F<neil@neely.cx>>. |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
http://neil-neely.blogspot.com/ |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=head1 BUGS |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
Please report all bugs here: |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
http://rt.cpan.org/Public/Dist/Display.html?Name=Data-Serializer |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
Copyright (c) 2011 Neil Neely. All rights reserved. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
324
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.2 or, |
325
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
See http://www.perl.com/language/misc/Artistic.html |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
Peter Makholm took the time to profile L<Data::Serializer(3)> and pointed out the value |
333
|
|
|
|
|
|
|
of having a very lean implementation that minimized overhead and just used the raw underlying serializers. |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=head1 SEE ALSO |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
perl(1), Data::Serializer(3). |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=cut |
340
|
|
|
|
|
|
|
|