← Index
NYTProf Performance Profile   « block view • line view • sub view »
For ddd2.pl
  Run on Tue May 25 16:52:24 2010
Reported on Tue May 25 16:56:56 2010

File /project/perl/lib/Storable.pm
Statements Executed 29
Statement Execution Time 1.33ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
412772µs772µsStorable::::dcloneStorable::dclone (xsub)
112138µs138µsStorable::::bootstrapStorable::bootstrap (xsub)
0000s0sStorable::::CLONEStorable::CLONE
0000s0sStorable::::retrieve_fdStorable::retrieve_fd
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1#
2# Copyright (c) 1995-2000, Raphael Manfredi
3#
4# You may redistribute only under the same terms as Perl 5, as specified
5# in the README file that comes with the distribution.
6#
7
816µsrequire DynaLoader;
915µsrequire Exporter;
10110µspackage Storable; @ISA = qw(Exporter DynaLoader);
11
1215µs@EXPORT = qw(store retrieve);
1319µs@EXPORT_OK = qw(
14 nstore store_fd nstore_fd fd_retrieve
15 freeze nfreeze thaw
16 dclone
17 retrieve_fd
18 lock_store lock_nstore lock_retrieve
19 file_magic read_magic
20);
21
22393µs1595µsuse AutoLoader;
# spent 595µs making 1 call to AutoLoader::import
233335µs12.97msuse FileHandle;
# spent 2.97ms making 1 call to FileHandle::import
243249µs1276µsuse vars qw($canonical $forgive_me $VERSION);
# spent 276µs making 1 call to vars::import
25
2615µs$VERSION = '2.21';
2718µs*AUTOLOAD = \&AutoLoader::AUTOLOAD; # Grrr...
28
29#
30# Use of Log::Agent is optional
31#
32
33{
34394µs local $SIG{__DIE__};
351183µs eval "use Log::Agent";
36}
37
3817µsrequire Carp;
39
40#
41# They might miss :flock in Fcntl
42#
43
44BEGIN {
45347µs1807µs if (eval { require Fcntl; 1 } && exists $Fcntl::EXPORT_TAGS{'flock'}) {
# spent 807µs making 1 call to Exporter::import
46 Fcntl->import(':flock');
47 } else {
48 eval q{
49 sub LOCK_SH () {1}
50 sub LOCK_EX () {2}
51 };
52 }
531200µs}
54
55sub CLONE {
56 # clone context under threads
57 Storable::init_perinterp();
58}
59
60# Can't Autoload cleanly as this clashes 8.3 with &retrieve
61sub retrieve_fd { &fd_retrieve } # Backward compatibility
62
63# By default restricted hashes are downgraded on earlier perls.
64
6516µs$Storable::downgrade_restricted = 1;
6614µs$Storable::accept_future_minor = 1;
67136µs1879µsbootstrap Storable;
# spent 879µs making 1 call to DynaLoader::bootstrap
68133µs1;
69__END__
70#
71# Use of Log::Agent is optional. If it hasn't imported these subs then
72# Autoloader will kindly supply our fallback implementation.
73#
74
75sub logcroak {
76 Carp::croak(@_);
77}
78
79sub logcarp {
80 Carp::carp(@_);
81}
82
83#
84# Determine whether locking is possible, but only when needed.
85#
86
87sub CAN_FLOCK; my $CAN_FLOCK; sub CAN_FLOCK {
88 return $CAN_FLOCK if defined $CAN_FLOCK;
89 require Config; import Config;
90 return $CAN_FLOCK =
91 $Config{'d_flock'} ||
92 $Config{'d_fcntl_can_lock'} ||
93 $Config{'d_lockf'};
94}
95
96sub show_file_magic {
97 print <<EOM;
98#
99# To recognize the data files of the Perl module Storable,
100# the following lines need to be added to the local magic(5) file,
101# usually either /usr/share/misc/magic or /etc/magic.
102#
1030 string perl-store perl Storable(v0.6) data
104>4 byte >0 (net-order %d)
105>>4 byte &01 (network-ordered)
106>>4 byte =3 (major 1)
107>>4 byte =2 (major 1)
108
1090 string pst0 perl Storable(v0.7) data
110>4 byte >0
111>>4 byte &01 (network-ordered)
112>>4 byte =5 (major 2)
113>>4 byte =4 (major 2)
114>>5 byte >0 (minor %d)
115EOM
116}
117
118sub file_magic {
119 my $file = shift;
120 my $fh = new FileHandle;
121 open($fh, "<". $file) || die "Can't open '$file': $!";
122 binmode($fh);
123 defined(sysread($fh, my $buf, 32)) || die "Can't read from '$file': $!";
124 close($fh);
125
126 $file = "./$file" unless $file; # ensure TRUE value
127
128 return read_magic($buf, $file);
129}
130
131sub read_magic {
132 my($buf, $file) = @_;
133 my %info;
134
135 my $buflen = length($buf);
136 my $magic;
137 if ($buf =~ s/^(pst0|perl-store)//) {
138 $magic = $1;
139 $info{file} = $file || 1;
140 }
141 else {
142 return undef if $file;
143 $magic = "";
144 }
145
146 return undef unless length($buf);
147
148 my $net_order;
149 if ($magic eq "perl-store" && ord(substr($buf, 0, 1)) > 1) {
150 $info{version} = -1;
151 $net_order = 0;
152 }
153 else {
154 $net_order = ord(substr($buf, 0, 1, ""));
155 my $major = $net_order >> 1;
156 return undef if $major > 4; # sanity (assuming we never go that high)
157 $info{major} = $major;
158 $net_order &= 0x01;
159 if ($major > 1) {
160 return undef unless length($buf);
161 my $minor = ord(substr($buf, 0, 1, ""));
162 $info{minor} = $minor;
163 $info{version} = "$major.$minor";
164 $info{version_nv} = sprintf "%d.%03d", $major, $minor;
165 }
166 else {
167 $info{version} = $major;
168 }
169 }
170 $info{version_nv} ||= $info{version};
171 $info{netorder} = $net_order;
172
173 unless ($net_order) {
174 return undef unless length($buf);
175 my $len = ord(substr($buf, 0, 1, ""));
176 return undef unless length($buf) >= $len;
177 return undef unless $len == 4 || $len == 8; # sanity
178 $info{byteorder} = substr($buf, 0, $len, "");
179 $info{intsize} = ord(substr($buf, 0, 1, ""));
180 $info{longsize} = ord(substr($buf, 0, 1, ""));
181 $info{ptrsize} = ord(substr($buf, 0, 1, ""));
182 if ($info{version_nv} >= 2.002) {
183 return undef unless length($buf);
184 $info{nvsize} = ord(substr($buf, 0, 1, ""));
185 }
186 }
187 $info{hdrsize} = $buflen - length($buf);
188
189 return \%info;
190}
191
192sub BIN_VERSION_NV {
193 sprintf "%d.%03d", BIN_MAJOR(), BIN_MINOR();
194}
195
196sub BIN_WRITE_VERSION_NV {
197 sprintf "%d.%03d", BIN_MAJOR(), BIN_WRITE_MINOR();
198}
199
200#
201# store
202#
203# Store target object hierarchy, identified by a reference to its root.
204# The stored object tree may later be retrieved to memory via retrieve.
205# Returns undef if an I/O error occurred, in which case the file is
206# removed.
207#
208sub store {
209 return _store(\&pstore, @_, 0);
210}
211
212#
213# nstore
214#
215# Same as store, but in network order.
216#
217sub nstore {
218 return _store(\&net_pstore, @_, 0);
219}
220
221#
222# lock_store
223#
224# Same as store, but flock the file first (advisory locking).
225#
226sub lock_store {
227 return _store(\&pstore, @_, 1);
228}
229
230#
231# lock_nstore
232#
233# Same as nstore, but flock the file first (advisory locking).
234#
235sub lock_nstore {
236 return _store(\&net_pstore, @_, 1);
237}
238
239# Internal store to file routine
240sub _store {
241 my $xsptr = shift;
242 my $self = shift;
243 my ($file, $use_locking) = @_;
244 logcroak "not a reference" unless ref($self);
245 logcroak "wrong argument number" unless @_ == 2; # No @foo in arglist
246 local *FILE;
247 if ($use_locking) {
248 open(FILE, ">>$file") || logcroak "can't write into $file: $!";
249 unless (&CAN_FLOCK) {
250 logcarp "Storable::lock_store: fcntl/flock emulation broken on $^O";
251 return undef;
252 }
253 flock(FILE, LOCK_EX) ||
254 logcroak "can't get exclusive lock on $file: $!";
255 truncate FILE, 0;
256 # Unlocking will happen when FILE is closed
257 } else {
258 open(FILE, ">$file") || logcroak "can't create $file: $!";
259 }
260 binmode FILE; # Archaic systems...
261 my $da = $@; # Don't mess if called from exception handler
262 my $ret;
263 # Call C routine nstore or pstore, depending on network order
264 eval { $ret = &$xsptr(*FILE, $self) };
265 close(FILE) or $ret = undef;
266 unlink($file) or warn "Can't unlink $file: $!\n" if $@ || !defined $ret;
267 logcroak $@ if $@ =~ s/\.?\n$/,/;
268 $@ = $da;
269 return $ret ? $ret : undef;
270}
271
272#
273# store_fd
274#
275# Same as store, but perform on an already opened file descriptor instead.
276# Returns undef if an I/O error occurred.
277#
278sub store_fd {
279 return _store_fd(\&pstore, @_);
280}
281
282#
283# nstore_fd
284#
285# Same as store_fd, but in network order.
286#
287sub nstore_fd {
288 my ($self, $file) = @_;
289 return _store_fd(\&net_pstore, @_);
290}
291
292# Internal store routine on opened file descriptor
293sub _store_fd {
294 my $xsptr = shift;
295 my $self = shift;
296 my ($file) = @_;
297 logcroak "not a reference" unless ref($self);
298 logcroak "too many arguments" unless @_ == 1; # No @foo in arglist
299 my $fd = fileno($file);
300 logcroak "not a valid file descriptor" unless defined $fd;
301 my $da = $@; # Don't mess if called from exception handler
302 my $ret;
303 # Call C routine nstore or pstore, depending on network order
304 eval { $ret = &$xsptr($file, $self) };
305 logcroak $@ if $@ =~ s/\.?\n$/,/;
306 local $\; print $file ''; # Autoflush the file if wanted
307 $@ = $da;
308 return $ret ? $ret : undef;
309}
310
311#
312# freeze
313#
314# Store oject and its hierarchy in memory and return a scalar
315# containing the result.
316#
317sub freeze {
318 _freeze(\&mstore, @_);
319}
320
321#
322# nfreeze
323#
324# Same as freeze but in network order.
325#
326sub nfreeze {
327 _freeze(\&net_mstore, @_);
328}
329
330# Internal freeze routine
331sub _freeze {
332 my $xsptr = shift;
333 my $self = shift;
334 logcroak "not a reference" unless ref($self);
335 logcroak "too many arguments" unless @_ == 0; # No @foo in arglist
336 my $da = $@; # Don't mess if called from exception handler
337 my $ret;
338 # Call C routine mstore or net_mstore, depending on network order
339 eval { $ret = &$xsptr($self) };
340 logcroak $@ if $@ =~ s/\.?\n$/,/;
341 $@ = $da;
342 return $ret ? $ret : undef;
343}
344
345#
346# retrieve
347#
348# Retrieve object hierarchy from disk, returning a reference to the root
349# object of that tree.
350#
351sub retrieve {
352 _retrieve($_[0], 0);
353}
354
355#
356# lock_retrieve
357#
358# Same as retrieve, but with advisory locking.
359#
360sub lock_retrieve {
361 _retrieve($_[0], 1);
362}
363
364# Internal retrieve routine
365sub _retrieve {
366 my ($file, $use_locking) = @_;
367 local *FILE;
368 open(FILE, $file) || logcroak "can't open $file: $!";
369 binmode FILE; # Archaic systems...
370 my $self;
371 my $da = $@; # Could be from exception handler
372 if ($use_locking) {
373 unless (&CAN_FLOCK) {
374 logcarp "Storable::lock_store: fcntl/flock emulation broken on $^O";
375 return undef;
376 }
377 flock(FILE, LOCK_SH) || logcroak "can't get shared lock on $file: $!";
378 # Unlocking will happen when FILE is closed
379 }
380 eval { $self = pretrieve(*FILE) }; # Call C routine
381 close(FILE);
382 logcroak $@ if $@ =~ s/\.?\n$/,/;
383 $@ = $da;
384 return $self;
385}
386
387#
388# fd_retrieve
389#
390# Same as retrieve, but perform from an already opened file descriptor instead.
391#
392sub fd_retrieve {
393 my ($file) = @_;
394 my $fd = fileno($file);
395 logcroak "not a valid file descriptor" unless defined $fd;
396 my $self;
397 my $da = $@; # Could be from exception handler
398 eval { $self = pretrieve($file) }; # Call C routine
399 logcroak $@ if $@ =~ s/\.?\n$/,/;
400 $@ = $da;
401 return $self;
402}
403
404#
405# thaw
406#
407# Recreate objects in memory from an existing frozen image created
408# by freeze. If the frozen image passed is undef, return undef.
409#
410sub thaw {
411 my ($frozen) = @_;
412 return undef unless defined $frozen;
413 my $self;
414 my $da = $@; # Could be from exception handler
415 eval { $self = mretrieve($frozen) }; # Call C routine
416 logcroak $@ if $@ =~ s/\.?\n$/,/;
417 $@ = $da;
418 return $self;
419}
420
4211;
422__END__
423
424=head1 NAME
425
426Storable - persistence for Perl data structures
427
428=head1 SYNOPSIS
429
430 use Storable;
431 store \%table, 'file';
432 $hashref = retrieve('file');
433
434 use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);
435
436 # Network order
437 nstore \%table, 'file';
438 $hashref = retrieve('file'); # There is NO nretrieve()
439
440 # Storing to and retrieving from an already opened file
441 store_fd \@array, \*STDOUT;
442 nstore_fd \%table, \*STDOUT;
443 $aryref = fd_retrieve(\*SOCKET);
444 $hashref = fd_retrieve(\*SOCKET);
445
446 # Serializing to memory
447 $serialized = freeze \%table;
448 %table_clone = %{ thaw($serialized) };
449
450 # Deep (recursive) cloning
451 $cloneref = dclone($ref);
452
453 # Advisory locking
454 use Storable qw(lock_store lock_nstore lock_retrieve)
455 lock_store \%table, 'file';
456 lock_nstore \%table, 'file';
457 $hashref = lock_retrieve('file');
458
459=head1 DESCRIPTION
460
461The Storable package brings persistence to your Perl data structures
462containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be
463conveniently stored to disk and retrieved at a later time.
464
465It can be used in the regular procedural way by calling C<store> with
466a reference to the object to be stored, along with the file name where
467the image should be written.
468
469The routine returns C<undef> for I/O problems or other internal error,
470a true value otherwise. Serious errors are propagated as a C<die> exception.
471
472To retrieve data stored to disk, use C<retrieve> with a file name.
473The objects stored into that file are recreated into memory for you,
474and a I<reference> to the root object is returned. In case an I/O error
475occurs while reading, C<undef> is returned instead. Other serious
476errors are propagated via C<die>.
477
478Since storage is performed recursively, you might want to stuff references
479to objects that share a lot of common data into a single array or hash
480table, and then store that object. That way, when you retrieve back the
481whole thing, the objects will continue to share what they originally shared.
482
483At the cost of a slight header overhead, you may store to an already
484opened file descriptor using the C<store_fd> routine, and retrieve
485from a file via C<fd_retrieve>. Those names aren't imported by default,
486so you will have to do that explicitly if you need those routines.
487The file descriptor you supply must be already opened, for read
488if you're going to retrieve and for write if you wish to store.
489
490 store_fd(\%table, *STDOUT) || die "can't store to stdout\n";
491 $hashref = fd_retrieve(*STDIN);
492
493You can also store data in network order to allow easy sharing across
494multiple platforms, or when storing on a socket known to be remotely
495connected. The routines to call have an initial C<n> prefix for I<network>,
496as in C<nstore> and C<nstore_fd>. At retrieval time, your data will be
497correctly restored so you don't have to know whether you're restoring
498from native or network ordered data. Double values are stored stringified
499to ensure portability as well, at the slight risk of loosing some precision
500in the last decimals.
501
502When using C<fd_retrieve>, objects are retrieved in sequence, one
503object (i.e. one recursive tree) per associated C<store_fd>.
504
505If you're more from the object-oriented camp, you can inherit from
506Storable and directly store your objects by invoking C<store> as
507a method. The fact that the root of the to-be-stored tree is a
508blessed reference (i.e. an object) is special-cased so that the
509retrieve does not provide a reference to that object but rather the
510blessed object reference itself. (Otherwise, you'd get a reference
511to that blessed object).
512
513=head1 MEMORY STORE
514
515The Storable engine can also store data into a Perl scalar instead, to
516later retrieve them. This is mainly used to freeze a complex structure in
517some safe compact memory place (where it can possibly be sent to another
518process via some IPC, since freezing the structure also serializes it in
519effect). Later on, and maybe somewhere else, you can thaw the Perl scalar
520out and recreate the original complex structure in memory.
521
522Surprisingly, the routines to be called are named C<freeze> and C<thaw>.
523If you wish to send out the frozen scalar to another machine, use
524C<nfreeze> instead to get a portable image.
525
526Note that freezing an object structure and immediately thawing it
527actually achieves a deep cloning of that structure:
528
529 dclone(.) = thaw(freeze(.))
530
531Storable provides you with a C<dclone> interface which does not create
532that intermediary scalar but instead freezes the structure in some
533internal memory space and then immediately thaws it out.
534
535=head1 ADVISORY LOCKING
536
537The C<lock_store> and C<lock_nstore> routine are equivalent to
538C<store> and C<nstore>, except that they get an exclusive lock on
539the file before writing. Likewise, C<lock_retrieve> does the same
540as C<retrieve>, but also gets a shared lock on the file before reading.
541
542As with any advisory locking scheme, the protection only works if you
543systematically use C<lock_store> and C<lock_retrieve>. If one side of
544your application uses C<store> whilst the other uses C<lock_retrieve>,
545you will get no protection at all.
546
547The internal advisory locking is implemented using Perl's flock()
548routine. If your system does not support any form of flock(), or if
549you share your files across NFS, you might wish to use other forms
550of locking by using modules such as LockFile::Simple which lock a
551file using a filesystem entry, instead of locking the file descriptor.
552
553=head1 SPEED
554
555The heart of Storable is written in C for decent speed. Extra low-level
556optimizations have been made when manipulating perl internals, to
557sacrifice encapsulation for the benefit of greater speed.
558
559=head1 CANONICAL REPRESENTATION
560
561Normally, Storable stores elements of hashes in the order they are
562stored internally by Perl, i.e. pseudo-randomly. If you set
563C<$Storable::canonical> to some C<TRUE> value, Storable will store
564hashes with the elements sorted by their key. This allows you to
565compare data structures by comparing their frozen representations (or
566even the compressed frozen representations), which can be useful for
567creating lookup tables for complicated queries.
568
569Canonical order does not imply network order; those are two orthogonal
570settings.
571
572=head1 CODE REFERENCES
573
574Since Storable version 2.05, CODE references may be serialized with
575the help of L<B::Deparse>. To enable this feature, set
576C<$Storable::Deparse> to a true value. To enable deserialization,
577C<$Storable::Eval> should be set to a true value. Be aware that
578deserialization is done through C<eval>, which is dangerous if the
579Storable file contains malicious data. You can set C<$Storable::Eval>
580to a subroutine reference which would be used instead of C<eval>. See
581below for an example using a L<Safe> compartment for deserialization
582of CODE references.
583
584If C<$Storable::Deparse> and/or C<$Storable::Eval> are set to false
585values, then the value of C<$Storable::forgive_me> (see below) is
586respected while serializing and deserializing.
587
588=head1 FORWARD COMPATIBILITY
589
590This release of Storable can be used on a newer version of Perl to
591serialize data which is not supported by earlier Perls. By default,
592Storable will attempt to do the right thing, by C<croak()>ing if it
593encounters data that it cannot deserialize. However, the defaults
594can be changed as follows:
595
596=over 4
597
598=item utf8 data
599
600Perl 5.6 added support for Unicode characters with code points > 255,
601and Perl 5.8 has full support for Unicode characters in hash keys.
602Perl internally encodes strings with these characters using utf8, and
603Storable serializes them as utf8. By default, if an older version of
604Perl encounters a utf8 value it cannot represent, it will C<croak()>.
605To change this behaviour so that Storable deserializes utf8 encoded
606values as the string of bytes (effectively dropping the I<is_utf8> flag)
607set C<$Storable::drop_utf8> to some C<TRUE> value. This is a form of
608data loss, because with C<$drop_utf8> true, it becomes impossible to tell
609whether the original data was the Unicode string, or a series of bytes
610that happen to be valid utf8.
611
612=item restricted hashes
613
614Perl 5.8 adds support for restricted hashes, which have keys
615restricted to a given set, and can have values locked to be read only.
616By default, when Storable encounters a restricted hash on a perl
617that doesn't support them, it will deserialize it as a normal hash,
618silently discarding any placeholder keys and leaving the keys and
619all values unlocked. To make Storable C<croak()> instead, set
620C<$Storable::downgrade_restricted> to a C<FALSE> value. To restore
621the default set it back to some C<TRUE> value.
622
623=item files from future versions of Storable
624
625Earlier versions of Storable would immediately croak if they encountered
626a file with a higher internal version number than the reading Storable
627knew about. Internal version numbers are increased each time new data
628types (such as restricted hashes) are added to the vocabulary of the file
629format. This meant that a newer Storable module had no way of writing a
630file readable by an older Storable, even if the writer didn't store newer
631data types.
632
633This version of Storable will defer croaking until it encounters a data
634type in the file that it does not recognize. This means that it will
635continue to read files generated by newer Storable modules which are careful
636in what they write out, making it easier to upgrade Storable modules in a
637mixed environment.
638
639The old behaviour of immediate croaking can be re-instated by setting
640C<$Storable::accept_future_minor> to some C<FALSE> value.
641
642=back
643
644All these variables have no effect on a newer Perl which supports the
645relevant feature.
646
647=head1 ERROR REPORTING
648
649Storable uses the "exception" paradigm, in that it does not try to workaround
650failures: if something bad happens, an exception is generated from the
651caller's perspective (see L<Carp> and C<croak()>). Use eval {} to trap
652those exceptions.
653
654When Storable croaks, it tries to report the error via the C<logcroak()>
655routine from the C<Log::Agent> package, if it is available.
656
657Normal errors are reported by having store() or retrieve() return C<undef>.
658Such errors are usually I/O errors (or truncated stream errors at retrieval).
659
660=head1 WIZARDS ONLY
661
662=head2 Hooks
663
664Any class may define hooks that will be called during the serialization
665and deserialization process on objects that are instances of that class.
666Those hooks can redefine the way serialization is performed (and therefore,
667how the symmetrical deserialization should be conducted).
668
669Since we said earlier:
670
671 dclone(.) = thaw(freeze(.))
672
673everything we say about hooks should also hold for deep cloning. However,
674hooks get to know whether the operation is a mere serialization, or a cloning.
675
676Therefore, when serializing hooks are involved,
677
678 dclone(.) <> thaw(freeze(.))
679
680Well, you could keep them in sync, but there's no guarantee it will always
681hold on classes somebody else wrote. Besides, there is little to gain in
682doing so: a serializing hook could keep only one attribute of an object,
683which is probably not what should happen during a deep cloning of that
684same object.
685
686Here is the hooking interface:
687
688=over 4
689
690=item C<STORABLE_freeze> I<obj>, I<cloning>
691
692The serializing hook, called on the object during serialization. It can be
693inherited, or defined in the class itself, like any other method.
694
695Arguments: I<obj> is the object to serialize, I<cloning> is a flag indicating
696whether we're in a dclone() or a regular serialization via store() or freeze().
697
698Returned value: A LIST C<($serialized, $ref1, $ref2, ...)> where $serialized
699is the serialized form to be used, and the optional $ref1, $ref2, etc... are
700extra references that you wish to let the Storable engine serialize.
701
702At deserialization time, you will be given back the same LIST, but all the
703extra references will be pointing into the deserialized structure.
704
705The B<first time> the hook is hit in a serialization flow, you may have it
706return an empty list. That will signal the Storable engine to further
707discard that hook for this class and to therefore revert to the default
708serialization of the underlying Perl data. The hook will again be normally
709processed in the next serialization.
710
711Unless you know better, serializing hook should always say:
712
713 sub STORABLE_freeze {
714 my ($self, $cloning) = @_;
715 return if $cloning; # Regular default serialization
716 ....
717 }
718
719in order to keep reasonable dclone() semantics.
720
721=item C<STORABLE_thaw> I<obj>, I<cloning>, I<serialized>, ...
722
723The deserializing hook called on the object during deserialization.
724But wait: if we're deserializing, there's no object yet... right?
725
726Wrong: the Storable engine creates an empty one for you. If you know Eiffel,
727you can view C<STORABLE_thaw> as an alternate creation routine.
728
729This means the hook can be inherited like any other method, and that
730I<obj> is your blessed reference for this particular instance.
731
732The other arguments should look familiar if you know C<STORABLE_freeze>:
733I<cloning> is true when we're part of a deep clone operation, I<serialized>
734is the serialized string you returned to the engine in C<STORABLE_freeze>,
735and there may be an optional list of references, in the same order you gave
736them at serialization time, pointing to the deserialized objects (which
737have been processed courtesy of the Storable engine).
738
739When the Storable engine does not find any C<STORABLE_thaw> hook routine,
740it tries to load the class by requiring the package dynamically (using
741the blessed package name), and then re-attempts the lookup. If at that
742time the hook cannot be located, the engine croaks. Note that this mechanism
743will fail if you define several classes in the same file, but L<perlmod>
744warned you.
745
746It is up to you to use this information to populate I<obj> the way you want.
747
748Returned value: none.
749
750=item C<STORABLE_attach> I<class>, I<cloning>, I<serialized>
751
752While C<STORABLE_freeze> and C<STORABLE_thaw> are useful for classes where
753each instance is independent, this mechanism has difficulty (or is
754incompatible) with objects that exist as common process-level or
755system-level resources, such as singleton objects, database pools, caches
756or memoized objects.
757
758The alternative C<STORABLE_attach> method provides a solution for these
759shared objects. Instead of C<STORABLE_freeze> --E<gt> C<STORABLE_thaw>,
760you implement C<STORABLE_freeze> --E<gt> C<STORABLE_attach> instead.
761
762Arguments: I<class> is the class we are attaching to, I<cloning> is a flag
763indicating whether we're in a dclone() or a regular de-serialization via
764thaw(), and I<serialized> is the stored string for the resource object.
765
766Because these resource objects are considered to be owned by the entire
767process/system, and not the "property" of whatever is being serialized,
768no references underneath the object should be included in the serialized
769string. Thus, in any class that implements C<STORABLE_attach>, the
770C<STORABLE_freeze> method cannot return any references, and C<Storable>
771will throw an error if C<STORABLE_freeze> tries to return references.
772
773All information required to "attach" back to the shared resource object
774B<must> be contained B<only> in the C<STORABLE_freeze> return string.
775Otherwise, C<STORABLE_freeze> behaves as normal for C<STORABLE_attach>
776classes.
777
778Because C<STORABLE_attach> is passed the class (rather than an object),
779it also returns the object directly, rather than modifying the passed
780object.
781
782Returned value: object of type C<class>
783
784=back
785
786=head2 Predicates
787
788Predicates are not exportable. They must be called by explicitly prefixing
789them with the Storable package name.
790
791=over 4
792
793=item C<Storable::last_op_in_netorder>
794
795The C<Storable::last_op_in_netorder()> predicate will tell you whether
796network order was used in the last store or retrieve operation. If you
797don't know how to use this, just forget about it.
798
799=item C<Storable::is_storing>
800
801Returns true if within a store operation (via STORABLE_freeze hook).
802
803=item C<Storable::is_retrieving>
804
805Returns true if within a retrieve operation (via STORABLE_thaw hook).
806
807=back
808
809=head2 Recursion
810
811With hooks comes the ability to recurse back to the Storable engine.
812Indeed, hooks are regular Perl code, and Storable is convenient when
813it comes to serializing and deserializing things, so why not use it
814to handle the serialization string?
815
816There are a few things you need to know, however:
817
818=over 4
819
820=item *
821
822You can create endless loops if the things you serialize via freeze()
823(for instance) point back to the object we're trying to serialize in
824the hook.
825
826=item *
827
828Shared references among objects will not stay shared: if we're serializing
829the list of object [A, C] where both object A and C refer to the SAME object
830B, and if there is a serializing hook in A that says freeze(B), then when
831deserializing, we'll get [A', C'] where A' refers to B', but C' refers to D,
832a deep clone of B'. The topology was not preserved.
833
834=back
835
836That's why C<STORABLE_freeze> lets you provide a list of references
837to serialize. The engine guarantees that those will be serialized in the
838same context as the other objects, and therefore that shared objects will
839stay shared.
840
841In the above [A, C] example, the C<STORABLE_freeze> hook could return:
842
843 ("something", $self->{B})
844
845and the B part would be serialized by the engine. In C<STORABLE_thaw>, you
846would get back the reference to the B' object, deserialized for you.
847
848Therefore, recursion should normally be avoided, but is nonetheless supported.
849
850=head2 Deep Cloning
851
852There is a Clone module available on CPAN which implements deep cloning
853natively, i.e. without freezing to memory and thawing the result. It is
854aimed to replace Storable's dclone() some day. However, it does not currently
855support Storable hooks to redefine the way deep cloning is performed.
856
857=head1 Storable magic
858
859Yes, there's a lot of that :-) But more precisely, in UNIX systems
860there's a utility called C<file>, which recognizes data files based on
861their contents (usually their first few bytes). For this to work,
862a certain file called F<magic> needs to taught about the I<signature>
863of the data. Where that configuration file lives depends on the UNIX
864flavour; often it's something like F</usr/share/misc/magic> or
865F</etc/magic>. Your system administrator needs to do the updating of
866the F<magic> file. The necessary signature information is output to
867STDOUT by invoking Storable::show_file_magic(). Note that the GNU
868implementation of the C<file> utility, version 3.38 or later,
869is expected to contain support for recognising Storable files
870out-of-the-box, in addition to other kinds of Perl files.
871
872You can also use the following functions to extract the file header
873information from Storable images:
874
875=over
876
877=item $info = Storable::file_magic( $filename )
878
879If the given file is a Storable image return a hash describing it. If
880the file is readable, but not a Storable image return C<undef>. If
881the file does not exist or is unreadable then croak.
882
883The hash returned has the following elements:
884
885=over
886
887=item C<version>
888
889This returns the file format version. It is a string like "2.7".
890
891Note that this version number is not the same as the version number of
892the Storable module itself. For instance Storable v0.7 create files
893in format v2.0 and Storable v2.15 create files in format v2.7. The
894file format version number only increment when additional features
895that would confuse older versions of the module are added.
896
897Files older than v2.0 will have the one of the version numbers "-1",
898"0" or "1". No minor number was used at that time.
899
900=item C<version_nv>
901
902This returns the file format version as number. It is a string like
903"2.007". This value is suitable for numeric comparisons.
904
905The constant function C<Storable::BIN_VERSION_NV> returns a comparable
906number that represent the highest file version number that this
907version of Storable fully support (but see discussion of
908C<$Storable::accept_future_minor> above). The constant
909C<Storable::BIN_WRITE_VERSION_NV> function returns what file version
910is written and might be less than C<Storable::BIN_VERSION_NV> in some
911configuations.
912
913=item C<major>, C<minor>
914
915This also returns the file format version. If the version is "2.7"
916then major would be 2 and minor would be 7. The minor element is
917missing for when major is less than 2.
918
919=item C<hdrsize>
920
921The is the number of bytes that the Storable header occupies.
922
923=item C<netorder>
924
925This is TRUE if the image store data in network order. This means
926that it was created with nstore() or similar.
927
928=item C<byteorder>
929
930This is only present when C<netorder> is FALSE. It is the
931$Config{byteorder} string of the perl that created this image. It is
932a string like "1234" (32 bit little endian) or "87654321" (64 bit big
933endian). This must match the current perl for the image to be
934readable by Storable.
935
936=item C<intsize>, C<longsize>, C<ptrsize>, C<nvsize>
937
938These are only present when C<netorder> is FALSE. These are the sizes of
939various C datatypes of the perl that created this image. These must
940match the current perl for the image to be readable by Storable.
941
942The C<nvsize> element is only present for file format v2.2 and
943higher.
944
945=item C<file>
946
947The name of the file.
948
949=back
950
951=item $info = Storable::read_magic( $buffer )
952
953=item $info = Storable::read_magic( $buffer, $must_be_file )
954
955The $buffer should be a Storable image or the first few bytes of it.
956If $buffer starts with a Storable header, then a hash describing the
957image is returned, otherwise C<undef> is returned.
958
959The hash has the same structure as the one returned by
960Storable::file_magic(). The C<file> element is true if the image is a
961file image.
962
963If the $must_be_file argument is provided and is TRUE, then return
964C<undef> unless the image looks like it belongs to a file dump.
965
966The maximum size of a Storable header is currently 21 bytes. If the
967provided $buffer is only the first part of a Storable image it should
968at least be this long to ensure that read_magic() will recognize it as
969such.
970
971=back
972
973=head1 EXAMPLES
974
975Here are some code samples showing a possible usage of Storable:
976
977 use Storable qw(store retrieve freeze thaw dclone);
978
979 %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1);
980
981 store(\%color, 'mycolors') or die "Can't store %a in mycolors!\n";
982
983 $colref = retrieve('mycolors');
984 die "Unable to retrieve from mycolors!\n" unless defined $colref;
985 printf "Blue is still %lf\n", $colref->{'Blue'};
986
987 $colref2 = dclone(\%color);
988
989 $str = freeze(\%color);
990 printf "Serialization of %%color is %d bytes long.\n", length($str);
991 $colref3 = thaw($str);
992
993which prints (on my machine):
994
995 Blue is still 0.100000
996 Serialization of %color is 102 bytes long.
997
998Serialization of CODE references and deserialization in a safe
999compartment:
1000
1001=for example begin
1002
1003 use Storable qw(freeze thaw);
1004 use Safe;
1005 use strict;
1006 my $safe = new Safe;
1007 # because of opcodes used in "use strict":
1008 $safe->permit(qw(:default require));
1009 local $Storable::Deparse = 1;
1010 local $Storable::Eval = sub { $safe->reval($_[0]) };
1011 my $serialized = freeze(sub { 42 });
1012 my $code = thaw($serialized);
1013 $code->() == 42;
1014
1015=for example end
1016
1017=for example_testing
1018 is( $code->(), 42 );
1019
1020=head1 WARNING
1021
1022If you're using references as keys within your hash tables, you're bound
1023to be disappointed when retrieving your data. Indeed, Perl stringifies
1024references used as hash table keys. If you later wish to access the
1025items via another reference stringification (i.e. using the same
1026reference that was used for the key originally to record the value into
1027the hash table), it will work because both references stringify to the
1028same string.
1029
1030It won't work across a sequence of C<store> and C<retrieve> operations,
1031however, because the addresses in the retrieved objects, which are
1032part of the stringified references, will probably differ from the
1033original addresses. The topology of your structure is preserved,
1034but not hidden semantics like those.
1035
1036On platforms where it matters, be sure to call C<binmode()> on the
1037descriptors that you pass to Storable functions.
1038
1039Storing data canonically that contains large hashes can be
1040significantly slower than storing the same data normally, as
1041temporary arrays to hold the keys for each hash have to be allocated,
1042populated, sorted and freed. Some tests have shown a halving of the
1043speed of storing -- the exact penalty will depend on the complexity of
1044your data. There is no slowdown on retrieval.
1045
1046=head1 BUGS
1047
1048You can't store GLOB, FORMLINE, etc.... If you can define semantics
1049for those operations, feel free to enhance Storable so that it can
1050deal with them.
1051
1052The store functions will C<croak> if they run into such references
1053unless you set C<$Storable::forgive_me> to some C<TRUE> value. In that
1054case, the fatal message is turned in a warning and some
1055meaningless string is stored instead.
1056
1057Setting C<$Storable::canonical> may not yield frozen strings that
1058compare equal due to possible stringification of numbers. When the
1059string version of a scalar exists, it is the form stored; therefore,
1060if you happen to use your numbers as strings between two freezing
1061operations on the same data structures, you will get different
1062results.
1063
1064When storing doubles in network order, their value is stored as text.
1065However, you should also not expect non-numeric floating-point values
1066such as infinity and "not a number" to pass successfully through a
1067nstore()/retrieve() pair.
1068
1069As Storable neither knows nor cares about character sets (although it
1070does know that characters may be more than eight bits wide), any difference
1071in the interpretation of character codes between a host and a target
1072system is your problem. In particular, if host and target use different
1073code points to represent the characters used in the text representation
1074of floating-point numbers, you will not be able be able to exchange
1075floating-point data, even with nstore().
1076
1077C<Storable::drop_utf8> is a blunt tool. There is no facility either to
1078return B<all> strings as utf8 sequences, or to attempt to convert utf8
1079data back to 8 bit and C<croak()> if the conversion fails.
1080
1081Prior to Storable 2.01, no distinction was made between signed and
1082unsigned integers on storing. By default Storable prefers to store a
1083scalars string representation (if it has one) so this would only cause
1084problems when storing large unsigned integers that had never been converted
1085to string or floating point. In other words values that had been generated
1086by integer operations such as logic ops and then not used in any string or
1087arithmetic context before storing.
1088
1089=head2 64 bit data in perl 5.6.0 and 5.6.1
1090
1091This section only applies to you if you have existing data written out
1092by Storable 2.02 or earlier on perl 5.6.0 or 5.6.1 on Unix or Linux which
1093has been configured with 64 bit integer support (not the default)
1094If you got a precompiled perl, rather than running Configure to build
1095your own perl from source, then it almost certainly does not affect you,
1096and you can stop reading now (unless you're curious). If you're using perl
1097on Windows it does not affect you.
1098
1099Storable writes a file header which contains the sizes of various C
1100language types for the C compiler that built Storable (when not writing in
1101network order), and will refuse to load files written by a Storable not
1102on the same (or compatible) architecture. This check and a check on
1103machine byteorder is needed because the size of various fields in the file
1104are given by the sizes of the C language types, and so files written on
1105different architectures are incompatible. This is done for increased speed.
1106(When writing in network order, all fields are written out as standard
1107lengths, which allows full interworking, but takes longer to read and write)
1108
1109Perl 5.6.x introduced the ability to optional configure the perl interpreter
1110to use C's C<long long> type to allow scalars to store 64 bit integers on 32
1111bit systems. However, due to the way the Perl configuration system
1112generated the C configuration files on non-Windows platforms, and the way
1113Storable generates its header, nothing in the Storable file header reflected
1114whether the perl writing was using 32 or 64 bit integers, despite the fact
1115that Storable was storing some data differently in the file. Hence Storable
1116running on perl with 64 bit integers will read the header from a file
1117written by a 32 bit perl, not realise that the data is actually in a subtly
1118incompatible format, and then go horribly wrong (possibly crashing) if it
1119encountered a stored integer. This is a design failure.
1120
1121Storable has now been changed to write out and read in a file header with
1122information about the size of integers. It's impossible to detect whether
1123an old file being read in was written with 32 or 64 bit integers (they have
1124the same header) so it's impossible to automatically switch to a correct
1125backwards compatibility mode. Hence this Storable defaults to the new,
1126correct behaviour.
1127
1128What this means is that if you have data written by Storable 1.x running
1129on perl 5.6.0 or 5.6.1 configured with 64 bit integers on Unix or Linux
1130then by default this Storable will refuse to read it, giving the error
1131I<Byte order is not compatible>. If you have such data then you you
1132should set C<$Storable::interwork_56_64bit> to a true value to make this
1133Storable read and write files with the old header. You should also
1134migrate your data, or any older perl you are communicating with, to this
1135current version of Storable.
1136
1137If you don't have data written with specific configuration of perl described
1138above, then you do not and should not do anything. Don't set the flag -
1139not only will Storable on an identically configured perl refuse to load them,
1140but Storable a differently configured perl will load them believing them
1141to be correct for it, and then may well fail or crash part way through
1142reading them.
1143
1144=head1 CREDITS
1145
1146Thank you to (in chronological order):
1147
1148 Jarkko Hietaniemi <jhi@iki.fi>
1149 Ulrich Pfeifer <pfeifer@charly.informatik.uni-dortmund.de>
1150 Benjamin A. Holzman <bah@ecnvantage.com>
1151 Andrew Ford <A.Ford@ford-mason.co.uk>
1152 Gisle Aas <gisle@aas.no>
1153 Jeff Gresham <gresham_jeffrey@jpmorgan.com>
1154 Murray Nesbitt <murray@activestate.com>
1155 Marc Lehmann <pcg@opengroup.org>
1156 Justin Banks <justinb@wamnet.com>
1157 Jarkko Hietaniemi <jhi@iki.fi> (AGAIN, as perl 5.7.0 Pumpkin!)
1158 Salvador Ortiz Garcia <sog@msg.com.mx>
1159 Dominic Dunlop <domo@computer.org>
1160 Erik Haugan <erik@solbors.no>
1161
1162for their bug reports, suggestions and contributions.
1163
1164Benjamin Holzman contributed the tied variable support, Andrew Ford
1165contributed the canonical order for hashes, and Gisle Aas fixed
1166a few misunderstandings of mine regarding the perl internals,
1167and optimized the emission of "tags" in the output streams by
1168simply counting the objects instead of tagging them (leading to
1169a binary incompatibility for the Storable image starting at version
11700.6--older images are, of course, still properly understood).
1171Murray Nesbitt made Storable thread-safe. Marc Lehmann added overloading
1172and references to tied items support.
1173
1174=head1 AUTHOR
1175
1176Storable was written by Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
1177Maintenance is now done by the perl5-porters F<E<lt>perl5-porters@perl.orgE<gt>>
1178
1179Please e-mail us with problems, bug fixes, comments and complaints,
1180although if you have compliments you should send them to Raphael.
1181Please don't e-mail Raphael with problems, as he no longer works on
1182Storable, and your message will be delayed while he forwards it to us.
1183
1184=head1 SEE ALSO
1185
1186L<Clone>.
1187
1188=cut
# spent 138µs within Storable::bootstrap which was called # once (138µs+0s) by DynaLoader::bootstrap at line 253 of DynaLoader.pm
sub Storable::bootstrap; # xsub
# spent 772µs within Storable::dclone which was called 4 times, avg 193µs/call: # 4 times (772µs+0s) by Class::DBI::ColumnGrouper::clone at line 68 of Class/DBI/ColumnGrouper.pm, avg 193µs/call
sub Storable::dclone; # xsub