Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / Storable.html
blob9e08cbc753e9aa8daaf478759439c661a53a986f
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Storable - persistence for Perl data structures</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;Storable - persistence for Perl data structures</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <li><a href="#memory_store">MEMORY STORE</a></li>
26 <li><a href="#advisory_locking">ADVISORY LOCKING</a></li>
27 <li><a href="#speed">SPEED</a></li>
28 <li><a href="#canonical_representation">CANONICAL REPRESENTATION</a></li>
29 <li><a href="#code_references">CODE REFERENCES</a></li>
30 <li><a href="#forward_compatibility">FORWARD COMPATIBILITY</a></li>
31 <li><a href="#error_reporting">ERROR REPORTING</a></li>
32 <li><a href="#wizards_only">WIZARDS ONLY</a></li>
33 <ul>
35 <li><a href="#hooks">Hooks</a></li>
36 <li><a href="#predicates">Predicates</a></li>
37 <li><a href="#recursion">Recursion</a></li>
38 <li><a href="#deep_cloning">Deep Cloning</a></li>
39 </ul>
41 <li><a href="#storable_magic">Storable magic</a></li>
42 <li><a href="#examples">EXAMPLES</a></li>
43 <li><a href="#warning">WARNING</a></li>
44 <li><a href="#bugs">BUGS</a></li>
45 <ul>
47 <li><a href="#64_bit_data_in_perl_5_6_0_and_5_6_1">64 bit data in perl 5.6.0 and 5.6.1</a></li>
48 </ul>
50 <li><a href="#credits">CREDITS</a></li>
51 <li><a href="#author">AUTHOR</a></li>
52 <li><a href="#see_also">SEE ALSO</a></li>
53 </ul>
54 <!-- INDEX END -->
56 <hr />
57 <p>
58 </p>
59 <h1><a name="name">NAME</a></h1>
60 <p>Storable - persistence for Perl data structures</p>
61 <p>
62 </p>
63 <hr />
64 <h1><a name="synopsis">SYNOPSIS</a></h1>
65 <pre>
66 use Storable;
67 store \%table, 'file';
68 $hashref = retrieve('file');</pre>
69 <pre>
70 use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);</pre>
71 <pre>
72 # Network order
73 nstore \%table, 'file';
74 $hashref = retrieve('file'); # There is NO nretrieve()</pre>
75 <pre>
76 # Storing to and retrieving from an already opened file
77 store_fd \@array, \*STDOUT;
78 nstore_fd \%table, \*STDOUT;
79 $aryref = fd_retrieve(\*SOCKET);
80 $hashref = fd_retrieve(\*SOCKET);</pre>
81 <pre>
82 # Serializing to memory
83 $serialized = freeze \%table;
84 %table_clone = %{ thaw($serialized) };</pre>
85 <pre>
86 # Deep (recursive) cloning
87 $cloneref = dclone($ref);</pre>
88 <pre>
89 # Advisory locking
90 use Storable qw(lock_store lock_nstore lock_retrieve)
91 lock_store \%table, 'file';
92 lock_nstore \%table, 'file';
93 $hashref = lock_retrieve('file');</pre>
94 <p>
95 </p>
96 <hr />
97 <h1><a name="description">DESCRIPTION</a></h1>
98 <p>The Storable package brings persistence to your Perl data structures
99 containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be
100 conveniently stored to disk and retrieved at a later time.</p>
101 <p>It can be used in the regular procedural way by calling <code>store</code> with
102 a reference to the object to be stored, along with the file name where
103 the image should be written.</p>
104 <p>The routine returns <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a> for I/O problems or other internal error,
105 a true value otherwise. Serious errors are propagated as a <code>die</code> exception.</p>
106 <p>To retrieve data stored to disk, use <code>retrieve</code> with a file name.
107 The objects stored into that file are recreated into memory for you,
108 and a <em>reference</em> to the root object is returned. In case an I/O error
109 occurs while reading, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a> is returned instead. Other serious
110 errors are propagated via <code>die</code>.</p>
111 <p>Since storage is performed recursively, you might want to stuff references
112 to objects that share a lot of common data into a single array or hash
113 table, and then store that object. That way, when you retrieve back the
114 whole thing, the objects will continue to share what they originally shared.</p>
115 <p>At the cost of a slight header overhead, you may store to an already
116 opened file descriptor using the <code>store_fd</code> routine, and retrieve
117 from a file via <code>fd_retrieve</code>. Those names aren't imported by default,
118 so you will have to do that explicitly if you need those routines.
119 The file descriptor you supply must be already opened, for read
120 if you're going to retrieve and for write if you wish to store.</p>
121 <pre>
122 store_fd(\%table, *STDOUT) || die &quot;can't store to stdout\n&quot;;
123 $hashref = fd_retrieve(*STDIN);</pre>
124 <p>You can also store data in network order to allow easy sharing across
125 multiple platforms, or when storing on a socket known to be remotely
126 connected. The routines to call have an initial <a href="file://C|\msysgit\mingw\html/pod/perlguts.html#item_n"><code>n</code></a> prefix for <em>network</em>,
127 as in <code>nstore</code> and <code>nstore_fd</code>. At retrieval time, your data will be
128 correctly restored so you don't have to know whether you're restoring
129 from native or network ordered data. Double values are stored stringified
130 to ensure portability as well, at the slight risk of loosing some precision
131 in the last decimals.</p>
132 <p>When using <code>fd_retrieve</code>, objects are retrieved in sequence, one
133 object (i.e. one recursive tree) per associated <code>store_fd</code>.</p>
134 <p>If you're more from the object-oriented camp, you can inherit from
135 Storable and directly store your objects by invoking <code>store</code> as
136 a method. The fact that the root of the to-be-stored tree is a
137 blessed reference (i.e. an object) is special-cased so that the
138 retrieve does not provide a reference to that object but rather the
139 blessed object reference itself. (Otherwise, you'd get a reference
140 to that blessed object).</p>
142 </p>
143 <hr />
144 <h1><a name="memory_store">MEMORY STORE</a></h1>
145 <p>The Storable engine can also store data into a Perl scalar instead, to
146 later retrieve them. This is mainly used to freeze a complex structure in
147 some safe compact memory place (where it can possibly be sent to another
148 process via some IPC, since freezing the structure also serializes it in
149 effect). Later on, and maybe somewhere else, you can thaw the Perl scalar
150 out and recreate the original complex structure in memory.</p>
151 <p>Surprisingly, the routines to be called are named <code>freeze</code> and <code>thaw</code>.
152 If you wish to send out the frozen scalar to another machine, use
153 <code>nfreeze</code> instead to get a portable image.</p>
154 <p>Note that freezing an object structure and immediately thawing it
155 actually achieves a deep cloning of that structure:</p>
156 <pre>
157 dclone(.) = thaw(freeze(.))</pre>
158 <p>Storable provides you with a <code>dclone</code> interface which does not create
159 that intermediary scalar but instead freezes the structure in some
160 internal memory space and then immediately thaws it out.</p>
162 </p>
163 <hr />
164 <h1><a name="advisory_locking">ADVISORY LOCKING</a></h1>
165 <p>The <code>lock_store</code> and <code>lock_nstore</code> routine are equivalent to
166 <code>store</code> and <code>nstore</code>, except that they get an exclusive lock on
167 the file before writing. Likewise, <code>lock_retrieve</code> does the same
168 as <code>retrieve</code>, but also gets a shared lock on the file before reading.</p>
169 <p>As with any advisory locking scheme, the protection only works if you
170 systematically use <code>lock_store</code> and <code>lock_retrieve</code>. If one side of
171 your application uses <code>store</code> whilst the other uses <code>lock_retrieve</code>,
172 you will get no protection at all.</p>
173 <p>The internal advisory locking is implemented using Perl's <code>flock()</code>
174 routine. If your system does not support any form of flock(), or if
175 you share your files across NFS, you might wish to use other forms
176 of locking by using modules such as LockFile::Simple which lock a
177 file using a filesystem entry, instead of locking the file descriptor.</p>
179 </p>
180 <hr />
181 <h1><a name="speed">SPEED</a></h1>
182 <p>The heart of Storable is written in C for decent speed. Extra low-level
183 optimizations have been made when manipulating perl internals, to
184 sacrifice encapsulation for the benefit of greater speed.</p>
186 </p>
187 <hr />
188 <h1><a name="canonical_representation">CANONICAL REPRESENTATION</a></h1>
189 <p>Normally, Storable stores elements of hashes in the order they are
190 stored internally by Perl, i.e. pseudo-randomly. If you set
191 <code>$Storable::canonical</code> to some <code>TRUE</code> value, Storable will store
192 hashes with the elements sorted by their key. This allows you to
193 compare data structures by comparing their frozen representations (or
194 even the compressed frozen representations), which can be useful for
195 creating lookup tables for complicated queries.</p>
196 <p>Canonical order does not imply network order; those are two orthogonal
197 settings.</p>
199 </p>
200 <hr />
201 <h1><a name="code_references">CODE REFERENCES</a></h1>
202 <p>Since Storable version 2.05, CODE references may be serialized with
203 the help of <a href="file://C|\msysgit\mingw\html/lib/B/Deparse.html">the B::Deparse manpage</a>. To enable this feature, set
204 <code>$Storable::Deparse</code> to a true value. To enable deserializazion,
205 <code>$Storable::Eval</code> should be set to a true value. Be aware that
206 deserialization is done through <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval</code></a>, which is dangerous if the
207 Storable file contains malicious data. You can set <code>$Storable::Eval</code>
208 to a subroutine reference which would be used instead of <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval</code></a>. See
209 below for an example using a <a href="file://C|\msysgit\mingw\html/lib/Safe.html">the Safe manpage</a> compartment for deserialization
210 of CODE references.</p>
211 <p>If <code>$Storable::Deparse</code> and/or <code>$Storable::Eval</code> are set to false
212 values, then the value of <code>$Storable::forgive_me</code> (see below) is
213 respected while serializing and deserializing.</p>
215 </p>
216 <hr />
217 <h1><a name="forward_compatibility">FORWARD COMPATIBILITY</a></h1>
218 <p>This release of Storable can be used on a newer version of Perl to
219 serialize data which is not supported by earlier Perls. By default,
220 Storable will attempt to do the right thing, by <code>croak()</code>ing if it
221 encounters data that it cannot deserialize. However, the defaults
222 can be changed as follows:</p>
223 <dl>
224 <dt><strong><a name="item_utf8_data">utf8 data</a></strong>
226 <dd>
227 <p>Perl 5.6 added support for Unicode characters with code points &gt; 255,
228 and Perl 5.8 has full support for Unicode characters in hash keys.
229 Perl internally encodes strings with these characters using utf8, and
230 Storable serializes them as utf8. By default, if an older version of
231 Perl encounters a utf8 value it cannot represent, it will <code>croak()</code>.
232 To change this behaviour so that Storable deserializes utf8 encoded
233 values as the string of bytes (effectively dropping the <em>is_utf8</em> flag)
234 set <code>$Storable::drop_utf8</code> to some <code>TRUE</code> value. This is a form of
235 data loss, because with <code>$drop_utf8</code> true, it becomes impossible to tell
236 whether the original data was the Unicode string, or a series of bytes
237 that happen to be valid utf8.</p>
238 </dd>
239 </li>
240 <dt><strong><a name="item_restricted_hashes">restricted hashes</a></strong>
242 <dd>
243 <p>Perl 5.8 adds support for restricted hashes, which have keys
244 restricted to a given set, and can have values locked to be read only.
245 By default, when Storable encounters a restricted hash on a perl
246 that doesn't support them, it will deserialize it as a normal hash,
247 silently discarding any placeholder keys and leaving the keys and
248 all values unlocked. To make Storable <code>croak()</code> instead, set
249 <code>$Storable::downgrade_restricted</code> to a <code>FALSE</code> value. To restore
250 the default set it back to some <code>TRUE</code> value.</p>
251 </dd>
252 </li>
253 <dt><strong><a name="item_files_from_future_versions_of_storable">files from future versions of Storable</a></strong>
255 <dd>
256 <p>Earlier versions of Storable would immediately croak if they encountered
257 a file with a higher internal version number than the reading Storable
258 knew about. Internal version numbers are increased each time new data
259 types (such as restricted hashes) are added to the vocabulary of the file
260 format. This meant that a newer Storable module had no way of writing a
261 file readable by an older Storable, even if the writer didn't store newer
262 data types.</p>
263 </dd>
264 <dd>
265 <p>This version of Storable will defer croaking until it encounters a data
266 type in the file that it does not recognize. This means that it will
267 continue to read files generated by newer Storable modules which are careful
268 in what they write out, making it easier to upgrade Storable modules in a
269 mixed environment.</p>
270 </dd>
271 <dd>
272 <p>The old behaviour of immediate croaking can be re-instated by setting
273 <code>$Storable::accept_future_minor</code> to some <code>FALSE</code> value.</p>
274 </dd>
275 </li>
276 </dl>
277 <p>All these variables have no effect on a newer Perl which supports the
278 relevant feature.</p>
280 </p>
281 <hr />
282 <h1><a name="error_reporting">ERROR REPORTING</a></h1>
283 <p>Storable uses the ``exception'' paradigm, in that it does not try to workaround
284 failures: if something bad happens, an exception is generated from the
285 caller's perspective (see <a href="file://C|\msysgit\mingw\html/lib/Carp.html">the Carp manpage</a> and <code>croak()</code>). Use eval {} to trap
286 those exceptions.</p>
287 <p>When Storable croaks, it tries to report the error via the <code>logcroak()</code>
288 routine from the <code>Log::Agent</code> package, if it is available.</p>
289 <p>Normal errors are reported by having <code>store()</code> or <code>retrieve()</code> return <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a>.
290 Such errors are usually I/O errors (or truncated stream errors at retrieval).</p>
292 </p>
293 <hr />
294 <h1><a name="wizards_only">WIZARDS ONLY</a></h1>
296 </p>
297 <h2><a name="hooks">Hooks</a></h2>
298 <p>Any class may define hooks that will be called during the serialization
299 and deserialization process on objects that are instances of that class.
300 Those hooks can redefine the way serialization is performed (and therefore,
301 how the symmetrical deserialization should be conducted).</p>
302 <p>Since we said earlier:</p>
303 <pre>
304 dclone(.) = thaw(freeze(.))</pre>
305 <p>everything we say about hooks should also hold for deep cloning. However,
306 hooks get to know whether the operation is a mere serialization, or a cloning.</p>
307 <p>Therefore, when serializing hooks are involved,</p>
308 <pre>
309 dclone(.) &lt;&gt; thaw(freeze(.))</pre>
310 <p>Well, you could keep them in sync, but there's no guarantee it will always
311 hold on classes somebody else wrote. Besides, there is little to gain in
312 doing so: a serializing hook could keep only one attribute of an object,
313 which is probably not what should happen during a deep cloning of that
314 same object.</p>
315 <p>Here is the hooking interface:</p>
316 <dl>
317 <dt><strong><a name="item_storable_freeze_obj_2c_cloning"><code>STORABLE_freeze</code> <em>obj</em>, <em>cloning</em></a></strong>
319 <dd>
320 <p>The serializing hook, called on the object during serialization. It can be
321 inherited, or defined in the class itself, like any other method.</p>
322 </dd>
323 <dd>
324 <p>Arguments: <em>obj</em> is the object to serialize, <em>cloning</em> is a flag indicating
325 whether we're in a <code>dclone()</code> or a regular serialization via <code>store()</code> or freeze().</p>
326 </dd>
327 <dd>
328 <p>Returned value: A LIST <code>($serialized, $ref1, $ref2, ...)</code> where $serialized
329 is the serialized form to be used, and the optional $ref1, $ref2, etc... are
330 extra references that you wish to let the Storable engine serialize.</p>
331 </dd>
332 <dd>
333 <p>At deserialization time, you will be given back the same LIST, but all the
334 extra references will be pointing into the deserialized structure.</p>
335 </dd>
336 <dd>
337 <p>The <strong>first time</strong> the hook is hit in a serialization flow, you may have it
338 return an empty list. That will signal the Storable engine to further
339 discard that hook for this class and to therefore revert to the default
340 serialization of the underlying Perl data. The hook will again be normally
341 processed in the next serialization.</p>
342 </dd>
343 <dd>
344 <p>Unless you know better, serializing hook should always say:</p>
345 </dd>
346 <dd>
347 <pre>
348 sub STORABLE_freeze {
349 my ($self, $cloning) = @_;
350 return if $cloning; # Regular default serialization
351 ....
352 }</pre>
353 </dd>
354 <dd>
355 <p>in order to keep reasonable <code>dclone()</code> semantics.</p>
356 </dd>
357 </li>
358 <dt><strong><a name="item_storable_thaw_obj_2c_cloning_2c_serialized_2c__2e_"><code>STORABLE_thaw</code> <em>obj</em>, <em>cloning</em>, <em>serialized</em>, ...</a></strong>
360 <dd>
361 <p>The deserializing hook called on the object during deserialization.
362 But wait: if we're deserializing, there's no object yet... right?</p>
363 </dd>
364 <dd>
365 <p>Wrong: the Storable engine creates an empty one for you. If you know Eiffel,
366 you can view <code>STORABLE_thaw</code> as an alternate creation routine.</p>
367 </dd>
368 <dd>
369 <p>This means the hook can be inherited like any other method, and that
370 <em>obj</em> is your blessed reference for this particular instance.</p>
371 </dd>
372 <dd>
373 <p>The other arguments should look familiar if you know <code>STORABLE_freeze</code>:
374 <em>cloning</em> is true when we're part of a deep clone operation, <em>serialized</em>
375 is the serialized string you returned to the engine in <code>STORABLE_freeze</code>,
376 and there may be an optional list of references, in the same order you gave
377 them at serialization time, pointing to the deserialized objects (which
378 have been processed courtesy of the Storable engine).</p>
379 </dd>
380 <dd>
381 <p>When the Storable engine does not find any <code>STORABLE_thaw</code> hook routine,
382 it tries to load the class by requiring the package dynamically (using
383 the blessed package name), and then re-attempts the lookup. If at that
384 time the hook cannot be located, the engine croaks. Note that this mechanism
385 will fail if you define several classes in the same file, but <a href="file://C|\msysgit\mingw\html/pod/perlmod.html">the perlmod manpage</a>
386 warned you.</p>
387 </dd>
388 <dd>
389 <p>It is up to you to use this information to populate <em>obj</em> the way you want.</p>
390 </dd>
391 <dd>
392 <p>Returned value: none.</p>
393 </dd>
394 </li>
395 <dt><strong><a name="item_storable_attach_class_2c_cloning_2c_serialized"><code>STORABLE_attach</code> <em>class</em>, <em>cloning</em>, <em>serialized</em></a></strong>
397 <dd>
398 <p>While <code>STORABLE_freeze</code> and <code>STORABLE_thaw</code> are useful for classes where
399 each instance is independant, this mechanism has difficulty (or is
400 incompatible) with objects that exist as common process-level or
401 system-level resources, such as singleton objects, database pools, caches
402 or memoized objects.</p>
403 </dd>
404 <dd>
405 <p>The alternative <code>STORABLE_attach</code> method provides a solution for these
406 shared objects. Instead of <code>STORABLE_freeze</code> --&GT; <code>STORABLE_thaw</code>,
407 you implement <code>STORABLE_freeze</code> --&GT; <code>STORABLE_attach</code> instead.</p>
408 </dd>
409 <dd>
410 <p>Arguments: <em>class</em> is the class we are attaching to, <em>cloning</em> is a flag
411 indicating whether we're in a <code>dclone()</code> or a regular de-serialization via
412 thaw(), and <em>serialized</em> is the stored string for the resource object.</p>
413 </dd>
414 <dd>
415 <p>Because these resource objects are considered to be owned by the entire
416 process/system, and not the ``property'' of whatever is being serialized,
417 no references underneath the object should be included in the serialized
418 string. Thus, in any class that implements <code>STORABLE_attach</code>, the
419 <code>STORABLE_freeze</code> method cannot return any references, and <code>Storable</code>
420 will throw an error if <code>STORABLE_freeze</code> tries to return references.</p>
421 </dd>
422 <dd>
423 <p>All information required to ``attach'' back to the shared resource object
424 <strong>must</strong> be contained <strong>only</strong> in the <code>STORABLE_freeze</code> return string.
425 Otherwise, <code>STORABLE_freeze</code> behaves as normal for <code>STORABLE_attach</code>
426 classes.</p>
427 </dd>
428 <dd>
429 <p>Because <code>STORABLE_attach</code> is passed the class (rather than an object),
430 it also returns the object directly, rather than modifying the passed
431 object.</p>
432 </dd>
433 <dd>
434 <p>Returned value: object of type <code>class</code></p>
435 </dd>
436 </li>
437 </dl>
439 </p>
440 <h2><a name="predicates">Predicates</a></h2>
441 <p>Predicates are not exportable. They must be called by explicitly prefixing
442 them with the Storable package name.</p>
443 <dl>
444 <dt><strong><a name="item_storable_3a_3alast_op_in_netorder"><code>Storable::last_op_in_netorder</code></a></strong>
446 <dd>
447 <p>The <code>Storable::last_op_in_netorder()</code> predicate will tell you whether
448 network order was used in the last store or retrieve operation. If you
449 don't know how to use this, just forget about it.</p>
450 </dd>
451 </li>
452 <dt><strong><a name="item_storable_3a_3ais_storing"><code>Storable::is_storing</code></a></strong>
454 <dd>
455 <p>Returns true if within a store operation (via STORABLE_freeze hook).</p>
456 </dd>
457 </li>
458 <dt><strong><a name="item_storable_3a_3ais_retrieving"><code>Storable::is_retrieving</code></a></strong>
460 <dd>
461 <p>Returns true if within a retrieve operation (via STORABLE_thaw hook).</p>
462 </dd>
463 </li>
464 </dl>
466 </p>
467 <h2><a name="recursion">Recursion</a></h2>
468 <p>With hooks comes the ability to recurse back to the Storable engine.
469 Indeed, hooks are regular Perl code, and Storable is convenient when
470 it comes to serializing and deserializing things, so why not use it
471 to handle the serialization string?</p>
472 <p>There are a few things you need to know, however:</p>
473 <ul>
474 <li>
475 <p>You can create endless loops if the things you serialize via <code>freeze()</code>
476 (for instance) point back to the object we're trying to serialize in
477 the hook.</p>
478 </li>
479 <li>
480 <p>Shared references among objects will not stay shared: if we're serializing
481 the list of object [A, C] where both object A and C refer to the SAME object
482 B, and if there is a serializing hook in A that says freeze(B), then when
483 deserializing, we'll get [A', C'] where A' refers to B', but C' refers to D,
484 a deep clone of B'. The topology was not preserved.</p>
485 </li>
486 </ul>
487 <p>That's why <code>STORABLE_freeze</code> lets you provide a list of references
488 to serialize. The engine guarantees that those will be serialized in the
489 same context as the other objects, and therefore that shared objects will
490 stay shared.</p>
491 <p>In the above [A, C] example, the <code>STORABLE_freeze</code> hook could return:</p>
492 <pre>
493 (&quot;something&quot;, $self-&gt;{B})</pre>
494 <p>and the B part would be serialized by the engine. In <code>STORABLE_thaw</code>, you
495 would get back the reference to the B' object, deserialized for you.</p>
496 <p>Therefore, recursion should normally be avoided, but is nonetheless supported.</p>
498 </p>
499 <h2><a name="deep_cloning">Deep Cloning</a></h2>
500 <p>There is a Clone module available on CPAN which implements deep cloning
501 natively, i.e. without freezing to memory and thawing the result. It is
502 aimed to replace Storable's <code>dclone()</code> some day. However, it does not currently
503 support Storable hooks to redefine the way deep cloning is performed.</p>
505 </p>
506 <hr />
507 <h1><a name="storable_magic">Storable magic</a></h1>
508 <p>Yes, there's a lot of that :-) But more precisely, in UNIX systems
509 there's a utility called <code>file</code>, which recognizes data files based on
510 their contents (usually their first few bytes). For this to work,
511 a certain file called <em>magic</em> needs to taught about the <em>signature</em>
512 of the data. Where that configuration file lives depends on the UNIX
513 flavour; often it's something like <em>/usr/share/misc/magic</em> or
514 <em>/etc/magic</em>. Your system administrator needs to do the updating of
515 the <em>magic</em> file. The necessary signature information is output to
516 STDOUT by invoking Storable::show_file_magic(). Note that the GNU
517 implementation of the <code>file</code> utility, version 3.38 or later,
518 is expected to contain support for recognising Storable files
519 out-of-the-box, in addition to other kinds of Perl files.</p>
521 </p>
522 <hr />
523 <h1><a name="examples">EXAMPLES</a></h1>
524 <p>Here are some code samples showing a possible usage of Storable:</p>
525 <pre>
526 use Storable qw(store retrieve freeze thaw dclone);</pre>
527 <pre>
528 %color = ('Blue' =&gt; 0.1, 'Red' =&gt; 0.8, 'Black' =&gt; 0, 'White' =&gt; 1);</pre>
529 <pre>
530 store(\%color, 'mycolors') or die &quot;Can't store %a in mycolors!\n&quot;;</pre>
531 <pre>
532 $colref = retrieve('mycolors');
533 die &quot;Unable to retrieve from mycolors!\n&quot; unless defined $colref;
534 printf &quot;Blue is still %lf\n&quot;, $colref-&gt;{'Blue'};</pre>
535 <pre>
536 $colref2 = dclone(\%color);</pre>
537 <pre>
538 $str = freeze(\%color);
539 printf &quot;Serialization of %%color is %d bytes long.\n&quot;, length($str);
540 $colref3 = thaw($str);</pre>
541 <p>which prints (on my machine):</p>
542 <pre>
543 Blue is still 0.100000
544 Serialization of %color is 102 bytes long.</pre>
545 <p>Serialization of CODE references and deserialization in a safe
546 compartment:</p>
547 <pre>
548 use Storable qw(freeze thaw);
549 use Safe;
550 use strict;
551 my $safe = new Safe;
552 # because of opcodes used in &quot;use strict&quot;:
553 $safe-&gt;permit(qw(:default require));
554 local $Storable::Deparse = 1;
555 local $Storable::Eval = sub { $safe-&gt;reval($_[0]) };
556 my $serialized = freeze(sub { 42 });
557 my $code = thaw($serialized);
558 $code-&gt;() == 42;</pre>
560 </p>
561 <hr />
562 <h1><a name="warning">WARNING</a></h1>
563 <p>If you're using references as keys within your hash tables, you're bound
564 to be disappointed when retrieving your data. Indeed, Perl stringifies
565 references used as hash table keys. If you later wish to access the
566 items via another reference stringification (i.e. using the same
567 reference that was used for the key originally to record the value into
568 the hash table), it will work because both references stringify to the
569 same string.</p>
570 <p>It won't work across a sequence of <code>store</code> and <code>retrieve</code> operations,
571 however, because the addresses in the retrieved objects, which are
572 part of the stringified references, will probably differ from the
573 original addresses. The topology of your structure is preserved,
574 but not hidden semantics like those.</p>
575 <p>On platforms where it matters, be sure to call <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_binmode"><code>binmode()</code></a> on the
576 descriptors that you pass to Storable functions.</p>
577 <p>Storing data canonically that contains large hashes can be
578 significantly slower than storing the same data normally, as
579 temporary arrays to hold the keys for each hash have to be allocated,
580 populated, sorted and freed. Some tests have shown a halving of the
581 speed of storing -- the exact penalty will depend on the complexity of
582 your data. There is no slowdown on retrieval.</p>
584 </p>
585 <hr />
586 <h1><a name="bugs">BUGS</a></h1>
587 <p>You can't store GLOB, FORMLINE, etc.... If you can define semantics
588 for those operations, feel free to enhance Storable so that it can
589 deal with them.</p>
590 <p>The store functions will <code>croak</code> if they run into such references
591 unless you set <code>$Storable::forgive_me</code> to some <code>TRUE</code> value. In that
592 case, the fatal message is turned in a warning and some
593 meaningless string is stored instead.</p>
594 <p>Setting <code>$Storable::canonical</code> may not yield frozen strings that
595 compare equal due to possible stringification of numbers. When the
596 string version of a scalar exists, it is the form stored; therefore,
597 if you happen to use your numbers as strings between two freezing
598 operations on the same data structures, you will get different
599 results.</p>
600 <p>When storing doubles in network order, their value is stored as text.
601 However, you should also not expect non-numeric floating-point values
602 such as infinity and ``not a number'' to pass successfully through a
603 <code>nstore()/retrieve()</code> pair.</p>
604 <p>As Storable neither knows nor cares about character sets (although it
605 does know that characters may be more than eight bits wide), any difference
606 in the interpretation of character codes between a host and a target
607 system is your problem. In particular, if host and target use different
608 code points to represent the characters used in the text representation
609 of floating-point numbers, you will not be able be able to exchange
610 floating-point data, even with nstore().</p>
611 <p><code>Storable::drop_utf8</code> is a blunt tool. There is no facility either to
612 return <strong>all</strong> strings as utf8 sequences, or to attempt to convert utf8
613 data back to 8 bit and <code>croak()</code> if the conversion fails.</p>
614 <p>Prior to Storable 2.01, no distinction was made between signed and
615 unsigned integers on storing. By default Storable prefers to store a
616 scalars string representation (if it has one) so this would only cause
617 problems when storing large unsigned integers that had never been coverted
618 to string or floating point. In other words values that had been generated
619 by integer operations such as logic ops and then not used in any string or
620 arithmetic context before storing.</p>
622 </p>
623 <h2><a name="64_bit_data_in_perl_5_6_0_and_5_6_1">64 bit data in perl 5.6.0 and 5.6.1</a></h2>
624 <p>This section only applies to you if you have existing data written out
625 by Storable 2.02 or earlier on perl 5.6.0 or 5.6.1 on Unix or Linux which
626 has been configured with 64 bit integer support (not the default)
627 If you got a precompiled perl, rather than running Configure to build
628 your own perl from source, then it almost certainly does not affect you,
629 and you can stop reading now (unless you're curious). If you're using perl
630 on Windows it does not affect you.</p>
631 <p>Storable writes a file header which contains the sizes of various C
632 language types for the C compiler that built Storable (when not writing in
633 network order), and will refuse to load files written by a Storable not
634 on the same (or compatible) architecture. This check and a check on
635 machine byteorder is needed because the size of various fields in the file
636 are given by the sizes of the C language types, and so files written on
637 different architectures are incompatible. This is done for increased speed.
638 (When writing in network order, all fields are written out as standard
639 lengths, which allows full interworking, but takes longer to read and write)</p>
640 <p>Perl 5.6.x introduced the ability to optional configure the perl interpreter
641 to use C's <code>long long</code> type to allow scalars to store 64 bit integers on 32
642 bit systems. However, due to the way the Perl configuration system
643 generated the C configuration files on non-Windows platforms, and the way
644 Storable generates its header, nothing in the Storable file header reflected
645 whether the perl writing was using 32 or 64 bit integers, despite the fact
646 that Storable was storing some data differently in the file. Hence Storable
647 running on perl with 64 bit integers will read the header from a file
648 written by a 32 bit perl, not realise that the data is actually in a subtly
649 incompatible format, and then go horribly wrong (possibly crashing) if it
650 encountered a stored integer. This is a design failure.</p>
651 <p>Storable has now been changed to write out and read in a file header with
652 information about the size of integers. It's impossible to detect whether
653 an old file being read in was written with 32 or 64 bit integers (they have
654 the same header) so it's impossible to automatically switch to a correct
655 backwards compatibility mode. Hence this Storable defaults to the new,
656 correct behaviour.</p>
657 <p>What this means is that if you have data written by Storable 1.x running
658 on perl 5.6.0 or 5.6.1 configured with 64 bit integers on Unix or Linux
659 then by default this Storable will refuse to read it, giving the error
660 <em>Byte order is not compatible</em>. If you have such data then you you
661 should set <code>$Storable::interwork_56_64bit</code> to a true value to make this
662 Storable read and write files with the old header. You should also
663 migrate your data, or any older perl you are communicating with, to this
664 current version of Storable.</p>
665 <p>If you don't have data written with specific configuration of perl described
666 above, then you do not and should not do anything. Don't set the flag -
667 not only will Storable on an identically configured perl refuse to load them,
668 but Storable a differently configured perl will load them believing them
669 to be correct for it, and then may well fail or crash part way through
670 reading them.</p>
672 </p>
673 <hr />
674 <h1><a name="credits">CREDITS</a></h1>
675 <p>Thank you to (in chronological order):</p>
676 <pre>
677 Jarkko Hietaniemi &lt;jhi@iki.fi&gt;
678 Ulrich Pfeifer &lt;pfeifer@charly.informatik.uni-dortmund.de&gt;
679 Benjamin A. Holzman &lt;bah@ecnvantage.com&gt;
680 Andrew Ford &lt;A.Ford@ford-mason.co.uk&gt;
681 Gisle Aas &lt;gisle@aas.no&gt;
682 Jeff Gresham &lt;gresham_jeffrey@jpmorgan.com&gt;
683 Murray Nesbitt &lt;murray@activestate.com&gt;
684 Marc Lehmann &lt;pcg@opengroup.org&gt;
685 Justin Banks &lt;justinb@wamnet.com&gt;
686 Jarkko Hietaniemi &lt;jhi@iki.fi&gt; (AGAIN, as perl 5.7.0 Pumpkin!)
687 Salvador Ortiz Garcia &lt;sog@msg.com.mx&gt;
688 Dominic Dunlop &lt;domo@computer.org&gt;
689 Erik Haugan &lt;erik@solbors.no&gt;</pre>
690 <p>for their bug reports, suggestions and contributions.</p>
691 <p>Benjamin Holzman contributed the tied variable support, Andrew Ford
692 contributed the canonical order for hashes, and Gisle Aas fixed
693 a few misunderstandings of mine regarding the perl internals,
694 and optimized the emission of ``tags'' in the output streams by
695 simply counting the objects instead of tagging them (leading to
696 a binary incompatibility for the Storable image starting at version
697 0.6--older images are, of course, still properly understood).
698 Murray Nesbitt made Storable thread-safe. Marc Lehmann added overloading
699 and references to tied items support.</p>
701 </p>
702 <hr />
703 <h1><a name="author">AUTHOR</a></h1>
704 <p>Storable was written by Raphael Manfredi <em>&lt;<a href="mailto:Raphael_Manfredi@pobox.com">Raphael_Manfredi@pobox.com</a>&gt;</em>
705 Maintenance is now done by the perl5-porters <em>&lt;<a href="mailto:perl5-porters@perl.org">perl5-porters@perl.org</a>&gt;</em></p>
706 <p>Please e-mail us with problems, bug fixes, comments and complaints,
707 although if you have complements you should send them to Raphael.
708 Please don't e-mail Raphael with problems, as he no longer works on
709 Storable, and your message will be delayed while he forwards it to us.</p>
711 </p>
712 <hr />
713 <h1><a name="see_also">SEE ALSO</a></h1>
714 <p><em>Clone</em>.</p>
715 <table border="0" width="100%" cellspacing="0" cellpadding="3">
716 <tr><td class="block" style="background-color: #cccccc" valign="middle">
717 <big><strong><span class="block">&nbsp;Storable - persistence for Perl data structures</span></strong></big>
718 </td></tr>
719 </table>
721 </body>
723 </html>