1 ;# $Id: Storable.pm,v 0.7.1.3 2000/08/23 22:49:25 ram Exp $
3 ;# Copyright (c) 1995-2000, Raphael Manfredi
5 ;# You may redistribute only under the terms of the Artistic License,
6 ;# as specified in the README file that comes with the distribution.
8 ;# $Log: Storable.pm,v $
9 ;# Revision 0.7.1.3 2000/08/23 22:49:25 ram
10 ;# patch3: updated version number
12 ;# Revision 0.7.1.2 2000/08/14 07:18:40 ram
13 ;# patch2: increased version number
15 ;# Revision 0.7.1.1 2000/08/13 20:08:58 ram
16 ;# patch1: mention new Clone(3) extension in SEE ALSO
17 ;# patch1: contributor Marc Lehmann added overloading and ref to tied items
18 ;# patch1: updated e-mail from Benjamin Holzman
20 ;# Revision 0.7 2000/08/03 22:04:44 ram
21 ;# Baseline for second beta release.
26 package Storable; @ISA = qw(Exporter DynaLoader);
28 @EXPORT = qw(store retrieve);
30 nstore store_fd nstore_fd retrieve_fd
36 use vars qw($forgive_me $VERSION);
39 *AUTOLOAD = \&AutoLoader::AUTOLOAD; # Grrr...
42 # Use of Log::Agent is optional
45 eval "use Log::Agent";
47 unless (defined @Log::Agent::EXPORT) {
58 # 8.3 limitation avoidance trickery. --mjtguy
59 sub retrieve_fd { goto &fdretrieve };
68 # Store target object hierarchy, identified by a reference to its root.
69 # The stored object tree may later be retrieved to memory via retrieve.
70 # Returns undef if an I/O error occurred, in which case the file is
74 return _store(\&pstore, @_);
80 # Same as store, but in network order.
83 return _store(\&net_pstore, @_);
86 # Internal store to file routine
91 logcroak "not a reference" unless ref($self);
92 logcroak "too many arguments" unless @_ == 1; # No @foo in arglist
94 open(FILE, ">$file") || logcroak "can't create $file: $!";
95 binmode FILE; # Archaic systems...
96 my $da = $@; # Don't mess if called from exception handler
98 # Call C routine nstore or pstore, depending on network order
99 eval { $ret = &$xsptr(*FILE, $self) };
100 close(FILE) or $ret = undef;
101 unlink($file) or warn "Can't unlink $file: $!\n" if $@ || !defined $ret;
102 logcroak $@ if $@ =~ s/\.?\n$/,/;
104 return $ret ? $ret : undef;
110 # Same as store, but perform on an already opened file descriptor instead.
111 # Returns undef if an I/O error occurred.
114 return _store_fd(\&pstore, @_);
120 # Same as store_fd, but in network order.
123 my ($self, $file) = @_;
124 return _store_fd(\&net_pstore, @_);
127 # Internal store routine on opened file descriptor
132 logcroak "not a reference" unless ref($self);
133 logcroak "too many arguments" unless @_ == 1; # No @foo in arglist
134 my $fd = fileno($file);
135 logcroak "not a valid file descriptor" unless defined $fd;
136 my $da = $@; # Don't mess if called from exception handler
138 # Call C routine nstore or pstore, depending on network order
139 eval { $ret = &$xsptr($file, $self) };
140 logcroak $@ if $@ =~ s/\.?\n$/,/;
142 return $ret ? $ret : undef;
148 # Store oject and its hierarchy in memory and return a scalar
149 # containing the result.
152 _freeze(\&mstore, @_);
158 # Same as freeze but in network order.
161 _freeze(\&net_mstore, @_);
164 # Internal freeze routine
168 logcroak "not a reference" unless ref($self);
169 logcroak "too many arguments" unless @_ == 0; # No @foo in arglist
170 my $da = $@; # Don't mess if called from exception handler
172 # Call C routine mstore or net_mstore, depending on network order
173 eval { $ret = &$xsptr($self) };
174 logcroak $@ if $@ =~ s/\.?\n$/,/;
176 return $ret ? $ret : undef;
182 # Retrieve object hierarchy from disk, returning a reference to the root
183 # object of that tree.
188 open(FILE, "$file") || logcroak "can't open $file: $!";
189 binmode FILE; # Archaic systems...
191 my $da = $@; # Could be from exception handler
192 eval { $self = pretrieve(*FILE) }; # Call C routine
194 logcroak $@ if $@ =~ s/\.?\n$/,/;
202 # Same as retrieve, but perform from an already opened file descriptor instead.
206 my $fd = fileno($file);
207 logcroak "not a valid file descriptor" unless defined $fd;
209 my $da = $@; # Could be from exception handler
210 eval { $self = pretrieve($file) }; # Call C routine
211 logcroak $@ if $@ =~ s/\.?\n$/,/;
219 # Recreate objects in memory from an existing frozen image created
220 # by freeze. If the frozen image passed is undef, return undef.
224 return undef unless defined $frozen;
226 my $da = $@; # Could be from exception handler
227 eval { $self = mretrieve($frozen) }; # Call C routine
228 logcroak $@ if $@ =~ s/\.?\n$/,/;
235 Storable - persistency for perl data structures
240 store \%table, 'file';
241 $hashref = retrieve('file');
243 use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);
246 nstore \%table, 'file';
247 $hashref = retrieve('file'); # There is NO nretrieve()
249 # Storing to and retrieving from an already opened file
250 store_fd \@array, \*STDOUT;
251 nstore_fd \%table, \*STDOUT;
252 $aryref = retrieve_fd(\*SOCKET);
253 $hashref = retrieve_fd(\*SOCKET);
255 # Serializing to memory
256 $serialized = freeze \%table;
257 %table_clone = %{ thaw($serialized) };
259 # Deep (recursive) cloning
260 $cloneref = dclone($ref);
264 The Storable package brings persistency to your perl data structures
265 containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be
266 convenientely stored to disk and retrieved at a later time.
268 It can be used in the regular procedural way by calling C<store> with
269 a reference to the object to be stored, along with the file name where
270 the image should be written.
271 The routine returns C<undef> for I/O problems or other internal error,
272 a true value otherwise. Serious errors are propagated as a C<die> exception.
274 To retrieve data stored to disk, use C<retrieve> with a file name,
275 and the objects stored into that file are recreated into memory for you,
276 a I<reference> to the root object being returned. In case an I/O error
277 occurs while reading, C<undef> is returned instead. Other serious
278 errors are propagated via C<die>.
280 Since storage is performed recursively, you might want to stuff references
281 to objects that share a lot of common data into a single array or hash
282 table, and then store that object. That way, when you retrieve back the
283 whole thing, the objects will continue to share what they originally shared.
285 At the cost of a slight header overhead, you may store to an already
286 opened file descriptor using the C<store_fd> routine, and retrieve
287 from a file via C<retrieve_fd>. Those names aren't imported by default,
288 so you will have to do that explicitely if you need those routines.
289 The file descriptor you supply must be already opened, for read
290 if you're going to retrieve and for write if you wish to store.
292 store_fd(\%table, *STDOUT) || die "can't store to stdout\n";
293 $hashref = retrieve_fd(*STDIN);
295 You can also store data in network order to allow easy sharing across
296 multiple platforms, or when storing on a socket known to be remotely
297 connected. The routines to call have an initial C<n> prefix for I<network>,
298 as in C<nstore> and C<nstore_fd>. At retrieval time, your data will be
299 correctly restored so you don't have to know whether you're restoring
300 from native or network ordered data.
302 When using C<retrieve_fd>, objects are retrieved in sequence, one
303 object (i.e. one recursive tree) per associated C<store_fd>.
305 If you're more from the object-oriented camp, you can inherit from
306 Storable and directly store your objects by invoking C<store> as
307 a method. The fact that the root of the to-be-stored tree is a
308 blessed reference (i.e. an object) is special-cased so that the
309 retrieve does not provide a reference to that object but rather the
310 blessed object reference itself. (Otherwise, you'd get a reference
311 to that blessed object).
315 The Storable engine can also store data into a Perl scalar instead, to
316 later retrieve them. This is mainly used to freeze a complex structure in
317 some safe compact memory place (where it can possibly be sent to another
318 process via some IPC, since freezing the structure also serializes it in
319 effect). Later on, and maybe somewhere else, you can thaw the Perl scalar
320 out and recreate the original complex structure in memory.
322 Surprisingly, the routines to be called are named C<freeze> and C<thaw>.
323 If you wish to send out the frozen scalar to another machine, use
324 C<nfreeze> instead to get a portable image.
326 Note that freezing an object structure and immediately thawing it
327 actually achieves a deep cloning of that structure:
329 dclone(.) = thaw(freeze(.))
331 Storable provides you with a C<dclone> interface which does not create
332 that intermediary scalar but instead freezes the structure in some
333 internal memory space and then immediatly thaws it out.
337 The heart of Storable is written in C for decent speed. Extra low-level
338 optimization have been made when manipulating perl internals, to
339 sacrifice encapsulation for the benefit of a greater speed.
341 =head1 CANONICAL REPRESENTATION
343 Normally Storable stores elements of hashes in the order they are
344 stored internally by Perl, i.e. pseudo-randomly. If you set
345 C<$Storable::canonical> to some C<TRUE> value, Storable will store
346 hashes with the elements sorted by their key. This allows you to
347 compare data structures by comparing their frozen representations (or
348 even the compressed frozen representations), which can be useful for
349 creating lookup tables for complicated queries.
351 Canonical order does not imply network order, those are two orthogonal
354 =head1 ERROR REPORTING
356 Storable uses the "exception" paradigm, in that it does not try to workaround
357 failures: if something bad happens, an exception is generated from the
358 caller's perspective (see L<Carp> and C<croak()>). Use eval {} to trap
361 When Storable croaks, it tries to report the error via the C<logcroak()>
362 routine from the C<Log::Agent> package, if it is available.
368 Any class may define hooks that will be called during the serialization
369 and deserialization process on objects that are instances of that class.
370 Those hooks can redefine the way serialization is performed (and therefore,
371 how the symetrical deserialization should be conducted).
373 Since we said earlier:
375 dclone(.) = thaw(freeze(.))
377 everything we say about hooks should also hold for deep cloning. However,
378 hooks get to know whether the operation is a mere serialization, or a cloning.
380 Therefore, when serializing hooks are involved,
382 dclone(.) <> thaw(freeze(.))
384 Well, you could keep them in sync, but there's no guarantee it will always
385 hold on classes somebody else wrote. Besides, there is little to gain in
386 doing so: a serializing hook could only keep one attribute of an object,
387 which is probably not what should happen during a deep cloning of that
390 Here is the hooking interface:
394 =item C<STORABLE_freeze> I<obj>, I<cloning>
396 The serializing hook, called on the object during serialization. It can be
397 inherited, or defined in the class itself, like any other method.
399 Arguments: I<obj> is the object to serialize, I<cloning> is a flag indicating
400 whether we're in a dclone() or a regular serialization via store() or freeze().
402 Returned value: A LIST C<($serialized, $ref1, $ref2, ...)> where $serialized
403 is the serialized form to be used, and the optional $ref1, $ref2, etc... are
404 extra references that you wish to let the Storable engine serialize.
406 At deserialization time, you will be given back the same LIST, but all the
407 extra references will be pointing into the deserialized structure.
409 The B<first time> the hook is hit in a serialization flow, you may have it
410 return an empty list. That will signal the Storable engine to further
411 discard that hook for this class and to therefore revert to the default
412 serialization of the underlying Perl data. The hook will again be normally
413 processed in the next serialization.
415 Unless you know better, serializing hook should always say:
417 sub STORABLE_freeze {
418 my ($self, $cloning) = @_;
419 return if $cloning; # Regular default serialization
423 in order to keep reasonable dclone() semantics.
425 =item C<STORABLE_thaw> I<obj>, I<cloning>, I<serialized>, ...
427 The deserializing hook called on the object during deserialization.
428 But wait. If we're deserializing, there's no object yet... right?
430 Wrong: the Storable engine creates an empty one for you. If you know Eiffel,
431 you can view C<STORABLE_thaw> as an alternate creation routine.
433 This means the hook can be inherited like any other method, and that
434 I<obj> is your blessed reference for this particular instance.
436 The other arguments should look familiar if you know C<STORABLE_freeze>:
437 I<cloning> is true when we're part of a deep clone operation, I<serialized>
438 is the serialized string you returned to the engine in C<STORABLE_freeze>,
439 and there may be an optional list of references, in the same order you gave
440 them at serialization time, pointing to the deserialized objects (which
441 have been processed courtesy of the Storable engine).
443 It is up to you to use these information to populate I<obj> the way you want.
445 Returned value: none.
451 Predicates are not exportable. They must be called by explicitely prefixing
452 them with the Storable package name.
456 =item C<Storable::last_op_in_netorder>
458 The C<Storable::last_op_in_netorder()> predicate will tell you whether
459 network order was used in the last store or retrieve operation. If you
460 don't know how to use this, just forget about it.
462 =item C<Storable::is_storing>
464 Returns true if within a store operation (via STORABLE_freeze hook).
466 =item C<Storable::is_retrieving>
468 Returns true if within a retrieve operation, (via STORABLE_thaw hook).
474 With hooks comes the ability to recurse back to the Storable engine. Indeed,
475 hooks are regular Perl code, and Storable is convenient when it comes to
476 serialize and deserialize things, so why not use it to handle the
477 serialization string?
479 There are a few things you need to know however:
485 You can create endless loops if the things you serialize via freeze()
486 (for instance) point back to the object we're trying to serialize in the hook.
490 Shared references among objects will not stay shared: if we're serializing
491 the list of object [A, C] where both object A and C refer to the SAME object
492 B, and if there is a serializing hook in A that says freeze(B), then when
493 deserializing, we'll get [A', C'] where A' refers to B', but C' refers to D,
494 a deep clone of B'. The topology was not preserved.
498 That's why C<STORABLE_freeze> lets you provide a list of references
499 to serialize. The engine guarantees that those will be serialized in the
500 same context as the other objects, and therefore that shared objects will
503 In the above [A, C] example, the C<STORABLE_freeze> hook could return:
505 ("something", $self->{B})
507 and the B part would be serialized by the engine. In C<STORABLE_thaw>, you
508 would get back the reference to the B' object, deserialized for you.
510 Therefore, recursion should normally be avoided, but is nonetheless supported.
514 There is a new Clone module available on CPAN which implements deep cloning
515 natively, i.e. without freezing to memory and thawing the result. It is
516 aimed to replace Storable's dclone() some day. However, it does not currently
517 support Storable hooks to redefine the way deep cloning is performed.
521 Here are some code samples showing a possible usage of Storable:
523 use Storable qw(store retrieve freeze thaw dclone);
525 %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1);
527 store(\%color, '/tmp/colors') or die "Can't store %a in /tmp/colors!\n";
529 $colref = retrieve('/tmp/colors');
530 die "Unable to retrieve from /tmp/colors!\n" unless defined $colref;
531 printf "Blue is still %lf\n", $colref->{'Blue'};
533 $colref2 = dclone(\%color);
535 $str = freeze(\%color);
536 printf "Serialization of %%color is %d bytes long.\n", length($str);
537 $colref3 = thaw($str);
539 which prints (on my machine):
541 Blue is still 0.100000
542 Serialization of %color is 102 bytes long.
546 If you're using references as keys within your hash tables, you're bound
547 to disapointment when retrieving your data. Indeed, Perl stringifies
548 references used as hash table keys. If you later wish to access the
549 items via another reference stringification (i.e. using the same
550 reference that was used for the key originally to record the value into
551 the hash table), it will work because both references stringify to the
554 It won't work across a C<store> and C<retrieve> operations however, because
555 the addresses in the retrieved objects, which are part of the stringified
556 references, will probably differ from the original addresses. The
557 topology of your structure is preserved, but not hidden semantics
560 On platforms where it matters, be sure to call C<binmode()> on the
561 descriptors that you pass to Storable functions.
563 Storing data canonically that contains large hashes can be
564 significantly slower than storing the same data normally, as
565 temprorary arrays to hold the keys for each hash have to be allocated,
566 populated, sorted and freed. Some tests have shown a halving of the
567 speed of storing -- the exact penalty will depend on the complexity of
568 your data. There is no slowdown on retrieval.
572 You can't store GLOB, CODE, FORMLINE, etc... If you can define
573 semantics for those operations, feel free to enhance Storable so that
574 it can deal with them.
576 The store functions will C<croak> if they run into such references
577 unless you set C<$Storable::forgive_me> to some C<TRUE> value. In that
578 case, the fatal message is turned in a warning and some
579 meaningless string is stored instead.
581 Setting C<$Storable::canonical> may not yield frozen strings that
582 compare equal due to possible stringification of numbers. When the
583 string version of a scalar exists, it is the form stored, therefore
584 if you happen to use your numbers as strings between two freezing
585 operations on the same data structures, you will get different
588 Due to the aforementionned optimizations, Storable is at the mercy
589 of perl's internal redesign or structure changes. If that bothers
590 you, you can try convincing Larry that what is used in Storable
591 should be documented and consistently kept in future revisions.
595 Thank you to (in chronological order):
597 Jarkko Hietaniemi <jhi@iki.fi>
598 Ulrich Pfeifer <pfeifer@charly.informatik.uni-dortmund.de>
599 Benjamin A. Holzman <bah@ecnvantage.com>
600 Andrew Ford <A.Ford@ford-mason.co.uk>
601 Gisle Aas <gisle@aas.no>
602 Jeff Gresham <gresham_jeffrey@jpmorgan.com>
603 Murray Nesbitt <murray@activestate.com>
604 Marc Lehmann <pcg@opengroup.org>
606 for their bug reports, suggestions and contributions.
608 Benjamin Holzman contributed the tied variable support, Andrew Ford
609 contributed the canonical order for hashes, and Gisle Aas fixed
610 a few misunderstandings of mine regarding the Perl internals,
611 and optimized the emission of "tags" in the output streams by
612 simply counting the objects instead of tagging them (leading to
613 a binary incompatibility for the Storable image starting at version
614 0.6--older images are of course still properly understood).
615 Murray Nesbitt made Storable thread-safe. Marc Lehmann added overloading
616 and reference to tied items support.
620 There is a Japanese translation of this man page available at
621 http://member.nifty.ne.jp/hippo2000/perltips/storable.htm ,
622 courtesy of Kawai, Takanori <kawai@nippon-rad.co.jp>.
626 Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>