Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Storable.3pm
1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 'br\}
42 .\"
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
44 .ie \n(.g .ds Aq \(aq
45 .el       .ds Aq '
46 .\"
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD.  Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
51 .ie \nF \{\
52 .    de IX
53 .    tm Index:\\$1\t\\n%\t"\\$2"
54 ..
55 .    nr % 0
56 .    rr F
57 .\}
58 .el \{\
59 .    de IX
60 ..
61 .\}
62 .\"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
65 .    \" fudge factors for nroff and troff
66 .if n \{\
67 .    ds #H 0
68 .    ds #V .8m
69 .    ds #F .3m
70 .    ds #[ \f1
71 .    ds #] \fP
72 .\}
73 .if t \{\
74 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
75 .    ds #V .6m
76 .    ds #F 0
77 .    ds #[ \&
78 .    ds #] \&
79 .\}
80 .    \" simple accents for nroff and troff
81 .if n \{\
82 .    ds ' \&
83 .    ds ` \&
84 .    ds ^ \&
85 .    ds , \&
86 .    ds ~ ~
87 .    ds /
88 .\}
89 .if t \{\
90 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
96 .\}
97 .    \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 .    \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 .    \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
112 \{\
113 .    ds : e
114 .    ds 8 ss
115 .    ds o a
116 .    ds d- d\h'-1'\(ga
117 .    ds D- D\h'-1'\(hy
118 .    ds th \o'bp'
119 .    ds Th \o'LP'
120 .    ds ae ae
121 .    ds Ae AE
122 .\}
123 .rm #[ #] #H #V #F C
124 .\" ========================================================================
125 .\"
126 .IX Title "Storable 3"
127 .TH Storable 3 "2009-08-06" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
130 .if n .ad l
131 .nh
132 .SH "NAME"
133 Storable \- persistence for Perl data structures
134 .SH "SYNOPSIS"
135 .IX Header "SYNOPSIS"
136 .Vb 3
137 \& use Storable;
138 \& store \e%table, \*(Aqfile\*(Aq;
139 \& $hashref = retrieve(\*(Aqfile\*(Aq);
140 \&
141 \& use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);
142 \&
143 \& # Network order
144 \& nstore \e%table, \*(Aqfile\*(Aq;
145 \& $hashref = retrieve(\*(Aqfile\*(Aq);   # There is NO nretrieve()
146 \&
147 \& # Storing to and retrieving from an already opened file
148 \& store_fd \e@array, \e*STDOUT;
149 \& nstore_fd \e%table, \e*STDOUT;
150 \& $aryref = fd_retrieve(\e*SOCKET);
151 \& $hashref = fd_retrieve(\e*SOCKET);
152 \&
153 \& # Serializing to memory
154 \& $serialized = freeze \e%table;
155 \& %table_clone = %{ thaw($serialized) };
156 \&
157 \& # Deep (recursive) cloning
158 \& $cloneref = dclone($ref);
159 \&
160 \& # Advisory locking
161 \& use Storable qw(lock_store lock_nstore lock_retrieve)
162 \& lock_store \e%table, \*(Aqfile\*(Aq;
163 \& lock_nstore \e%table, \*(Aqfile\*(Aq;
164 \& $hashref = lock_retrieve(\*(Aqfile\*(Aq);
165 .Ve
166 .SH "DESCRIPTION"
167 .IX Header "DESCRIPTION"
168 The Storable package brings persistence to your Perl data structures
169 containing \s-1SCALAR\s0, \s-1ARRAY\s0, \s-1HASH\s0 or \s-1REF\s0 objects, i.e. anything that can be
170 conveniently stored to disk and retrieved at a later time.
171 .PP
172 It can be used in the regular procedural way by calling \f(CW\*(C`store\*(C'\fR with
173 a reference to the object to be stored, along with the file name where
174 the image should be written.
175 .PP
176 The routine returns \f(CW\*(C`undef\*(C'\fR for I/O problems or other internal error,
177 a true value otherwise. Serious errors are propagated as a \f(CW\*(C`die\*(C'\fR exception.
178 .PP
179 To retrieve data stored to disk, use \f(CW\*(C`retrieve\*(C'\fR with a file name.
180 The objects stored into that file are recreated into memory for you,
181 and a \fIreference\fR to the root object is returned. In case an I/O error
182 occurs while reading, \f(CW\*(C`undef\*(C'\fR is returned instead. Other serious
183 errors are propagated via \f(CW\*(C`die\*(C'\fR.
184 .PP
185 Since storage is performed recursively, you might want to stuff references
186 to objects that share a lot of common data into a single array or hash
187 table, and then store that object. That way, when you retrieve back the
188 whole thing, the objects will continue to share what they originally shared.
189 .PP
190 At the cost of a slight header overhead, you may store to an already
191 opened file descriptor using the \f(CW\*(C`store_fd\*(C'\fR routine, and retrieve
192 from a file via \f(CW\*(C`fd_retrieve\*(C'\fR. Those names aren't imported by default,
193 so you will have to do that explicitly if you need those routines.
194 The file descriptor you supply must be already opened, for read
195 if you're going to retrieve and for write if you wish to store.
196 .PP
197 .Vb 2
198 \&        store_fd(\e%table, *STDOUT) || die "can\*(Aqt store to stdout\en";
199 \&        $hashref = fd_retrieve(*STDIN);
200 .Ve
201 .PP
202 You can also store data in network order to allow easy sharing across
203 multiple platforms, or when storing on a socket known to be remotely
204 connected. The routines to call have an initial \f(CW\*(C`n\*(C'\fR prefix for \fInetwork\fR,
205 as in \f(CW\*(C`nstore\*(C'\fR and \f(CW\*(C`nstore_fd\*(C'\fR. At retrieval time, your data will be
206 correctly restored so you don't have to know whether you're restoring
207 from native or network ordered data.  Double values are stored stringified
208 to ensure portability as well, at the slight risk of loosing some precision
209 in the last decimals.
210 .PP
211 When using \f(CW\*(C`fd_retrieve\*(C'\fR, objects are retrieved in sequence, one
212 object (i.e. one recursive tree) per associated \f(CW\*(C`store_fd\*(C'\fR.
213 .PP
214 If you're more from the object-oriented camp, you can inherit from
215 Storable and directly store your objects by invoking \f(CW\*(C`store\*(C'\fR as
216 a method. The fact that the root of the to-be-stored tree is a
217 blessed reference (i.e. an object) is special-cased so that the
218 retrieve does not provide a reference to that object but rather the
219 blessed object reference itself. (Otherwise, you'd get a reference
220 to that blessed object).
221 .SH "MEMORY STORE"
222 .IX Header "MEMORY STORE"
223 The Storable engine can also store data into a Perl scalar instead, to
224 later retrieve them. This is mainly used to freeze a complex structure in
225 some safe compact memory place (where it can possibly be sent to another
226 process via some \s-1IPC\s0, since freezing the structure also serializes it in
227 effect). Later on, and maybe somewhere else, you can thaw the Perl scalar
228 out and recreate the original complex structure in memory.
229 .PP
230 Surprisingly, the routines to be called are named \f(CW\*(C`freeze\*(C'\fR and \f(CW\*(C`thaw\*(C'\fR.
231 If you wish to send out the frozen scalar to another machine, use
232 \&\f(CW\*(C`nfreeze\*(C'\fR instead to get a portable image.
233 .PP
234 Note that freezing an object structure and immediately thawing it
235 actually achieves a deep cloning of that structure:
236 .PP
237 .Vb 1
238 \&    dclone(.) = thaw(freeze(.))
239 .Ve
240 .PP
241 Storable provides you with a \f(CW\*(C`dclone\*(C'\fR interface which does not create
242 that intermediary scalar but instead freezes the structure in some
243 internal memory space and then immediately thaws it out.
244 .SH "ADVISORY LOCKING"
245 .IX Header "ADVISORY LOCKING"
246 The \f(CW\*(C`lock_store\*(C'\fR and \f(CW\*(C`lock_nstore\*(C'\fR routine are equivalent to
247 \&\f(CW\*(C`store\*(C'\fR and \f(CW\*(C`nstore\*(C'\fR, except that they get an exclusive lock on
248 the file before writing.  Likewise, \f(CW\*(C`lock_retrieve\*(C'\fR does the same
249 as \f(CW\*(C`retrieve\*(C'\fR, but also gets a shared lock on the file before reading.
250 .PP
251 As with any advisory locking scheme, the protection only works if you
252 systematically use \f(CW\*(C`lock_store\*(C'\fR and \f(CW\*(C`lock_retrieve\*(C'\fR.  If one side of
253 your application uses \f(CW\*(C`store\*(C'\fR whilst the other uses \f(CW\*(C`lock_retrieve\*(C'\fR,
254 you will get no protection at all.
255 .PP
256 The internal advisory locking is implemented using Perl's \fIflock()\fR
257 routine.  If your system does not support any form of \fIflock()\fR, or if
258 you share your files across \s-1NFS\s0, you might wish to use other forms
259 of locking by using modules such as LockFile::Simple which lock a
260 file using a filesystem entry, instead of locking the file descriptor.
261 .SH "SPEED"
262 .IX Header "SPEED"
263 The heart of Storable is written in C for decent speed. Extra low-level
264 optimizations have been made when manipulating perl internals, to
265 sacrifice encapsulation for the benefit of greater speed.
266 .SH "CANONICAL REPRESENTATION"
267 .IX Header "CANONICAL REPRESENTATION"
268 Normally, Storable stores elements of hashes in the order they are
269 stored internally by Perl, i.e. pseudo-randomly.  If you set
270 \&\f(CW$Storable::canonical\fR to some \f(CW\*(C`TRUE\*(C'\fR value, Storable will store
271 hashes with the elements sorted by their key.  This allows you to
272 compare data structures by comparing their frozen representations (or
273 even the compressed frozen representations), which can be useful for
274 creating lookup tables for complicated queries.
275 .PP
276 Canonical order does not imply network order; those are two orthogonal
277 settings.
278 .SH "CODE REFERENCES"
279 .IX Header "CODE REFERENCES"
280 Since Storable version 2.05, \s-1CODE\s0 references may be serialized with
281 the help of B::Deparse. To enable this feature, set
282 \&\f(CW$Storable::Deparse\fR to a true value. To enable deserialization,
283 \&\f(CW$Storable::Eval\fR should be set to a true value. Be aware that
284 deserialization is done through \f(CW\*(C`eval\*(C'\fR, which is dangerous if the
285 Storable file contains malicious data. You can set \f(CW$Storable::Eval\fR
286 to a subroutine reference which would be used instead of \f(CW\*(C`eval\*(C'\fR. See
287 below for an example using a Safe compartment for deserialization
288 of \s-1CODE\s0 references.
289 .PP
290 If \f(CW$Storable::Deparse\fR and/or \f(CW$Storable::Eval\fR are set to false
291 values, then the value of \f(CW$Storable::forgive_me\fR (see below) is
292 respected while serializing and deserializing.
293 .SH "FORWARD COMPATIBILITY"
294 .IX Header "FORWARD COMPATIBILITY"
295 This release of Storable can be used on a newer version of Perl to
296 serialize data which is not supported by earlier Perls.  By default,
297 Storable will attempt to do the right thing, by \f(CW\*(C`croak()\*(C'\fRing if it
298 encounters data that it cannot deserialize.  However, the defaults
299 can be changed as follows:
300 .IP "utf8 data" 4
301 .IX Item "utf8 data"
302 Perl 5.6 added support for Unicode characters with code points > 255,
303 and Perl 5.8 has full support for Unicode characters in hash keys.
304 Perl internally encodes strings with these characters using utf8, and
305 Storable serializes them as utf8.  By default, if an older version of
306 Perl encounters a utf8 value it cannot represent, it will \f(CW\*(C`croak()\*(C'\fR.
307 To change this behaviour so that Storable deserializes utf8 encoded
308 values as the string of bytes (effectively dropping the \fIis_utf8\fR flag)
309 set \f(CW$Storable::drop_utf8\fR to some \f(CW\*(C`TRUE\*(C'\fR value.  This is a form of
310 data loss, because with \f(CW$drop_utf8\fR true, it becomes impossible to tell
311 whether the original data was the Unicode string, or a series of bytes
312 that happen to be valid utf8.
313 .IP "restricted hashes" 4
314 .IX Item "restricted hashes"
315 Perl 5.8 adds support for restricted hashes, which have keys
316 restricted to a given set, and can have values locked to be read only.
317 By default, when Storable encounters a restricted hash on a perl
318 that doesn't support them, it will deserialize it as a normal hash,
319 silently discarding any placeholder keys and leaving the keys and
320 all values unlocked.  To make Storable \f(CW\*(C`croak()\*(C'\fR instead, set
321 \&\f(CW$Storable::downgrade_restricted\fR to a \f(CW\*(C`FALSE\*(C'\fR value.  To restore
322 the default set it back to some \f(CW\*(C`TRUE\*(C'\fR value.
323 .IP "files from future versions of Storable" 4
324 .IX Item "files from future versions of Storable"
325 Earlier versions of Storable would immediately croak if they encountered
326 a file with a higher internal version number than the reading Storable
327 knew about.  Internal version numbers are increased each time new data
328 types (such as restricted hashes) are added to the vocabulary of the file
329 format.  This meant that a newer Storable module had no way of writing a
330 file readable by an older Storable, even if the writer didn't store newer
331 data types.
332 .Sp
333 This version of Storable will defer croaking until it encounters a data
334 type in the file that it does not recognize.  This means that it will
335 continue to read files generated by newer Storable modules which are careful
336 in what they write out, making it easier to upgrade Storable modules in a
337 mixed environment.
338 .Sp
339 The old behaviour of immediate croaking can be re-instated by setting
340 \&\f(CW$Storable::accept_future_minor\fR to some \f(CW\*(C`FALSE\*(C'\fR value.
341 .PP
342 All these variables have no effect on a newer Perl which supports the
343 relevant feature.
344 .SH "ERROR REPORTING"
345 .IX Header "ERROR REPORTING"
346 Storable uses the \*(L"exception\*(R" paradigm, in that it does not try to workaround
347 failures: if something bad happens, an exception is generated from the
348 caller's perspective (see Carp and \f(CW\*(C`croak()\*(C'\fR).  Use eval {} to trap
349 those exceptions.
350 .PP
351 When Storable croaks, it tries to report the error via the \f(CW\*(C`logcroak()\*(C'\fR
352 routine from the \f(CW\*(C`Log::Agent\*(C'\fR package, if it is available.
353 .PP
354 Normal errors are reported by having \fIstore()\fR or \fIretrieve()\fR return \f(CW\*(C`undef\*(C'\fR.
355 Such errors are usually I/O errors (or truncated stream errors at retrieval).
356 .SH "WIZARDS ONLY"
357 .IX Header "WIZARDS ONLY"
358 .SS "Hooks"
359 .IX Subsection "Hooks"
360 Any class may define hooks that will be called during the serialization
361 and deserialization process on objects that are instances of that class.
362 Those hooks can redefine the way serialization is performed (and therefore,
363 how the symmetrical deserialization should be conducted).
364 .PP
365 Since we said earlier:
366 .PP
367 .Vb 1
368 \&    dclone(.) = thaw(freeze(.))
369 .Ve
370 .PP
371 everything we say about hooks should also hold for deep cloning. However,
372 hooks get to know whether the operation is a mere serialization, or a cloning.
373 .PP
374 Therefore, when serializing hooks are involved,
375 .PP
376 .Vb 1
377 \&    dclone(.) <> thaw(freeze(.))
378 .Ve
379 .PP
380 Well, you could keep them in sync, but there's no guarantee it will always
381 hold on classes somebody else wrote.  Besides, there is little to gain in
382 doing so: a serializing hook could keep only one attribute of an object,
383 which is probably not what should happen during a deep cloning of that
384 same object.
385 .PP
386 Here is the hooking interface:
387 .ie n .IP """STORABLE_freeze"" \fIobj\fR, \fIcloning\fR" 4
388 .el .IP "\f(CWSTORABLE_freeze\fR \fIobj\fR, \fIcloning\fR" 4
389 .IX Item "STORABLE_freeze obj, cloning"
390 The serializing hook, called on the object during serialization.  It can be
391 inherited, or defined in the class itself, like any other method.
392 .Sp
393 Arguments: \fIobj\fR is the object to serialize, \fIcloning\fR is a flag indicating
394 whether we're in a \fIdclone()\fR or a regular serialization via \fIstore()\fR or \fIfreeze()\fR.
395 .Sp
396 Returned value: A \s-1LIST\s0 \f(CW\*(C`($serialized, $ref1, $ref2, ...)\*(C'\fR where \f(CW$serialized\fR
397 is the serialized form to be used, and the optional \f(CW$ref1\fR, \f(CW$ref2\fR, etc... are
398 extra references that you wish to let the Storable engine serialize.
399 .Sp
400 At deserialization time, you will be given back the same \s-1LIST\s0, but all the
401 extra references will be pointing into the deserialized structure.
402 .Sp
403 The \fBfirst time\fR the hook is hit in a serialization flow, you may have it
404 return an empty list.  That will signal the Storable engine to further
405 discard that hook for this class and to therefore revert to the default
406 serialization of the underlying Perl data.  The hook will again be normally
407 processed in the next serialization.
408 .Sp
409 Unless you know better, serializing hook should always say:
410 .Sp
411 .Vb 5
412 \&    sub STORABLE_freeze {
413 \&        my ($self, $cloning) = @_;
414 \&        return if $cloning;         # Regular default serialization
415 \&        ....
416 \&    }
417 .Ve
418 .Sp
419 in order to keep reasonable \fIdclone()\fR semantics.
420 .ie n .IP """STORABLE_thaw"" \fIobj\fR, \fIcloning\fR, \fIserialized\fR, ..." 4
421 .el .IP "\f(CWSTORABLE_thaw\fR \fIobj\fR, \fIcloning\fR, \fIserialized\fR, ..." 4
422 .IX Item "STORABLE_thaw obj, cloning, serialized, ..."
423 The deserializing hook called on the object during deserialization.
424 But wait: if we're deserializing, there's no object yet... right?
425 .Sp
426 Wrong: the Storable engine creates an empty one for you.  If you know Eiffel,
427 you can view \f(CW\*(C`STORABLE_thaw\*(C'\fR as an alternate creation routine.
428 .Sp
429 This means the hook can be inherited like any other method, and that
430 \&\fIobj\fR is your blessed reference for this particular instance.
431 .Sp
432 The other arguments should look familiar if you know \f(CW\*(C`STORABLE_freeze\*(C'\fR:
433 \&\fIcloning\fR is true when we're part of a deep clone operation, \fIserialized\fR
434 is the serialized string you returned to the engine in \f(CW\*(C`STORABLE_freeze\*(C'\fR,
435 and there may be an optional list of references, in the same order you gave
436 them at serialization time, pointing to the deserialized objects (which
437 have been processed courtesy of the Storable engine).
438 .Sp
439 When the Storable engine does not find any \f(CW\*(C`STORABLE_thaw\*(C'\fR hook routine,
440 it tries to load the class by requiring the package dynamically (using
441 the blessed package name), and then re-attempts the lookup.  If at that
442 time the hook cannot be located, the engine croaks.  Note that this mechanism
443 will fail if you define several classes in the same file, but perlmod
444 warned you.
445 .Sp
446 It is up to you to use this information to populate \fIobj\fR the way you want.
447 .Sp
448 Returned value: none.
449 .ie n .IP """STORABLE_attach"" \fIclass\fR, \fIcloning\fR, \fIserialized\fR" 4
450 .el .IP "\f(CWSTORABLE_attach\fR \fIclass\fR, \fIcloning\fR, \fIserialized\fR" 4
451 .IX Item "STORABLE_attach class, cloning, serialized"
452 While \f(CW\*(C`STORABLE_freeze\*(C'\fR and \f(CW\*(C`STORABLE_thaw\*(C'\fR are useful for classes where
453 each instance is independent, this mechanism has difficulty (or is
454 incompatible) with objects that exist as common process-level or
455 system-level resources, such as singleton objects, database pools, caches
456 or memoized objects.
457 .Sp
458 The alternative \f(CW\*(C`STORABLE_attach\*(C'\fR method provides a solution for these
459 shared objects. Instead of \f(CW\*(C`STORABLE_freeze\*(C'\fR \-\-> \f(CW\*(C`STORABLE_thaw\*(C'\fR,
460 you implement \f(CW\*(C`STORABLE_freeze\*(C'\fR \-\-> \f(CW\*(C`STORABLE_attach\*(C'\fR instead.
461 .Sp
462 Arguments: \fIclass\fR is the class we are attaching to, \fIcloning\fR is a flag
463 indicating whether we're in a \fIdclone()\fR or a regular de-serialization via
464 \&\fIthaw()\fR, and \fIserialized\fR is the stored string for the resource object.
465 .Sp
466 Because these resource objects are considered to be owned by the entire
467 process/system, and not the \*(L"property\*(R" of whatever is being serialized,
468 no references underneath the object should be included in the serialized
469 string. Thus, in any class that implements \f(CW\*(C`STORABLE_attach\*(C'\fR, the
470 \&\f(CW\*(C`STORABLE_freeze\*(C'\fR method cannot return any references, and \f(CW\*(C`Storable\*(C'\fR
471 will throw an error if \f(CW\*(C`STORABLE_freeze\*(C'\fR tries to return references.
472 .Sp
473 All information required to \*(L"attach\*(R" back to the shared resource object
474 \&\fBmust\fR be contained \fBonly\fR in the \f(CW\*(C`STORABLE_freeze\*(C'\fR return string.
475 Otherwise, \f(CW\*(C`STORABLE_freeze\*(C'\fR behaves as normal for \f(CW\*(C`STORABLE_attach\*(C'\fR
476 classes.
477 .Sp
478 Because \f(CW\*(C`STORABLE_attach\*(C'\fR is passed the class (rather than an object),
479 it also returns the object directly, rather than modifying the passed
480 object.
481 .Sp
482 Returned value: object of type \f(CW\*(C`class\*(C'\fR
483 .SS "Predicates"
484 .IX Subsection "Predicates"
485 Predicates are not exportable.  They must be called by explicitly prefixing
486 them with the Storable package name.
487 .ie n .IP """Storable::last_op_in_netorder""" 4
488 .el .IP "\f(CWStorable::last_op_in_netorder\fR" 4
489 .IX Item "Storable::last_op_in_netorder"
490 The \f(CW\*(C`Storable::last_op_in_netorder()\*(C'\fR predicate will tell you whether
491 network order was used in the last store or retrieve operation.  If you
492 don't know how to use this, just forget about it.
493 .ie n .IP """Storable::is_storing""" 4
494 .el .IP "\f(CWStorable::is_storing\fR" 4
495 .IX Item "Storable::is_storing"
496 Returns true if within a store operation (via STORABLE_freeze hook).
497 .ie n .IP """Storable::is_retrieving""" 4
498 .el .IP "\f(CWStorable::is_retrieving\fR" 4
499 .IX Item "Storable::is_retrieving"
500 Returns true if within a retrieve operation (via STORABLE_thaw hook).
501 .SS "Recursion"
502 .IX Subsection "Recursion"
503 With hooks comes the ability to recurse back to the Storable engine.
504 Indeed, hooks are regular Perl code, and Storable is convenient when
505 it comes to serializing and deserializing things, so why not use it
506 to handle the serialization string?
507 .PP
508 There are a few things you need to know, however:
509 .IP "\(bu" 4
510 You can create endless loops if the things you serialize via \fIfreeze()\fR
511 (for instance) point back to the object we're trying to serialize in
512 the hook.
513 .IP "\(bu" 4
514 Shared references among objects will not stay shared: if we're serializing
515 the list of object [A, C] where both object A and C refer to the \s-1SAME\s0 object
516 B, and if there is a serializing hook in A that says freeze(B), then when
517 deserializing, we'll get [A', C'] where A' refers to B', but C' refers to D,
518 a deep clone of B'.  The topology was not preserved.
519 .PP
520 That's why \f(CW\*(C`STORABLE_freeze\*(C'\fR lets you provide a list of references
521 to serialize.  The engine guarantees that those will be serialized in the
522 same context as the other objects, and therefore that shared objects will
523 stay shared.
524 .PP
525 In the above [A, C] example, the \f(CW\*(C`STORABLE_freeze\*(C'\fR hook could return:
526 .PP
527 .Vb 1
528 \&        ("something", $self\->{B})
529 .Ve
530 .PP
531 and the B part would be serialized by the engine.  In \f(CW\*(C`STORABLE_thaw\*(C'\fR, you
532 would get back the reference to the B' object, deserialized for you.
533 .PP
534 Therefore, recursion should normally be avoided, but is nonetheless supported.
535 .SS "Deep Cloning"
536 .IX Subsection "Deep Cloning"
537 There is a Clone module available on \s-1CPAN\s0 which implements deep cloning
538 natively, i.e. without freezing to memory and thawing the result.  It is
539 aimed to replace Storable's \fIdclone()\fR some day.  However, it does not currently
540 support Storable hooks to redefine the way deep cloning is performed.
541 .SH "Storable magic"
542 .IX Header "Storable magic"
543 Yes, there's a lot of that :\-) But more precisely, in \s-1UNIX\s0 systems
544 there's a utility called \f(CW\*(C`file\*(C'\fR, which recognizes data files based on
545 their contents (usually their first few bytes).  For this to work,
546 a certain file called \fImagic\fR needs to taught about the \fIsignature\fR
547 of the data.  Where that configuration file lives depends on the \s-1UNIX\s0
548 flavour; often it's something like \fI/usr/share/misc/magic\fR or
549 \&\fI/etc/magic\fR.  Your system administrator needs to do the updating of
550 the \fImagic\fR file.  The necessary signature information is output to
551 \&\s-1STDOUT\s0 by invoking \fIStorable::show_file_magic()\fR.  Note that the \s-1GNU\s0
552 implementation of the \f(CW\*(C`file\*(C'\fR utility, version 3.38 or later,
553 is expected to contain support for recognising Storable files
554 out-of-the-box, in addition to other kinds of Perl files.
555 .PP
556 You can also use the following functions to extract the file header
557 information from Storable images:
558 .ie n .IP "$info = Storable::file_magic( $filename )" 4
559 .el .IP "\f(CW$info\fR = Storable::file_magic( \f(CW$filename\fR )" 4
560 .IX Item "$info = Storable::file_magic( $filename )"
561 If the given file is a Storable image return a hash describing it.  If
562 the file is readable, but not a Storable image return \f(CW\*(C`undef\*(C'\fR.  If
563 the file does not exist or is unreadable then croak.
564 .Sp
565 The hash returned has the following elements:
566 .RS 4
567 .ie n .IP """version""" 4
568 .el .IP "\f(CWversion\fR" 4
569 .IX Item "version"
570 This returns the file format version.  It is a string like \*(L"2.7\*(R".
571 .Sp
572 Note that this version number is not the same as the version number of
573 the Storable module itself.  For instance Storable v0.7 create files
574 in format v2.0 and Storable v2.15 create files in format v2.7.  The
575 file format version number only increment when additional features
576 that would confuse older versions of the module are added.
577 .Sp
578 Files older than v2.0 will have the one of the version numbers \*(L"\-1\*(R",
579 \&\*(L"0\*(R" or \*(L"1\*(R".  No minor number was used at that time.
580 .ie n .IP """version_nv""" 4
581 .el .IP "\f(CWversion_nv\fR" 4
582 .IX Item "version_nv"
583 This returns the file format version as number.  It is a string like
584 \&\*(L"2.007\*(R".  This value is suitable for numeric comparisons.
585 .Sp
586 The constant function \f(CW\*(C`Storable::BIN_VERSION_NV\*(C'\fR returns a comparable
587 number that represent the highest file version number that this
588 version of Storable fully support (but see discussion of
589 \&\f(CW$Storable::accept_future_minor\fR above).  The constant
590 \&\f(CW\*(C`Storable::BIN_WRITE_VERSION_NV\*(C'\fR function returns what file version
591 is written and might be less than \f(CW\*(C`Storable::BIN_VERSION_NV\*(C'\fR in some
592 configuations.
593 .ie n .IP """major"", ""minor""" 4
594 .el .IP "\f(CWmajor\fR, \f(CWminor\fR" 4
595 .IX Item "major, minor"
596 This also returns the file format version.  If the version is \*(L"2.7\*(R"
597 then major would be 2 and minor would be 7.  The minor element is
598 missing for when major is less than 2.
599 .ie n .IP """hdrsize""" 4
600 .el .IP "\f(CWhdrsize\fR" 4
601 .IX Item "hdrsize"
602 The is the number of bytes that the Storable header occupies.
603 .ie n .IP """netorder""" 4
604 .el .IP "\f(CWnetorder\fR" 4
605 .IX Item "netorder"
606 This is \s-1TRUE\s0 if the image store data in network order.  This means
607 that it was created with \fInstore()\fR or similar.
608 .ie n .IP """byteorder""" 4
609 .el .IP "\f(CWbyteorder\fR" 4
610 .IX Item "byteorder"
611 This is only present when \f(CW\*(C`netorder\*(C'\fR is \s-1FALSE\s0.  It is the
612 \&\f(CW$Config\fR{byteorder} string of the perl that created this image.  It is
613 a string like \*(L"1234\*(R" (32 bit little endian) or \*(L"87654321\*(R" (64 bit big
614 endian).  This must match the current perl for the image to be
615 readable by Storable.
616 .ie n .IP """intsize"", ""longsize"", ""ptrsize"", ""nvsize""" 4
617 .el .IP "\f(CWintsize\fR, \f(CWlongsize\fR, \f(CWptrsize\fR, \f(CWnvsize\fR" 4
618 .IX Item "intsize, longsize, ptrsize, nvsize"
619 These are only present when \f(CW\*(C`netorder\*(C'\fR is \s-1FALSE\s0. These are the sizes of
620 various C datatypes of the perl that created this image.  These must
621 match the current perl for the image to be readable by Storable.
622 .Sp
623 The \f(CW\*(C`nvsize\*(C'\fR element is only present for file format v2.2 and
624 higher.
625 .ie n .IP """file""" 4
626 .el .IP "\f(CWfile\fR" 4
627 .IX Item "file"
628 The name of the file.
629 .RE
630 .RS 4
631 .RE
632 .ie n .IP "$info = Storable::read_magic( $buffer )" 4
633 .el .IP "\f(CW$info\fR = Storable::read_magic( \f(CW$buffer\fR )" 4
634 .IX Item "$info = Storable::read_magic( $buffer )"
635 .PD 0
636 .ie n .IP "$info = Storable::read_magic( $buffer, $must_be_file )" 4
637 .el .IP "\f(CW$info\fR = Storable::read_magic( \f(CW$buffer\fR, \f(CW$must_be_file\fR )" 4
638 .IX Item "$info = Storable::read_magic( $buffer, $must_be_file )"
639 .PD
640 The \f(CW$buffer\fR should be a Storable image or the first few bytes of it.
641 If \f(CW$buffer\fR starts with a Storable header, then a hash describing the
642 image is returned, otherwise \f(CW\*(C`undef\*(C'\fR is returned.
643 .Sp
644 The hash has the same structure as the one returned by
645 \&\fIStorable::file_magic()\fR.  The \f(CW\*(C`file\*(C'\fR element is true if the image is a
646 file image.
647 .Sp
648 If the \f(CW$must_be_file\fR argument is provided and is \s-1TRUE\s0, then return
649 \&\f(CW\*(C`undef\*(C'\fR unless the image looks like it belongs to a file dump.
650 .Sp
651 The maximum size of a Storable header is currently 21 bytes.  If the
652 provided \f(CW$buffer\fR is only the first part of a Storable image it should
653 at least be this long to ensure that \fIread_magic()\fR will recognize it as
654 such.
655 .SH "EXAMPLES"
656 .IX Header "EXAMPLES"
657 Here are some code samples showing a possible usage of Storable:
658 .PP
659 .Vb 1
660 \&        use Storable qw(store retrieve freeze thaw dclone);
661 \&
662 \&        %color = (\*(AqBlue\*(Aq => 0.1, \*(AqRed\*(Aq => 0.8, \*(AqBlack\*(Aq => 0, \*(AqWhite\*(Aq => 1);
663 \&
664 \&        store(\e%color, \*(Aqmycolors\*(Aq) or die "Can\*(Aqt store %a in mycolors!\en";
665 \&
666 \&        $colref = retrieve(\*(Aqmycolors\*(Aq);
667 \&        die "Unable to retrieve from mycolors!\en" unless defined $colref;
668 \&        printf "Blue is still %lf\en", $colref\->{\*(AqBlue\*(Aq};
669 \&
670 \&        $colref2 = dclone(\e%color);
671 \&
672 \&        $str = freeze(\e%color);
673 \&        printf "Serialization of %%color is %d bytes long.\en", length($str);
674 \&        $colref3 = thaw($str);
675 .Ve
676 .PP
677 which prints (on my machine):
678 .PP
679 .Vb 2
680 \&        Blue is still 0.100000
681 \&        Serialization of %color is 102 bytes long.
682 .Ve
683 .PP
684 Serialization of \s-1CODE\s0 references and deserialization in a safe
685 compartment:
686 .PP
687 .Vb 11
688 \&        use Storable qw(freeze thaw);
689 \&        use Safe;
690 \&        use strict;
691 \&        my $safe = new Safe;
692 \&        # because of opcodes used in "use strict":
693 \&        $safe\->permit(qw(:default require));
694 \&        local $Storable::Deparse = 1;
695 \&        local $Storable::Eval = sub { $safe\->reval($_[0]) };
696 \&        my $serialized = freeze(sub { 42 });
697 \&        my $code = thaw($serialized);
698 \&        $code\->() == 42;
699 .Ve
700 .SH "WARNING"
701 .IX Header "WARNING"
702 If you're using references as keys within your hash tables, you're bound
703 to be disappointed when retrieving your data. Indeed, Perl stringifies
704 references used as hash table keys. If you later wish to access the
705 items via another reference stringification (i.e. using the same
706 reference that was used for the key originally to record the value into
707 the hash table), it will work because both references stringify to the
708 same string.
709 .PP
710 It won't work across a sequence of \f(CW\*(C`store\*(C'\fR and \f(CW\*(C`retrieve\*(C'\fR operations,
711 however, because the addresses in the retrieved objects, which are
712 part of the stringified references, will probably differ from the
713 original addresses. The topology of your structure is preserved,
714 but not hidden semantics like those.
715 .PP
716 On platforms where it matters, be sure to call \f(CW\*(C`binmode()\*(C'\fR on the
717 descriptors that you pass to Storable functions.
718 .PP
719 Storing data canonically that contains large hashes can be
720 significantly slower than storing the same data normally, as
721 temporary arrays to hold the keys for each hash have to be allocated,
722 populated, sorted and freed.  Some tests have shown a halving of the
723 speed of storing \*(-- the exact penalty will depend on the complexity of
724 your data.  There is no slowdown on retrieval.
725 .SH "BUGS"
726 .IX Header "BUGS"
727 You can't store \s-1GLOB\s0, \s-1FORMLINE\s0, etc.... If you can define semantics
728 for those operations, feel free to enhance Storable so that it can
729 deal with them.
730 .PP
731 The store functions will \f(CW\*(C`croak\*(C'\fR if they run into such references
732 unless you set \f(CW$Storable::forgive_me\fR to some \f(CW\*(C`TRUE\*(C'\fR value. In that
733 case, the fatal message is turned in a warning and some
734 meaningless string is stored instead.
735 .PP
736 Setting \f(CW$Storable::canonical\fR may not yield frozen strings that
737 compare equal due to possible stringification of numbers. When the
738 string version of a scalar exists, it is the form stored; therefore,
739 if you happen to use your numbers as strings between two freezing
740 operations on the same data structures, you will get different
741 results.
742 .PP
743 When storing doubles in network order, their value is stored as text.
744 However, you should also not expect non-numeric floating-point values
745 such as infinity and \*(L"not a number\*(R" to pass successfully through a
746 \&\fInstore()\fR/\fIretrieve()\fR pair.
747 .PP
748 As Storable neither knows nor cares about character sets (although it
749 does know that characters may be more than eight bits wide), any difference
750 in the interpretation of character codes between a host and a target
751 system is your problem.  In particular, if host and target use different
752 code points to represent the characters used in the text representation
753 of floating-point numbers, you will not be able be able to exchange
754 floating-point data, even with \fInstore()\fR.
755 .PP
756 \&\f(CW\*(C`Storable::drop_utf8\*(C'\fR is a blunt tool.  There is no facility either to
757 return \fBall\fR strings as utf8 sequences, or to attempt to convert utf8
758 data back to 8 bit and \f(CW\*(C`croak()\*(C'\fR if the conversion fails.
759 .PP
760 Prior to Storable 2.01, no distinction was made between signed and
761 unsigned integers on storing.  By default Storable prefers to store a
762 scalars string representation (if it has one) so this would only cause
763 problems when storing large unsigned integers that had never been converted
764 to string or floating point.  In other words values that had been generated
765 by integer operations such as logic ops and then not used in any string or
766 arithmetic context before storing.
767 .SS "64 bit data in perl 5.6.0 and 5.6.1"
768 .IX Subsection "64 bit data in perl 5.6.0 and 5.6.1"
769 This section only applies to you if you have existing data written out
770 by Storable 2.02 or earlier on perl 5.6.0 or 5.6.1 on Unix or Linux which
771 has been configured with 64 bit integer support (not the default)
772 If you got a precompiled perl, rather than running Configure to build
773 your own perl from source, then it almost certainly does not affect you,
774 and you can stop reading now (unless you're curious). If you're using perl
775 on Windows it does not affect you.
776 .PP
777 Storable writes a file header which contains the sizes of various C
778 language types for the C compiler that built Storable (when not writing in
779 network order), and will refuse to load files written by a Storable not
780 on the same (or compatible) architecture.  This check and a check on
781 machine byteorder is needed because the size of various fields in the file
782 are given by the sizes of the C language types, and so files written on
783 different architectures are incompatible.  This is done for increased speed.
784 (When writing in network order, all fields are written out as standard
785 lengths, which allows full interworking, but takes longer to read and write)
786 .PP
787 Perl 5.6.x introduced the ability to optional configure the perl interpreter
788 to use C's \f(CW\*(C`long long\*(C'\fR type to allow scalars to store 64 bit integers on 32
789 bit systems.  However, due to the way the Perl configuration system
790 generated the C configuration files on non-Windows platforms, and the way
791 Storable generates its header, nothing in the Storable file header reflected
792 whether the perl writing was using 32 or 64 bit integers, despite the fact
793 that Storable was storing some data differently in the file.  Hence Storable
794 running on perl with 64 bit integers will read the header from a file
795 written by a 32 bit perl, not realise that the data is actually in a subtly
796 incompatible format, and then go horribly wrong (possibly crashing) if it
797 encountered a stored integer.  This is a design failure.
798 .PP
799 Storable has now been changed to write out and read in a file header with
800 information about the size of integers.  It's impossible to detect whether
801 an old file being read in was written with 32 or 64 bit integers (they have
802 the same header) so it's impossible to automatically switch to a correct
803 backwards compatibility mode.  Hence this Storable defaults to the new,
804 correct behaviour.
805 .PP
806 What this means is that if you have data written by Storable 1.x running
807 on perl 5.6.0 or 5.6.1 configured with 64 bit integers on Unix or Linux
808 then by default this Storable will refuse to read it, giving the error
809 \&\fIByte order is not compatible\fR.  If you have such data then you you
810 should set \f(CW$Storable::interwork_56_64bit\fR to a true value to make this
811 Storable read and write files with the old header.  You should also
812 migrate your data, or any older perl you are communicating with, to this
813 current version of Storable.
814 .PP
815 If you don't have data written with specific configuration of perl described
816 above, then you do not and should not do anything.  Don't set the flag \-
817 not only will Storable on an identically configured perl refuse to load them,
818 but Storable a differently configured perl will load them believing them
819 to be correct for it, and then may well fail or crash part way through
820 reading them.
821 .SH "CREDITS"
822 .IX Header "CREDITS"
823 Thank you to (in chronological order):
824 .PP
825 .Vb 10
826 \&        Jarkko Hietaniemi <jhi@iki.fi>
827 \&        Ulrich Pfeifer <pfeifer@charly.informatik.uni\-dortmund.de>
828 \&        Benjamin A. Holzman <bah@ecnvantage.com>
829 \&        Andrew Ford <A.Ford@ford\-mason.co.uk>
830 \&        Gisle Aas <gisle@aas.no>
831 \&        Jeff Gresham <gresham_jeffrey@jpmorgan.com>
832 \&        Murray Nesbitt <murray@activestate.com>
833 \&        Marc Lehmann <pcg@opengroup.org>
834 \&        Justin Banks <justinb@wamnet.com>
835 \&        Jarkko Hietaniemi <jhi@iki.fi> (AGAIN, as perl 5.7.0 Pumpkin!)
836 \&        Salvador Ortiz Garcia <sog@msg.com.mx>
837 \&        Dominic Dunlop <domo@computer.org>
838 \&        Erik Haugan <erik@solbors.no>
839 .Ve
840 .PP
841 for their bug reports, suggestions and contributions.
842 .PP
843 Benjamin Holzman contributed the tied variable support, Andrew Ford
844 contributed the canonical order for hashes, and Gisle Aas fixed
845 a few misunderstandings of mine regarding the perl internals,
846 and optimized the emission of \*(L"tags\*(R" in the output streams by
847 simply counting the objects instead of tagging them (leading to
848 a binary incompatibility for the Storable image starting at version
849 0.6\-\-older images are, of course, still properly understood).
850 Murray Nesbitt made Storable thread-safe.  Marc Lehmann added overloading
851 and references to tied items support.
852 .SH "AUTHOR"
853 .IX Header "AUTHOR"
854 Storable was written by Raphael Manfredi \fI<Raphael_Manfredi@pobox.com>\fR
855 Maintenance is now done by the perl5\-porters \fI<perl5\-porters@perl.org>\fR
856 .PP
857 Please e\-mail us with problems, bug fixes, comments and complaints,
858 although if you have compliments you should send them to Raphael.
859 Please don't e\-mail Raphael with problems, as he no longer works on
860 Storable, and your message will be delayed while he forwards it to us.
861 .SH "SEE ALSO"
862 .IX Header "SEE ALSO"
863 Clone.