This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add install-silent target.
[perl5.git] / ext / Storable / Storable.pm
CommitLineData
f0ffaed8 1;# $Id: Storable.pm,v 0.7.1.3 2000/08/23 22:49:25 ram Exp $
7a6a85bf
RG
2;#
3;# Copyright (c) 1995-2000, Raphael Manfredi
4;#
5;# You may redistribute only under the terms of the Artistic License,
6;# as specified in the README file that comes with the distribution.
7;#
8;# $Log: Storable.pm,v $
f0ffaed8
JH
9;# Revision 0.7.1.3 2000/08/23 22:49:25 ram
10;# patch3: updated version number
11;#
7a6a85bf
RG
12;# Revision 0.7.1.2 2000/08/14 07:18:40 ram
13;# patch2: increased version number
14;#
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
19;#
20;# Revision 0.7 2000/08/03 22:04:44 ram
21;# Baseline for second beta release.
22;#
23
24require DynaLoader;
25require Exporter;
26package Storable; @ISA = qw(Exporter DynaLoader);
27
28@EXPORT = qw(store retrieve);
29@EXPORT_OK = qw(
30 nstore store_fd nstore_fd retrieve_fd
31 freeze nfreeze thaw
32 dclone
33);
34
35use AutoLoader;
36use vars qw($forgive_me $VERSION);
37
f0ffaed8 38$VERSION = '0.703';
7a6a85bf
RG
39*AUTOLOAD = \&AutoLoader::AUTOLOAD; # Grrr...
40
41#
42# Use of Log::Agent is optional
43#
44
45eval "use Log::Agent";
46
47unless (defined @Log::Agent::EXPORT) {
48 eval q{
49 sub logcroak {
50 require Carp;
51 Carp::croak(@_);
52 }
53 };
54}
55
56sub logcroak;
57
58bootstrap Storable;
591;
60__END__
61
62#
63# store
64#
65# Store target object hierarchy, identified by a reference to its root.
66# The stored object tree may later be retrieved to memory via retrieve.
67# Returns undef if an I/O error occurred, in which case the file is
68# removed.
69#
70sub store {
71 return _store(\&pstore, @_);
72}
73
74#
75# nstore
76#
77# Same as store, but in network order.
78#
79sub nstore {
80 return _store(\&net_pstore, @_);
81}
82
83# Internal store to file routine
84sub _store {
85 my $xsptr = shift;
86 my $self = shift;
87 my ($file) = @_;
88 logcroak "not a reference" unless ref($self);
89 logcroak "too many arguments" unless @_ == 1; # No @foo in arglist
90 local *FILE;
91 open(FILE, ">$file") || logcroak "can't create $file: $!";
92 binmode FILE; # Archaic systems...
93 my $da = $@; # Don't mess if called from exception handler
94 my $ret;
95 # Call C routine nstore or pstore, depending on network order
96 eval { $ret = &$xsptr(*FILE, $self) };
97 close(FILE) or $ret = undef;
98 unlink($file) or warn "Can't unlink $file: $!\n" if $@ || !defined $ret;
99 logcroak $@ if $@ =~ s/\.?\n$/,/;
100 $@ = $da;
101 return $ret ? $ret : undef;
102}
103
104#
105# store_fd
106#
107# Same as store, but perform on an already opened file descriptor instead.
108# Returns undef if an I/O error occurred.
109#
110sub store_fd {
111 return _store_fd(\&pstore, @_);
112}
113
114#
115# nstore_fd
116#
117# Same as store_fd, but in network order.
118#
119sub nstore_fd {
120 my ($self, $file) = @_;
121 return _store_fd(\&net_pstore, @_);
122}
123
124# Internal store routine on opened file descriptor
125sub _store_fd {
126 my $xsptr = shift;
127 my $self = shift;
128 my ($file) = @_;
129 logcroak "not a reference" unless ref($self);
130 logcroak "too many arguments" unless @_ == 1; # No @foo in arglist
131 my $fd = fileno($file);
132 logcroak "not a valid file descriptor" unless defined $fd;
133 my $da = $@; # Don't mess if called from exception handler
134 my $ret;
135 # Call C routine nstore or pstore, depending on network order
136 eval { $ret = &$xsptr($file, $self) };
137 logcroak $@ if $@ =~ s/\.?\n$/,/;
138 $@ = $da;
139 return $ret ? $ret : undef;
140}
141
142#
143# freeze
144#
145# Store oject and its hierarchy in memory and return a scalar
146# containing the result.
147#
148sub freeze {
149 _freeze(\&mstore, @_);
150}
151
152#
153# nfreeze
154#
155# Same as freeze but in network order.
156#
157sub nfreeze {
158 _freeze(\&net_mstore, @_);
159}
160
161# Internal freeze routine
162sub _freeze {
163 my $xsptr = shift;
164 my $self = shift;
165 logcroak "not a reference" unless ref($self);
166 logcroak "too many arguments" unless @_ == 0; # No @foo in arglist
167 my $da = $@; # Don't mess if called from exception handler
168 my $ret;
169 # Call C routine mstore or net_mstore, depending on network order
170 eval { $ret = &$xsptr($self) };
171 logcroak $@ if $@ =~ s/\.?\n$/,/;
172 $@ = $da;
173 return $ret ? $ret : undef;
174}
175
176#
177# retrieve
178#
179# Retrieve object hierarchy from disk, returning a reference to the root
180# object of that tree.
181#
182sub retrieve {
183 my ($file) = @_;
184 local *FILE;
185 open(FILE, "$file") || logcroak "can't open $file: $!";
186 binmode FILE; # Archaic systems...
187 my $self;
188 my $da = $@; # Could be from exception handler
189 eval { $self = pretrieve(*FILE) }; # Call C routine
190 close(FILE);
191 logcroak $@ if $@ =~ s/\.?\n$/,/;
192 $@ = $da;
193 return $self;
194}
195
196#
197# retrieve_fd
198#
199# Same as retrieve, but perform from an already opened file descriptor instead.
200#
201sub retrieve_fd {
202 my ($file) = @_;
203 my $fd = fileno($file);
204 logcroak "not a valid file descriptor" unless defined $fd;
205 my $self;
206 my $da = $@; # Could be from exception handler
207 eval { $self = pretrieve($file) }; # Call C routine
208 logcroak $@ if $@ =~ s/\.?\n$/,/;
209 $@ = $da;
210 return $self;
211}
212
213#
214# thaw
215#
216# Recreate objects in memory from an existing frozen image created
217# by freeze. If the frozen image passed is undef, return undef.
218#
219sub thaw {
220 my ($frozen) = @_;
221 return undef unless defined $frozen;
222 my $self;
223 my $da = $@; # Could be from exception handler
224 eval { $self = mretrieve($frozen) }; # Call C routine
225 logcroak $@ if $@ =~ s/\.?\n$/,/;
226 $@ = $da;
227 return $self;
228}
229
230=head1 NAME
231
232Storable - persistency for perl data structures
233
234=head1 SYNOPSIS
235
236 use Storable;
237 store \%table, 'file';
238 $hashref = retrieve('file');
239
240 use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);
241
242 # Network order
243 nstore \%table, 'file';
244 $hashref = retrieve('file'); # There is NO nretrieve()
245
246 # Storing to and retrieving from an already opened file
247 store_fd \@array, \*STDOUT;
248 nstore_fd \%table, \*STDOUT;
249 $aryref = retrieve_fd(\*SOCKET);
250 $hashref = retrieve_fd(\*SOCKET);
251
252 # Serializing to memory
253 $serialized = freeze \%table;
254 %table_clone = %{ thaw($serialized) };
255
256 # Deep (recursive) cloning
257 $cloneref = dclone($ref);
258
259=head1 DESCRIPTION
260
261The Storable package brings persistency to your perl data structures
262containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be
263convenientely stored to disk and retrieved at a later time.
264
265It can be used in the regular procedural way by calling C<store> with
266a reference to the object to be stored, along with the file name where
267the image should be written.
268The routine returns C<undef> for I/O problems or other internal error,
269a true value otherwise. Serious errors are propagated as a C<die> exception.
270
271To retrieve data stored to disk, use C<retrieve> with a file name,
272and the objects stored into that file are recreated into memory for you,
273a I<reference> to the root object being returned. In case an I/O error
274occurs while reading, C<undef> is returned instead. Other serious
275errors are propagated via C<die>.
276
277Since storage is performed recursively, you might want to stuff references
278to objects that share a lot of common data into a single array or hash
279table, and then store that object. That way, when you retrieve back the
280whole thing, the objects will continue to share what they originally shared.
281
282At the cost of a slight header overhead, you may store to an already
283opened file descriptor using the C<store_fd> routine, and retrieve
284from a file via C<retrieve_fd>. Those names aren't imported by default,
285so you will have to do that explicitely if you need those routines.
286The file descriptor you supply must be already opened, for read
287if you're going to retrieve and for write if you wish to store.
288
289 store_fd(\%table, *STDOUT) || die "can't store to stdout\n";
290 $hashref = retrieve_fd(*STDIN);
291
292You can also store data in network order to allow easy sharing across
293multiple platforms, or when storing on a socket known to be remotely
294connected. The routines to call have an initial C<n> prefix for I<network>,
295as in C<nstore> and C<nstore_fd>. At retrieval time, your data will be
296correctly restored so you don't have to know whether you're restoring
297from native or network ordered data.
298
299When using C<retrieve_fd>, objects are retrieved in sequence, one
300object (i.e. one recursive tree) per associated C<store_fd>.
301
302If you're more from the object-oriented camp, you can inherit from
303Storable and directly store your objects by invoking C<store> as
304a method. The fact that the root of the to-be-stored tree is a
305blessed reference (i.e. an object) is special-cased so that the
306retrieve does not provide a reference to that object but rather the
307blessed object reference itself. (Otherwise, you'd get a reference
308to that blessed object).
309
310=head1 MEMORY STORE
311
312The Storable engine can also store data into a Perl scalar instead, to
313later retrieve them. This is mainly used to freeze a complex structure in
314some safe compact memory place (where it can possibly be sent to another
315process via some IPC, since freezing the structure also serializes it in
316effect). Later on, and maybe somewhere else, you can thaw the Perl scalar
317out and recreate the original complex structure in memory.
318
319Surprisingly, the routines to be called are named C<freeze> and C<thaw>.
320If you wish to send out the frozen scalar to another machine, use
321C<nfreeze> instead to get a portable image.
322
323Note that freezing an object structure and immediately thawing it
324actually achieves a deep cloning of that structure:
325
326 dclone(.) = thaw(freeze(.))
327
328Storable provides you with a C<dclone> interface which does not create
329that intermediary scalar but instead freezes the structure in some
330internal memory space and then immediatly thaws it out.
331
332=head1 SPEED
333
334The heart of Storable is written in C for decent speed. Extra low-level
335optimization have been made when manipulating perl internals, to
336sacrifice encapsulation for the benefit of a greater speed.
337
338=head1 CANONICAL REPRESENTATION
339
340Normally Storable stores elements of hashes in the order they are
341stored internally by Perl, i.e. pseudo-randomly. If you set
342C<$Storable::canonical> to some C<TRUE> value, Storable will store
343hashes with the elements sorted by their key. This allows you to
344compare data structures by comparing their frozen representations (or
345even the compressed frozen representations), which can be useful for
346creating lookup tables for complicated queries.
347
348Canonical order does not imply network order, those are two orthogonal
349settings.
350
351=head1 ERROR REPORTING
352
353Storable uses the "exception" paradigm, in that it does not try to workaround
354failures: if something bad happens, an exception is generated from the
355caller's perspective (see L<Carp> and C<croak()>). Use eval {} to trap
356those exceptions.
357
358When Storable croaks, it tries to report the error via the C<logcroak()>
359routine from the C<Log::Agent> package, if it is available.
360
361=head1 WIZARDS ONLY
362
363=head2 Hooks
364
365Any class may define hooks that will be called during the serialization
366and deserialization process on objects that are instances of that class.
367Those hooks can redefine the way serialization is performed (and therefore,
368how the symetrical deserialization should be conducted).
369
370Since we said earlier:
371
372 dclone(.) = thaw(freeze(.))
373
374everything we say about hooks should also hold for deep cloning. However,
375hooks get to know whether the operation is a mere serialization, or a cloning.
376
377Therefore, when serializing hooks are involved,
378
379 dclone(.) <> thaw(freeze(.))
380
381Well, you could keep them in sync, but there's no guarantee it will always
382hold on classes somebody else wrote. Besides, there is little to gain in
383doing so: a serializing hook could only keep one attribute of an object,
384which is probably not what should happen during a deep cloning of that
385same object.
386
387Here is the hooking interface:
388
389=over
390
391=item C<STORABLE_freeze> I<obj>, I<cloning>
392
393The serializing hook, called on the object during serialization. It can be
394inherited, or defined in the class itself, like any other method.
395
396Arguments: I<obj> is the object to serialize, I<cloning> is a flag indicating
397whether we're in a dclone() or a regular serialization via store() or freeze().
398
399Returned value: A LIST C<($serialized, $ref1, $ref2, ...)> where $serialized
400is the serialized form to be used, and the optional $ref1, $ref2, etc... are
401extra references that you wish to let the Storable engine serialize.
402
403At deserialization time, you will be given back the same LIST, but all the
404extra references will be pointing into the deserialized structure.
405
406The B<first time> the hook is hit in a serialization flow, you may have it
407return an empty list. That will signal the Storable engine to further
408discard that hook for this class and to therefore revert to the default
409serialization of the underlying Perl data. The hook will again be normally
410processed in the next serialization.
411
412Unless you know better, serializing hook should always say:
413
414 sub STORABLE_freeze {
415 my ($self, $cloning) = @_;
416 return if $cloning; # Regular default serialization
417 ....
418 }
419
420in order to keep reasonable dclone() semantics.
421
422=item C<STORABLE_thaw> I<obj>, I<cloning>, I<serialized>, ...
423
424The deserializing hook called on the object during deserialization.
425But wait. If we're deserializing, there's no object yet... right?
426
427Wrong: the Storable engine creates an empty one for you. If you know Eiffel,
428you can view C<STORABLE_thaw> as an alternate creation routine.
429
430This means the hook can be inherited like any other method, and that
431I<obj> is your blessed reference for this particular instance.
432
433The other arguments should look familiar if you know C<STORABLE_freeze>:
434I<cloning> is true when we're part of a deep clone operation, I<serialized>
435is the serialized string you returned to the engine in C<STORABLE_freeze>,
436and there may be an optional list of references, in the same order you gave
437them at serialization time, pointing to the deserialized objects (which
438have been processed courtesy of the Storable engine).
439
440It is up to you to use these information to populate I<obj> the way you want.
441
442Returned value: none.
443
444=back
445
446=head2 Predicates
447
448Predicates are not exportable. They must be called by explicitely prefixing
449them with the Storable package name.
450
451=over
452
453=item C<Storable::last_op_in_netorder>
454
455The C<Storable::last_op_in_netorder()> predicate will tell you whether
456network order was used in the last store or retrieve operation. If you
457don't know how to use this, just forget about it.
458
459=item C<Storable::is_storing>
460
461Returns true if within a store operation (via STORABLE_freeze hook).
462
463=item C<Storable::is_retrieving>
464
465Returns true if within a retrieve operation, (via STORABLE_thaw hook).
466
467=back
468
469=head2 Recursion
470
471With hooks comes the ability to recurse back to the Storable engine. Indeed,
472hooks are regular Perl code, and Storable is convenient when it comes to
473serialize and deserialize things, so why not use it to handle the
474serialization string?
475
476There are a few things you need to know however:
477
478=over
479
480=item *
481
482You can create endless loops if the things you serialize via freeze()
483(for instance) point back to the object we're trying to serialize in the hook.
484
485=item *
486
487Shared references among objects will not stay shared: if we're serializing
488the list of object [A, C] where both object A and C refer to the SAME object
489B, and if there is a serializing hook in A that says freeze(B), then when
490deserializing, we'll get [A', C'] where A' refers to B', but C' refers to D,
491a deep clone of B'. The topology was not preserved.
492
493=back
494
495That's why C<STORABLE_freeze> lets you provide a list of references
496to serialize. The engine guarantees that those will be serialized in the
497same context as the other objects, and therefore that shared objects will
498stay shared.
499
500In the above [A, C] example, the C<STORABLE_freeze> hook could return:
501
502 ("something", $self->{B})
503
504and the B part would be serialized by the engine. In C<STORABLE_thaw>, you
505would get back the reference to the B' object, deserialized for you.
506
507Therefore, recursion should normally be avoided, but is nonetheless supported.
508
509=head2 Deep Cloning
510
511There is a new Clone module available on CPAN which implements deep cloning
512natively, i.e. without freezing to memory and thawing the result. It is
513aimed to replace Storable's dclone() some day. However, it does not currently
514support Storable hooks to redefine the way deep cloning is performed.
515
516=head1 EXAMPLES
517
518Here are some code samples showing a possible usage of Storable:
519
520 use Storable qw(store retrieve freeze thaw dclone);
521
522 %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1);
523
524 store(\%color, '/tmp/colors') or die "Can't store %a in /tmp/colors!\n";
525
526 $colref = retrieve('/tmp/colors');
527 die "Unable to retrieve from /tmp/colors!\n" unless defined $colref;
528 printf "Blue is still %lf\n", $colref->{'Blue'};
529
530 $colref2 = dclone(\%color);
531
532 $str = freeze(\%color);
533 printf "Serialization of %%color is %d bytes long.\n", length($str);
534 $colref3 = thaw($str);
535
536which prints (on my machine):
537
538 Blue is still 0.100000
539 Serialization of %color is 102 bytes long.
540
541=head1 WARNING
542
543If you're using references as keys within your hash tables, you're bound
544to disapointment when retrieving your data. Indeed, Perl stringifies
545references used as hash table keys. If you later wish to access the
546items via another reference stringification (i.e. using the same
547reference that was used for the key originally to record the value into
548the hash table), it will work because both references stringify to the
549same string.
550
551It won't work across a C<store> and C<retrieve> operations however, because
552the addresses in the retrieved objects, which are part of the stringified
553references, will probably differ from the original addresses. The
554topology of your structure is preserved, but not hidden semantics
555like those.
556
557On platforms where it matters, be sure to call C<binmode()> on the
558descriptors that you pass to Storable functions.
559
560Storing data canonically that contains large hashes can be
561significantly slower than storing the same data normally, as
562temprorary arrays to hold the keys for each hash have to be allocated,
563populated, sorted and freed. Some tests have shown a halving of the
564speed of storing -- the exact penalty will depend on the complexity of
565your data. There is no slowdown on retrieval.
566
567=head1 BUGS
568
569You can't store GLOB, CODE, FORMLINE, etc... If you can define
570semantics for those operations, feel free to enhance Storable so that
571it can deal with them.
572
573The store functions will C<croak> if they run into such references
574unless you set C<$Storable::forgive_me> to some C<TRUE> value. In that
575case, the fatal message is turned in a warning and some
576meaningless string is stored instead.
577
578Setting C<$Storable::canonical> may not yield frozen strings that
579compare equal due to possible stringification of numbers. When the
580string version of a scalar exists, it is the form stored, therefore
581if you happen to use your numbers as strings between two freezing
582operations on the same data structures, you will get different
583results.
584
585Due to the aforementionned optimizations, Storable is at the mercy
586of perl's internal redesign or structure changes. If that bothers
587you, you can try convincing Larry that what is used in Storable
588should be documented and consistently kept in future revisions.
589
590=head1 CREDITS
591
592Thank you to (in chronological order):
593
594 Jarkko Hietaniemi <jhi@iki.fi>
595 Ulrich Pfeifer <pfeifer@charly.informatik.uni-dortmund.de>
596 Benjamin A. Holzman <bah@ecnvantage.com>
597 Andrew Ford <A.Ford@ford-mason.co.uk>
598 Gisle Aas <gisle@aas.no>
599 Jeff Gresham <gresham_jeffrey@jpmorgan.com>
600 Murray Nesbitt <murray@activestate.com>
601 Marc Lehmann <pcg@opengroup.org>
602
603for their bug reports, suggestions and contributions.
604
605Benjamin Holzman contributed the tied variable support, Andrew Ford
606contributed the canonical order for hashes, and Gisle Aas fixed
607a few misunderstandings of mine regarding the Perl internals,
608and optimized the emission of "tags" in the output streams by
609simply counting the objects instead of tagging them (leading to
610a binary incompatibility for the Storable image starting at version
6110.6--older images are of course still properly understood).
612Murray Nesbitt made Storable thread-safe. Marc Lehmann added overloading
613and reference to tied items support.
614
615=head1 TRANSLATIONS
616
617There is a Japanese translation of this man page available at
618http://member.nifty.ne.jp/hippo2000/perltips/storable.htm ,
619courtesy of Kawai, Takanori <kawai@nippon-rad.co.jp>.
620
621=head1 AUTHOR
622
623Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
624
625=head1 SEE ALSO
626
627Clone(3).
628
629=cut
630