File: Storable.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (1127 lines) | stat: -rw-r--r-- 65,561 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Storable - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>Storable</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-modules-S.html">Core modules (S)</a> &gt;
      
    
    Storable
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>Storable</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#MEMORY-STORE">MEMORY STORE</a><li><a href="#ADVISORY-LOCKING">ADVISORY LOCKING</a><li><a href="#SPEED">SPEED</a><li><a href="#CANONICAL-REPRESENTATION">CANONICAL REPRESENTATION</a><li><a href="#CODE-REFERENCES">CODE REFERENCES</a><li><a href="#FORWARD-COMPATIBILITY">FORWARD COMPATIBILITY</a><li><a href="#ERROR-REPORTING">ERROR REPORTING</a><li><a href="#WIZARDS-ONLY">WIZARDS ONLY</a><ul><li><a href="#Hooks">Hooks</a><li><a href="#Predicates">Predicates</a><li><a href="#Recursion">Recursion</a><li><a href="#Deep-Cloning">Deep Cloning</a></ul><li><a href="#Storable-magic">Storable magic</a><li><a href="#EXAMPLES">EXAMPLES</a><li><a href="#SECURITY-WARNING">SECURITY WARNING</a><li><a href="#WARNING">WARNING</a><li><a href="#BUGS">BUGS</a><ul><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></ul><li><a href="#CREDITS">CREDITS</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>Storable - persistence for Perl data structures</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li> use Storable;</li><li> store \%table, 'file';</li><li> $hashref = retrieve('file');</li><li></li><li> use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);</li><li></li><li> # Network order</li><li> nstore \%table, 'file';</li><li> $hashref = retrieve('file');	# There is NO nretrieve()</li><li></li><li> # Storing to and retrieving from an already opened file</li><li> store_fd \@array, \*STDOUT;</li><li> nstore_fd \%table, \*STDOUT;</li><li> $aryref = fd_retrieve(\*SOCKET);</li><li> $hashref = fd_retrieve(\*SOCKET);</li><li></li><li> # Serializing to memory</li><li> $serialized = freeze \%table;</li><li> %table_clone = %{ thaw($serialized) };</li><li></li><li> # Deep (recursive) cloning</li><li> $cloneref = dclone($ref);</li><li></li><li> # Advisory locking</li><li> use Storable qw(lock_store lock_nstore lock_retrieve)</li><li> lock_store \%table, 'file';</li><li> lock_nstore \%table, 'file';</li><li> $hashref = lock_retrieve('file');</li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>The Storable package brings persistence to your Perl data structures
containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be
conveniently stored to disk and retrieved at a later time.</p>
<p>It can be used in the regular procedural way by calling <code class="inline"><span class="w">store</span></code>
 with
a reference to the object to be stored, along with the file name where
the image should be written.</p>
<p>The routine returns <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> for I/O problems or other internal error,
a true value otherwise. Serious errors are propagated as a <code class="inline"><a class="l_k" href="functions/die.html">die</a></code> exception.</p>
<p>To retrieve data stored to disk, use <code class="inline"><span class="w">retrieve</span></code>
 with a file name.
The objects stored into that file are recreated into memory for you,
and a <i>reference</i> to the root object is returned. In case an I/O error
occurs while reading, <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is returned instead. Other serious
errors are propagated via <code class="inline"><a class="l_k" href="functions/die.html">die</a></code>.</p>
<p>Since storage is performed recursively, you might want to stuff references
to objects that share a lot of common data into a single array or hash
table, and then store that object. That way, when you retrieve back the
whole thing, the objects will continue to share what they originally shared.</p>
<p>At the cost of a slight header overhead, you may store to an already
opened file descriptor using the <code class="inline"><span class="w">store_fd</span></code>
 routine, and retrieve
from a file via <code class="inline"><span class="w">fd_retrieve</span></code>
. Those names aren't imported by default,
so you will have to do that explicitly if you need those routines.
The file descriptor you supply must be already opened, for read
if you're going to retrieve and for write if you wish to store.</p>
<pre class="verbatim"><ol><li>	<span class="i">store_fd</span><span class="s">(</span>\<span class="i">%table</span><span class="cm">,</span> <span class="i">*STDOUT</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;can&#39;t store to stdout\n&quot;</span><span class="sc">;</span></li><li>	<span class="i">$hashref</span> = <span class="i">fd_retrieve</span><span class="s">(</span><span class="i">*STDIN</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can also store data in network order to allow easy sharing across
multiple platforms, or when storing on a socket known to be remotely
connected. The routines to call have an initial <code class="inline"><span class="w">n</span></code>
 prefix for <i>network</i>,
as in <code class="inline"><span class="w">nstore</span></code>
 and <code class="inline"><span class="w">nstore_fd</span></code>
. At retrieval time, your data will be
correctly restored so you don't have to know whether you're restoring
from native or network ordered data.  Double values are stored stringified
to ensure portability as well, at the slight risk of loosing some precision
in the last decimals.</p>
<p>When using <code class="inline"><span class="w">fd_retrieve</span></code>
, objects are retrieved in sequence, one
object (i.e. one recursive tree) per associated <code class="inline"><span class="w">store_fd</span></code>
.</p>
<p>If you're more from the object-oriented camp, you can inherit from
Storable and directly store your objects by invoking <code class="inline"><span class="w">store</span></code>
 as
a method. The fact that the root of the to-be-stored tree is a
blessed reference (i.e. an object) is special-cased so that the
retrieve does not provide a reference to that object but rather the
blessed object reference itself. (Otherwise, you'd get a reference
to that blessed object).</p>
<a name="MEMORY-STORE"></a><h1>MEMORY STORE</h1>
<p>The Storable engine can also store data into a Perl scalar instead, to
later retrieve them. This is mainly used to freeze a complex structure in
some safe compact memory place (where it can possibly be sent to another
process via some IPC, since freezing the structure also serializes it in
effect). Later on, and maybe somewhere else, you can thaw the Perl scalar
out and recreate the original complex structure in memory.</p>
<p>Surprisingly, the routines to be called are named <code class="inline"><span class="w">freeze</span></code>
 and <code class="inline"><span class="w">thaw</span></code>
.
If you wish to send out the frozen scalar to another machine, use
<code class="inline"><span class="w">nfreeze</span></code>
 instead to get a portable image.</p>
<p>Note that freezing an object structure and immediately thawing it
actually achieves a deep cloning of that structure:</p>
<pre class="verbatim"><ol><li>    dclone(.) = thaw(freeze(.))</li></ol></pre><p>Storable provides you with a <code class="inline"><span class="w">dclone</span></code>
 interface which does not create
that intermediary scalar but instead freezes the structure in some
internal memory space and then immediately thaws it out.</p>
<a name="ADVISORY-LOCKING"></a><h1>ADVISORY LOCKING</h1>
<p>The <code class="inline"><span class="w">lock_store</span></code>
 and <code class="inline"><span class="w">lock_nstore</span></code>
 routine are equivalent to
<code class="inline"><span class="w">store</span></code>
 and <code class="inline"><span class="w">nstore</span></code>
, except that they get an exclusive lock on
the file before writing.  Likewise, <code class="inline"><span class="w">lock_retrieve</span></code>
 does the same
as <code class="inline"><span class="w">retrieve</span></code>
, but also gets a shared lock on the file before reading.</p>
<p>As with any advisory locking scheme, the protection only works if you
systematically use <code class="inline"><span class="w">lock_store</span></code>
 and <code class="inline"><span class="w">lock_retrieve</span></code>
.  If one side of
your application uses <code class="inline"><span class="w">store</span></code>
 whilst the other uses <code class="inline"><span class="w">lock_retrieve</span></code>
,
you will get no protection at all.</p>
<p>The internal advisory locking is implemented using Perl's flock()
routine.  If your system does not support any form of flock(), or if
you share your files across NFS, you might wish to use other forms
of locking by using modules such as LockFile::Simple which lock a
file using a filesystem entry, instead of locking the file descriptor.</p>
<a name="SPEED"></a><h1>SPEED</h1>
<p>The heart of Storable is written in C for decent speed. Extra low-level
optimizations have been made when manipulating perl internals, to
sacrifice encapsulation for the benefit of greater speed.</p>
<a name="CANONICAL-REPRESENTATION"></a><h1>CANONICAL REPRESENTATION</h1>
<p>Normally, Storable stores elements of hashes in the order they are
stored internally by Perl, i.e. pseudo-randomly.  If you set
<code class="inline"><span class="i">$Storable::canonical</span></code>
 to some <code class="inline"><span class="w">TRUE</span></code>
 value, Storable will store
hashes with the elements sorted by their key.  This allows you to
compare data structures by comparing their frozen representations (or
even the compressed frozen representations), which can be useful for
creating lookup tables for complicated queries.</p>
<p>Canonical order does not imply network order; those are two orthogonal
settings.</p>
<a name="CODE-REFERENCES"></a><h1>CODE REFERENCES</h1>
<p>Since Storable version 2.05, CODE references may be serialized with
the help of <a href="B/Deparse.html">B::Deparse</a>. To enable this feature, set
<code class="inline"><span class="i">$Storable::Deparse</span></code>
 to a true value. To enable deserialization,
<code class="inline"><span class="i">$Storable::Eval</span></code>
 should be set to a true value. Be aware that
deserialization is done through <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>, which is dangerous if the
Storable file contains malicious data. You can set <code class="inline"><span class="i">$Storable::Eval</span></code>

to a subroutine reference which would be used instead of <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>. See
below for an example using a <a href="Safe.html">Safe</a> compartment for deserialization
of CODE references.</p>
<p>If <code class="inline"><span class="i">$Storable::Deparse</span></code>
 and/or <code class="inline"><span class="i">$Storable::Eval</span></code>
 are set to false
values, then the value of <code class="inline"><span class="i">$Storable::forgive_me</span></code>
 (see below) is
respected while serializing and deserializing.</p>
<a name="FORWARD-COMPATIBILITY"></a><h1>FORWARD COMPATIBILITY</h1>
<p>This release of Storable can be used on a newer version of Perl to
serialize data which is not supported by earlier Perls.  By default,
Storable will attempt to do the right thing, by <code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
ing if it
encounters data that it cannot deserialize.  However, the defaults
can be changed as follows:</p>
<ul>
<li><a name="utf8-data"></a><b>utf8 data</b>
<p>Perl 5.6 added support for Unicode characters with code points &gt; 255,
and Perl 5.8 has full support for Unicode characters in hash keys.
Perl internally encodes strings with these characters using utf8, and
Storable serializes them as utf8.  By default, if an older version of
Perl encounters a utf8 value it cannot represent, it will <code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
.
To change this behaviour so that Storable deserializes utf8 encoded
values as the string of bytes (effectively dropping the <i>is_utf8</i> flag)
set <code class="inline"><span class="i">$Storable::drop_utf8</span></code>
 to some <code class="inline"><span class="w">TRUE</span></code>
 value.  This is a form of
data loss, because with <code class="inline"><span class="i">$drop_utf8</span></code>
 true, it becomes impossible to tell
whether the original data was the Unicode string, or a series of bytes
that happen to be valid utf8.</p>
</li>
<li><a name="restricted-hashes"></a><b>restricted hashes</b>
<p>Perl 5.8 adds support for restricted hashes, which have keys
restricted to a given set, and can have values locked to be read only.
By default, when Storable encounters a restricted hash on a perl
that doesn't support them, it will deserialize it as a normal hash,
silently discarding any placeholder keys and leaving the keys and
all values unlocked.  To make Storable <code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
 instead, set
<code class="inline"><span class="i">$Storable::downgrade_restricted</span></code>
 to a <code class="inline"><span class="w">FALSE</span></code>
 value.  To restore
the default set it back to some <code class="inline"><span class="w">TRUE</span></code>
 value.</p>
</li>
<li><a name="files-from-future-versions-of-Storable"></a><b>files from future versions of Storable</b>
<p>Earlier versions of Storable would immediately croak if they encountered
a file with a higher internal version number than the reading Storable
knew about.  Internal version numbers are increased each time new data
types (such as restricted hashes) are added to the vocabulary of the file
format.  This meant that a newer Storable module had no way of writing a
file readable by an older Storable, even if the writer didn't store newer
data types.</p>
<p>This version of Storable will defer croaking until it encounters a data
type in the file that it does not recognize.  This means that it will
continue to read files generated by newer Storable modules which are careful
in what they write out, making it easier to upgrade Storable modules in a
mixed environment.</p>
<p>The old behaviour of immediate croaking can be re-instated by setting
<code class="inline"><span class="i">$Storable::accept_future_minor</span></code>
 to some <code class="inline"><span class="w">FALSE</span></code>
 value.</p>
</li>
</ul>
<p>All these variables have no effect on a newer Perl which supports the
relevant feature.</p>
<a name="ERROR-REPORTING"></a><h1>ERROR REPORTING</h1>
<p>Storable uses the "exception" paradigm, in that it does not try to workaround
failures: if something bad happens, an exception is generated from the
caller's perspective (see <a href="Carp.html">Carp</a> and <code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
).  Use eval {} to trap
those exceptions.</p>
<p>When Storable croaks, it tries to report the error via the <code class="inline"><span class="i">logcroak</span><span class="s">(</span><span class="s">)</span></code>

routine from the <code class="inline"><span class="w">Log::Agent</span></code>
 package, if it is available.</p>
<p>Normal errors are reported by having store() or retrieve() return <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>.
Such errors are usually I/O errors (or truncated stream errors at retrieval).</p>
<a name="WIZARDS-ONLY"></a><h1>WIZARDS ONLY</h1>
<a name="Hooks"></a><h2>Hooks</h2>
<p>Any class may define hooks that will be called during the serialization
and deserialization process on objects that are instances of that class.
Those hooks can redefine the way serialization is performed (and therefore,
how the symmetrical deserialization should be conducted).</p>
<p>Since we said earlier:</p>
<pre class="verbatim"><ol><li>    dclone(.) = thaw(freeze(.))</li></ol></pre><p>everything we say about hooks should also hold for deep cloning. However,
hooks get to know whether the operation is a mere serialization, or a cloning.</p>
<p>Therefore, when serializing hooks are involved,</p>
<pre class="verbatim"><ol><li>    dclone(.) &lt;&gt; thaw(freeze(.))</li></ol></pre><p>Well, you could keep them in sync, but there's no guarantee it will always
hold on classes somebody else wrote.  Besides, there is little to gain in
doing so: a serializing hook could keep only one attribute of an object,
which is probably not what should happen during a deep cloning of that
same object.</p>
<p>Here is the hooking interface:</p>
<ul>
<li><a name="STORABLE_freeze-_obj_%2c-_cloning_"></a><b><code class="inline"><span class="w">STORABLE_freeze</span></code>
 <i>obj</i>, <i>cloning</i></b>
<p>The serializing hook, called on the object during serialization.  It can be
inherited, or defined in the class itself, like any other method.</p>
<p>Arguments: <i>obj</i> is the object to serialize, <i>cloning</i> is a flag indicating
whether we're in a dclone() or a regular serialization via store() or freeze().</p>
<p>Returned value: A LIST <code class="inline"><span class="s">(</span><span class="i">$serialized</span><span class="cm">,</span> <span class="i">$ref1</span><span class="cm">,</span> <span class="i">$ref2</span><span class="cm">,</span> ...<span class="s">)</span></code>
 where $serialized
is the serialized form to be used, and the optional $ref1, $ref2, etc... are
extra references that you wish to let the Storable engine serialize.</p>
<p>At deserialization time, you will be given back the same LIST, but all the
extra references will be pointing into the deserialized structure.</p>
<p>The <b>first time</b> the hook is hit in a serialization flow, you may have it
return an empty list.  That will signal the Storable engine to further
discard that hook for this class and to therefore revert to the default
serialization of the underlying Perl data.  The hook will again be normally
processed in the next serialization.</p>
<p>Unless you know better, serializing hook should always say:</p>
<pre class="verbatim"><ol><li>    sub STORABLE_freeze {</li><li>        my ($self, $cloning) = @_;</li><li>        return if $cloning;         # Regular default serialization</li><li>        ....</li><li>    }</li></ol></pre><p>in order to keep reasonable dclone() semantics.</p>
</li>
<li><a name="STORABLE_thaw-_obj_%2c-_cloning_%2c-_serialized_%2c-..."></a><b><code class="inline"><span class="w">STORABLE_thaw</span></code>
 <i>obj</i>, <i>cloning</i>, <i>serialized</i>, ...</b>
<p>The deserializing hook called on the object during deserialization.
But wait: if we're deserializing, there's no object yet... right?</p>
<p>Wrong: the Storable engine creates an empty one for you.  If you know Eiffel,
you can view <code class="inline"><span class="w">STORABLE_thaw</span></code>
 as an alternate creation routine.</p>
<p>This means the hook can be inherited like any other method, and that
<i>obj</i> is your blessed reference for this particular instance.</p>
<p>The other arguments should look familiar if you know <code class="inline"><span class="w">STORABLE_freeze</span></code>
:
<i>cloning</i> is true when we're part of a deep clone operation, <i>serialized</i>
is the serialized string you returned to the engine in <code class="inline"><span class="w">STORABLE_freeze</span></code>
,
and there may be an optional list of references, in the same order you gave
them at serialization time, pointing to the deserialized objects (which
have been processed courtesy of the Storable engine).</p>
<p>When the Storable engine does not find any <code class="inline"><span class="w">STORABLE_thaw</span></code>
 hook routine,
it tries to load the class by requiring the package dynamically (using
the blessed package name), and then re-attempts the lookup.  If at that
time the hook cannot be located, the engine croaks.  Note that this mechanism
will fail if you define several classes in the same file, but <a href="perlmod.html">perlmod</a>
warned you.</p>
<p>It is up to you to use this information to populate <i>obj</i> the way you want.</p>
<p>Returned value: none.</p>
</li>
<li><a name="STORABLE_attach-_class_%2c-_cloning_%2c-_serialized_"></a><b><code class="inline"><span class="w">STORABLE_attach</span></code>
 <i>class</i>, <i>cloning</i>, <i>serialized</i></b>
<p>While <code class="inline"><span class="w">STORABLE_freeze</span></code>
 and <code class="inline"><span class="w">STORABLE_thaw</span></code>
 are useful for classes where
each instance is independent, this mechanism has difficulty (or is
incompatible) with objects that exist as common process-level or
system-level resources, such as singleton objects, database pools, caches
or memoized objects.</p>
<p>The alternative <code class="inline"><span class="w">STORABLE_attach</span></code>
 method provides a solution for these
shared objects. Instead of <code class="inline"><span class="w">STORABLE_freeze</span></code>
 --&gt; <code class="inline"><span class="w">STORABLE_thaw</span></code>
,
you implement <code class="inline"><span class="w">STORABLE_freeze</span></code>
 --&gt; <code class="inline"><span class="w">STORABLE_attach</span></code>
 instead.</p>
<p>Arguments: <i>class</i> is the class we are attaching to, <i>cloning</i> is a flag
indicating whether we're in a dclone() or a regular de-serialization via
thaw(), and <i>serialized</i> is the stored string for the resource object.</p>
<p>Because these resource objects are considered to be owned by the entire
process/system, and not the "property" of whatever is being serialized,
no references underneath the object should be included in the serialized
string. Thus, in any class that implements <code class="inline"><span class="w">STORABLE_attach</span></code>
, the
<code class="inline"><span class="w">STORABLE_freeze</span></code>
 method cannot return any references, and <code class="inline"><span class="w">Storable</span></code>

will throw an error if <code class="inline"><span class="w">STORABLE_freeze</span></code>
 tries to return references.</p>
<p>All information required to "attach" back to the shared resource object
<b>must</b> be contained <b>only</b> in the <code class="inline"><span class="w">STORABLE_freeze</span></code>
 return string.
Otherwise, <code class="inline"><span class="w">STORABLE_freeze</span></code>
 behaves as normal for <code class="inline"><span class="w">STORABLE_attach</span></code>

classes.</p>
<p>Because <code class="inline"><span class="w">STORABLE_attach</span></code>
 is passed the class (rather than an object),
it also returns the object directly, rather than modifying the passed
object.</p>
<p>Returned value: object of type <code class="inline"><span class="w">class</span></code>
</p>
</li>
</ul>
<a name="Predicates"></a><h2>Predicates</h2>
<p>Predicates are not exportable.  They must be called by explicitly prefixing
them with the Storable package name.</p>
<ul>
<li><a name="Storable%3a%3alast_op_in_netorder"></a><b><code class="inline"><span class="w">Storable::last_op_in_netorder</span></code>
</b>
<p>The <code class="inline"><span class="i">Storable::last_op_in_netorder</span><span class="s">(</span><span class="s">)</span></code>
 predicate will tell you whether
network order was used in the last store or retrieve operation.  If you
don't know how to use this, just forget about it.</p>
</li>
<li><a name="Storable%3a%3ais_storing"></a><b><code class="inline"><span class="w">Storable::is_storing</span></code>
</b>
<p>Returns true if within a store operation (via STORABLE_freeze hook).</p>
</li>
<li><a name="Storable%3a%3ais_retrieving"></a><b><code class="inline"><span class="w">Storable::is_retrieving</span></code>
</b>
<p>Returns true if within a retrieve operation (via STORABLE_thaw hook).</p>
</li>
</ul>
<a name="Recursion"></a><h2>Recursion</h2>
<p>With hooks comes the ability to recurse back to the Storable engine.
Indeed, hooks are regular Perl code, and Storable is convenient when
it comes to serializing and deserializing things, so why not use it
to handle the serialization string?</p>
<p>There are a few things you need to know, however:</p>
<ul>
<li>
<p>You can create endless loops if the things you serialize via freeze()
(for instance) point back to the object we're trying to serialize in
the hook.</p>
</li>
<li>
<p>Shared references among objects will not stay shared: if we're serializing
the list of object [A, C] where both object A and C refer to the SAME object
B, and if there is a serializing hook in A that says freeze(B), then when
deserializing, we'll get [A', C'] where A' refers to B', but C' refers to D,
a deep clone of B'.  The topology was not preserved.</p>
</li>
</ul>
<p>That's why <code class="inline"><span class="w">STORABLE_freeze</span></code>
 lets you provide a list of references
to serialize.  The engine guarantees that those will be serialized in the
same context as the other objects, and therefore that shared objects will
stay shared.</p>
<p>In the above [A, C] example, the <code class="inline"><span class="w">STORABLE_freeze</span></code>
 hook could return:</p>
<pre class="verbatim"><ol><li>	<span class="s">(</span><span class="q">&quot;something&quot;</span><span class="cm">,</span> <span class="i">$self</span>-&gt;{<span class="w">B</span>}<span class="s">)</span></li></ol></pre><p>and the B part would be serialized by the engine.  In <code class="inline"><span class="w">STORABLE_thaw</span></code>
, you
would get back the reference to the B' object, deserialized for you.</p>
<p>Therefore, recursion should normally be avoided, but is nonetheless supported.</p>
<a name="Deep-Cloning"></a><h2>Deep Cloning</h2>
<p>There is a Clone module available on CPAN which implements deep cloning
natively, i.e. without freezing to memory and thawing the result.  It is
aimed to replace Storable's dclone() some day.  However, it does not currently
support Storable hooks to redefine the way deep cloning is performed.</p>
<a name="Storable-magic"></a><h1>Storable magic</h1>
<p>Yes, there's a lot of that :-) But more precisely, in UNIX systems
there's a utility called <code class="inline"><span class="w">file</span></code>
, which recognizes data files based on
their contents (usually their first few bytes).  For this to work,
a certain file called <i>magic</i> needs to taught about the <i>signature</i>
of the data.  Where that configuration file lives depends on the UNIX
flavour; often it's something like <i>/usr/share/misc/magic</i> or
<i>/etc/magic</i>.  Your system administrator needs to do the updating of
the <i>magic</i> file.  The necessary signature information is output to
STDOUT by invoking Storable::show_file_magic().  Note that the GNU
implementation of the <code class="inline"><span class="w">file</span></code>
 utility, version 3.38 or later,
is expected to contain support for recognising Storable files
out-of-the-box, in addition to other kinds of Perl files.</p>
<p>You can also use the following functions to extract the file header
information from Storable images:</p>
<ul>
<li><a name="%24info-%3d-Storable%3a%3afile_magic(-%24filename-)"></a><b>$info = Storable::file_magic( $filename )</b>
<p>If the given file is a Storable image return a hash describing it.  If
the file is readable, but not a Storable image return <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>.  If
the file does not exist or is unreadable then croak.</p>
<p>The hash returned has the following elements:</p>
<ul>
<li><a name="version"></a><b><code class="inline"><span class="w">version</span></code>
</b>
<p>This returns the file format version.  It is a string like "2.7".</p>
<p>Note that this version number is not the same as the version number of
the Storable module itself.  For instance Storable v0.7 create files
in format v2.0 and Storable v2.15 create files in format v2.7.  The
file format version number only increment when additional features
that would confuse older versions of the module are added.</p>
<p>Files older than v2.0 will have the one of the version numbers "-1",
"0" or "1".  No minor number was used at that time.</p>
</li>
<li><a name="version_nv"></a><b><code class="inline"><span class="w">version_nv</span></code>
</b>
<p>This returns the file format version as number.  It is a string like
"2.007".  This value is suitable for numeric comparisons.</p>
<p>The constant function <code class="inline"><span class="w">Storable::BIN_VERSION_NV</span></code>
 returns a comparable
number that represents the highest file version number that this
version of Storable fully supports (but see discussion of
<code class="inline"><span class="i">$Storable::accept_future_minor</span></code>
 above).  The constant
<code class="inline"><span class="w">Storable::BIN_WRITE_VERSION_NV</span></code>
 function returns what file version
is written and might be less than <code class="inline"><span class="w">Storable::BIN_VERSION_NV</span></code>
 in some
configurations.</p>
</li>
<li><a name="major%2c-minor"></a><b><code class="inline"><span class="w">major</span></code>
, <code class="inline"><span class="w">minor</span></code>
</b>
<p>This also returns the file format version.  If the version is "2.7"
then major would be 2 and minor would be 7.  The minor element is
missing for when major is less than 2.</p>
</li>
<li><a name="hdrsize"></a><b><code class="inline"><span class="w">hdrsize</span></code>
</b>
<p>The is the number of bytes that the Storable header occupies.</p>
</li>
<li><a name="netorder"></a><b><code class="inline"><span class="w">netorder</span></code>
</b>
<p>This is TRUE if the image store data in network order.  This means
that it was created with nstore() or similar.</p>
</li>
<li><a name="byteorder"></a><b><code class="inline"><span class="w">byteorder</span></code>
</b>
<p>This is only present when <code class="inline"><span class="w">netorder</span></code>
 is FALSE.  It is the
$Config{byteorder} string of the perl that created this image.  It is
a string like "1234" (32 bit little endian) or "87654321" (64 bit big
endian).  This must match the current perl for the image to be
readable by Storable.</p>
</li>
<li><a name="intsize%2c-longsize%2c-ptrsize%2c-nvsize"></a><b><code class="inline"><span class="w">intsize</span></code>
, <code class="inline"><span class="w">longsize</span></code>
, <code class="inline"><span class="w">ptrsize</span></code>
, <code class="inline"><span class="w">nvsize</span></code>
</b>
<p>These are only present when <code class="inline"><span class="w">netorder</span></code>
 is FALSE. These are the sizes of
various C datatypes of the perl that created this image.  These must
match the current perl for the image to be readable by Storable.</p>
<p>The <code class="inline"><span class="w">nvsize</span></code>
 element is only present for file format v2.2 and
higher.</p>
</li>
<li><a name="file"></a><b><code class="inline"><span class="w">file</span></code>
</b>
<p>The name of the file.</p>
</li>
</ul>
</li>
<li><a name="%24info-%3d-Storable%3a%3aread_magic(-%24buffer-)"></a><b>$info = Storable::read_magic( $buffer )</b>
</li>
<li><a name="%24info-%3d-Storable%3a%3aread_magic(-%24buffer%2c-%24must_be_file-)"></a><b>$info = Storable::read_magic( $buffer, $must_be_file )</b>
<p>The $buffer should be a Storable image or the first few bytes of it.
If $buffer starts with a Storable header, then a hash describing the
image is returned, otherwise <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is returned.</p>
<p>The hash has the same structure as the one returned by
Storable::file_magic().  The <code class="inline"><span class="w">file</span></code>
 element is true if the image is a
file image.</p>
<p>If the $must_be_file argument is provided and is TRUE, then return
<code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> unless the image looks like it belongs to a file dump.</p>
<p>The maximum size of a Storable header is currently 21 bytes.  If the
provided $buffer is only the first part of a Storable image it should
at least be this long to ensure that read_magic() will recognize it as
such.</p>
</li>
</ul>
<a name="EXAMPLES"></a><h1>EXAMPLES</h1>
<p>Here are some code samples showing a possible usage of Storable:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Storable</span> <span class="q">qw(store retrieve freeze thaw dclone)</span><span class="sc">;</span></li><li></li><li> <span class="i">%color</span> = <span class="s">(</span><span class="q">&#39;Blue&#39;</span> <span class="cm">=&gt;</span> <span class="n">0.1</span><span class="cm">,</span> <span class="q">&#39;Red&#39;</span> <span class="cm">=&gt;</span> <span class="n">0.8</span><span class="cm">,</span> <span class="q">&#39;Black&#39;</span> <span class="cm">=&gt;</span> <span class="n">0</span><span class="cm">,</span> <span class="q">&#39;White&#39;</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="i">store</span><span class="s">(</span>\<span class="i">%color</span><span class="cm">,</span> <span class="q">&#39;mycolors&#39;</span><span class="s">)</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t store %a in mycolors!\n&quot;</span><span class="sc">;</span></li><li></li><li> <span class="i">$colref</span> = <span class="i">retrieve</span><span class="s">(</span><span class="q">&#39;mycolors&#39;</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Unable to retrieve from mycolors!\n&quot;</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$colref</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;Blue is still %lf\n&quot;</span><span class="cm">,</span> <span class="i">$colref</span>-&gt;{<span class="q">&#39;Blue&#39;</span>}<span class="sc">;</span></li><li></li><li> <span class="i">$colref2</span> = <span class="i">dclone</span><span class="s">(</span>\<span class="i">%color</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="i">$str</span> = <span class="i">freeze</span><span class="s">(</span>\<span class="i">%color</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;Serialization of %%color is %d bytes long.\n&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/length.html">length</a><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$colref3</span> = <span class="i">thaw</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>which prints (on my machine):</p>
<pre class="verbatim"><ol><li> <span class="w">Blue</span> <span class="w">is</span> <span class="w">still</span> <span class="n">0.100000</span></li><li> <span class="w">Serialization</span> <span class="w">of</span> <span class="i">%color</span> <span class="w">is</span> <span class="n">102</span> <span class="w">bytes</span> <span class="w">long</span>.</li></ol></pre><p>Serialization of CODE references and deserialization in a safe
compartment:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Storable</span> <span class="q">qw(freeze thaw)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Safe</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$safe</span> = <span class="w">new</span> <span class="w">Safe</span><span class="sc">;</span></li><li>        <span class="c"># because of opcodes used in &quot;use strict&quot;:</span></li><li> <span class="i">$safe</span><span class="i">-&gt;permit</span><span class="s">(</span><span class="q">qw(:default require)</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/local.html">local</a> <span class="i">$Storable::Deparse</span> = <span class="n">1</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/local.html">local</a> <span class="i">$Storable::Eval</span> = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$safe</span><span class="i">-&gt;reval</span><span class="s">(</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span> <span class="s">}</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$serialized</span> = <span class="i">freeze</span><span class="s">(</span><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="n">42</span> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$code</span> = <span class="i">thaw</span><span class="s">(</span><span class="i">$serialized</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$code</span>-&gt;<span class="s">(</span><span class="s">)</span> == <span class="n">42</span><span class="sc">;</span></li></ol></pre><a name="SECURITY-WARNING"></a><h1>SECURITY WARNING</h1>
<p><b>Do not accept Storable documents from untrusted sources!</b></p>
<p>Some features of Storable can lead to security vulnerabilities if you
accept Storable documents from untrusted sources. Most obviously, the
optional (off by default) CODE reference serialization feature allows
transfer of code to the deserializing process. Furthermore, any
serialized object will cause Storable to helpfully load the module
corresponding to the class of the object in the deserializing module.
For manipulated module names, this can load almost arbitrary code.
Finally, the deserialized object's destructors will be invoked when
the objects get destroyed in the deserializing process. Maliciously
crafted Storable documents may put such objects in the value of
a hash key that is overridden by another key/value pair in the
same hash, thus causing immediate destructor execution.</p>
<p>In a future version of Storable, we intend to provide options to disable
loading modules for classes and to disable deserializing objects
altogether. <i>Nonetheless, Storable deserializing documents from
untrusted sources is expected to have other, yet undiscovered,
security concerns such as allowing an attacker to cause the deserializer
to crash hard.</i></p>
<p><b>Therefore, let me repeat: Do not accept Storable documents from
untrusted sources!</b></p>
<p>If your application requires accepting data from untrusted sources, you
are best off with a less powerful and more-likely safe serialization format
and implementation. If your data is sufficiently simple, JSON is a good
choice and offers maximum interoperability.</p>
<a name="WARNING"></a><h1>WARNING</h1>
<p>If you're using references as keys within your hash tables, you're bound
to be disappointed when retrieving your data. Indeed, Perl stringifies
references used as hash table keys. If you later wish to access the
items via another reference stringification (i.e. using the same
reference that was used for the key originally to record the value into
the hash table), it will work because both references stringify to the
same string.</p>
<p>It won't work across a sequence of <code class="inline"><span class="w">store</span></code>
 and <code class="inline"><span class="w">retrieve</span></code>
 operations,
however, because the addresses in the retrieved objects, which are
part of the stringified references, will probably differ from the
original addresses. The topology of your structure is preserved,
but not hidden semantics like those.</p>
<p>On platforms where it matters, be sure to call <code class="inline"><a class="l_k" href="functions/binmode.html">binmode()</a></code> on the
descriptors that you pass to Storable functions.</p>
<p>Storing data canonically that contains large hashes can be
significantly slower than storing the same data normally, as
temporary arrays to hold the keys for each hash have to be allocated,
populated, sorted and freed.  Some tests have shown a halving of the
speed of storing -- the exact penalty will depend on the complexity of
your data.  There is no slowdown on retrieval.</p>
<a name="BUGS"></a><h1>BUGS</h1>
<p>You can't store GLOB, FORMLINE, REGEXP, etc.... If you can define semantics
for those operations, feel free to enhance Storable so that it can
deal with them.</p>
<p>The store functions will <code class="inline"><span class="w">croak</span></code>
 if they run into such references
unless you set <code class="inline"><span class="i">$Storable::forgive_me</span></code>
 to some <code class="inline"><span class="w">TRUE</span></code>
 value. In that
case, the fatal message is converted to a warning and some meaningless
string is stored instead.</p>
<p>Setting <code class="inline"><span class="i">$Storable::canonical</span></code>
 may not yield frozen strings that
compare equal due to possible stringification of numbers. When the
string version of a scalar exists, it is the form stored; therefore,
if you happen to use your numbers as strings between two freezing
operations on the same data structures, you will get different
results.</p>
<p>When storing doubles in network order, their value is stored as text.
However, you should also not expect non-numeric floating-point values
such as infinity and "not a number" to pass successfully through a
nstore()/retrieve() pair.</p>
<p>As Storable neither knows nor cares about character sets (although it
does know that characters may be more than eight bits wide), any difference
in the interpretation of character codes between a host and a target
system is your problem.  In particular, if host and target use different
code points to represent the characters used in the text representation
of floating-point numbers, you will not be able be able to exchange
floating-point data, even with nstore().</p>
<p><code class="inline"><span class="w">Storable::drop_utf8</span></code>
 is a blunt tool.  There is no facility either to
return <b>all</b> strings as utf8 sequences, or to attempt to convert utf8
data back to 8 bit and <code class="inline"><span class="i">croak</span><span class="s">(</span><span class="s">)</span></code>
 if the conversion fails.</p>
<p>Prior to Storable 2.01, no distinction was made between signed and
unsigned integers on storing.  By default Storable prefers to store a
scalars string representation (if it has one) so this would only cause
problems when storing large unsigned integers that had never been converted
to string or floating point.  In other words values that had been generated
by integer operations such as logic ops and then not used in any string or
arithmetic context before storing.</p>
<a name="64-bit-data-in-perl-5.6.0-and-5.6.1"></a><h2>64 bit data in perl 5.6.0 and 5.6.1</h2>
<p>This section only applies to you if you have existing data written out
by Storable 2.02 or earlier on perl 5.6.0 or 5.6.1 on Unix or Linux which
has been configured with 64 bit integer support (not the default)
If you got a precompiled perl, rather than running Configure to build
your own perl from source, then it almost certainly does not affect you,
and you can stop reading now (unless you're curious). If you're using perl
on Windows it does not affect you.</p>
<p>Storable writes a file header which contains the sizes of various C
language types for the C compiler that built Storable (when not writing in
network order), and will refuse to load files written by a Storable not
on the same (or compatible) architecture.  This check and a check on
machine byteorder is needed because the size of various fields in the file
are given by the sizes of the C language types, and so files written on
different architectures are incompatible.  This is done for increased speed.
(When writing in network order, all fields are written out as standard
lengths, which allows full interworking, but takes longer to read and write)</p>
<p>Perl 5.6.x introduced the ability to optional configure the perl interpreter
to use C's <code class="inline"><span class="w">long</span> <span class="w">long</span></code>
 type to allow scalars to store 64 bit integers on 32
bit systems.  However, due to the way the Perl configuration system
generated the C configuration files on non-Windows platforms, and the way
Storable generates its header, nothing in the Storable file header reflected
whether the perl writing was using 32 or 64 bit integers, despite the fact
that Storable was storing some data differently in the file.  Hence Storable
running on perl with 64 bit integers will read the header from a file
written by a 32 bit perl, not realise that the data is actually in a subtly
incompatible format, and then go horribly wrong (possibly crashing) if it
encountered a stored integer.  This is a design failure.</p>
<p>Storable has now been changed to write out and read in a file header with
information about the size of integers.  It's impossible to detect whether
an old file being read in was written with 32 or 64 bit integers (they have
the same header) so it's impossible to automatically switch to a correct
backwards compatibility mode.  Hence this Storable defaults to the new,
correct behaviour.</p>
<p>What this means is that if you have data written by Storable 1.x running
on perl 5.6.0 or 5.6.1 configured with 64 bit integers on Unix or Linux
then by default this Storable will refuse to read it, giving the error
<i>Byte order is not compatible</i>.  If you have such data then you
should set <code class="inline"><span class="i">$Storable::interwork_56_64bit</span></code>
 to a true value to make this
Storable read and write files with the old header.  You should also
migrate your data, or any older perl you are communicating with, to this
current version of Storable.</p>
<p>If you don't have data written with specific configuration of perl described
above, then you do not and should not do anything.  Don't set the flag -
not only will Storable on an identically configured perl refuse to load them,
but Storable a differently configured perl will load them believing them
to be correct for it, and then may well fail or crash part way through
reading them.</p>
<a name="CREDITS"></a><h1>CREDITS</h1>
<p>Thank you to (in chronological order):</p>
<pre class="verbatim"><ol><li>	<span class="w">Jarkko</span> <span class="w">Hietaniemi</span> <span class="q">&lt;jhi@iki.fi&gt;</span></li><li>	<span class="w">Ulrich</span> <span class="w">Pfeifer</span> <span class="q">&lt;pfeifer@charly.informatik.uni-dortmund.de&gt;</span></li><li>	<span class="w">Benjamin</span> <span class="w">A</span>. <span class="w">Holzman</span> <span class="q">&lt;bholzman@earthlink.net&gt;</span></li><li>	<span class="w">Andrew</span> <span class="w">Ford</span> <span class="q">&lt;A.Ford@ford-mason.co.uk&gt;</span></li><li>	<span class="w">Gisle</span> <span class="w">Aas</span> <span class="q">&lt;gisle@aas.no&gt;</span></li><li>	<span class="w">Jeff</span> <span class="w">Gresham</span> <span class="q">&lt;gresham_jeffrey@jpmorgan.com&gt;</span></li><li>	<span class="w">Murray</span> <span class="w">Nesbitt</span> <span class="q">&lt;murray@activestate.com&gt;</span></li><li>	<span class="w">Marc</span> <span class="w">Lehmann</span> <span class="q">&lt;pcg@opengroup.org&gt;</span></li><li>	<span class="w">Justin</span> <span class="w">Banks</span> <span class="q">&lt;justinb@wamnet.com&gt;</span></li><li>	<span class="w">Jarkko</span> <span class="w">Hietaniemi</span> <span class="q">&lt;jhi@iki.fi&gt;</span> <span class="s">(</span><span class="w">AGAIN</span><span class="cm">,</span> <span class="w">as</span> <span class="w">perl</span> <span class="v">5.7.0</span> <span class="w">Pumpkin</span>!<span class="s">)</span></li><li>	<span class="w">Salvador</span> <span class="w">Ortiz</span> <span class="w">Garcia</span> <span class="q">&lt;sog@msg.com.mx&gt;</span></li><li>	<span class="w">Dominic</span> <span class="w">Dunlop</span> <span class="q">&lt;domo@computer.org&gt;</span></li><li>	<span class="w">Erik</span> <span class="w">Haugan</span> <span class="q">&lt;erik@solbors.no&gt;</span></li><li>	<span class="w">Benjamin</span> <span class="w">A</span>. <span class="w">Holzman</span> <span class="q">&lt;ben.holzman@grantstreet.com&gt;</span></li><li>	<span class="w">Reini</span> <span class="w">Urban</span> <span class="q">&lt;rurban@cpanel.net&gt;</span></li></ol></pre><p>for their bug reports, suggestions and contributions.</p>
<p>Benjamin Holzman contributed the tied variable support, Andrew Ford
contributed the canonical order for hashes, and Gisle Aas fixed
a few misunderstandings of mine regarding the perl internals,
and optimized the emission of "tags" in the output streams by
simply counting the objects instead of tagging them (leading to
a binary incompatibility for the Storable image starting at version
0.6--older images are, of course, still properly understood).
Murray Nesbitt made Storable thread-safe.  Marc Lehmann added overloading
and references to tied items support.  Benjamin Holzman added a performance
improvement for overloaded classes; thanks to Grant Street Group for footing
the bill.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Storable was written by Raphael Manfredi
<i>&lt;Raphael_Manfredi@pobox.com&gt;</i>
Maintenance is now done by the perl5-porters
<i>&lt;perl5-porters@perl.org&gt;</i></p>
<p>Please e-mail us with problems, bug fixes, comments and complaints,
although if you have compliments you should send them to Raphael.
Please don't e-mail Raphael with problems, as he no longer works on
Storable, and your message will be delayed while he forwards it to us.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="http://search.cpan.org/perldoc/Clone">Clone</a>.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#MEMORY-STORE">MEMORY STORE</a><li><a href="#ADVISORY-LOCKING">ADVISORY LOCKING</a><li><a href="#SPEED">SPEED</a><li><a href="#CANONICAL-REPRESENTATION">CANONICAL REPRESENTATION</a><li><a href="#CODE-REFERENCES">CODE REFERENCES</a><li><a href="#FORWARD-COMPATIBILITY">FORWARD COMPATIBILITY</a><li><a href="#ERROR-REPORTING">ERROR REPORTING</a><li><a href="#WIZARDS-ONLY">WIZARDS ONLY</a><ul><li><a href="#Hooks">Hooks</a><li><a href="#Predicates">Predicates</a><li><a href="#Recursion">Recursion</a><li><a href="#Deep-Cloning">Deep Cloning</a></ul><li><a href="#Storable-magic">Storable magic</a><li><a href="#EXAMPLES">EXAMPLES</a><li><a href="#SECURITY-WARNING">SECURITY WARNING</a><li><a href="#WARNING">WARNING</a><li><a href="#BUGS">BUGS</a><ul><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></ul><li><a href="#CREDITS">CREDITS</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'Storable';
  perldoc.pageAddress = 'Storable.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>