1FreezeThaw(3)         User Contributed Perl Documentation        FreezeThaw(3)
2
3
4

NAME

6       FreezeThaw - converting Perl structures to strings and back.
7

SYNOPSIS

9         use FreezeThaw qw(freeze thaw cmpStr safeFreeze cmpStrHard);
10         $string = freeze $data1, $data2, $data3;
11         ...
12         ($olddata1, $olddata2, $olddata3) = thaw $string;
13         if (cmpStr($olddata2,$data2) == 0) {print "OK!"}
14

DESCRIPTION

16       Converts data to/from stringified form, appropriate for
17       saving-to/reading-from permanent storage.
18
19       Deals with objects, circular lists, repeated appearence of the same
20       refence. Does not deal with overloaded stringify operator yet.
21

EXPORT

23       Default     None.
24
25       Exportable  "freeze thaw cmpStr cmpStrHard safeFreeze".
26

User API

28       "cmpStr"    analogue of "cmp" for data. Takes two arguments and
29                   compares them as separate entities.
30
31       "cmpStrHard"
32                   analogue of "cmp" for data. Takes two arguments and
33                   compares them considered as a group.
34
35       "freeze"    returns a string that encupsulates its arguments
36                   (considered as a group). "thaw"ing this string leads to a
37                   fatal error if arguments to "freeze" contained references
38                   to "GLOB"s and "CODE"s.
39
40       "safeFreeze"
41                   returns a string that encupsulates its arguments
42                   (considered as a group). The result is "thaw"able in the
43                   same process. "thaw"ing the result in a different process
44                   should result in a fatal error if arguments to "safeFreeze"
45                   contained references to "GLOB"s and "CODE"s.
46
47       "thaw"      takes one string argument and returns an array. The
48                   elements of the array are "equivalent" to arguments of the
49                   "freeze" command that created the string. Can result in a
50                   fatal error (see above).
51

Developer API

53       "FreezeThaw" "freeze"s and "thaw"s data blessed in some package by
54       calling methods "Freeze" and "Thaw" in the package. The fallback
55       methods are provided by the "FreezeThaw" itself. The fallback "Freeze"
56       freezes the "content" of blessed object (from Perl point of view). The
57       fallback "Thaw" blesses the "thaw"ed data back into the package.
58
59       So the package needs to define its own methods only if the fallback
60       methods will fail (for example, for a lot of data the "content" of an
61       object is an address of some C data). The methods are called like
62
63         $newcooky = $obj->Freeze($cooky);
64         $obj = Package->Thaw($content,$cooky);
65
66       To save and restore the data the following method are applicable:
67
68         $cooky->FreezeScalar($data,$ignorePackage,$noduplicate);
69
70       during Freeze()ing, and
71
72         $data = $cooky->ThawScalar;
73
74       Two optional arguments $ignorePackage and $noduplicate regulate whether
75       the freezing should not call the methods even if $data is a reference
76       to a blessed object, and whether the data should not be marked as seen
77       already even if it was seen before. The default methods
78
79         sub UNIVERSAL::Freeze {
80           my ($obj, $cooky) = (shift, shift);
81           $cooky->FreezeScalar($obj,1,1);
82         }
83
84         sub UNIVERSAL::Thaw {
85           my ($package, $cooky) = (shift, shift);
86           my $obj = $cooky->ThawScalar;
87           bless $obj, $package;
88         }
89
90       call the "FreezeScalar" method of the $cooky since the freezing engine
91       will see the data the second time during this call. Indeed, it is the
92       freezing engine who calls UNIVERSAL::Freeze(), and it calls it because
93       it needs to freeze $obj. The above call to $cooky->FreezeScalar()
94       handles the same data back to engine, but because flags are different,
95       the code does not cycle.
96
97       Freezing and thawing $cooky also allows the following additional
98       methods:
99
100         $cooky->isSafe;
101
102       to find out whether the current freeze was initiated by "freeze" or
103       "safeFreeze" command. Analogous method for thaw $cooky returns whether
104       the current thaw operation is considered safe (i.e., either does not
105       contain cached elsewhere data, or comes from the same application). You
106       can use
107
108         $cooky->makeSafe;
109
110       to prohibit cached data for the duration of the rest of freezing or
111       thawing of current object.
112
113       Two methods
114
115         $value = $cooky->repeatedOK;
116         $cooky->noRepeated;           # Now repeated are prohibited
117
118       allow to find out/change the current setting for allowing repeated
119       references.
120
121       If you want to flush the cache of saved objects you can use
122
123         FreezeThaw->flushCache;
124
125       this can invalidate some frozen string, so that thawing them will
126       result in fatal error.
127
128   Instantiating
129       Sometimes, when an object from a package is recreated in presense of
130       repeated references, it is not safe to recreate the internal structure
131       of an object in one step. In such a situation recreation of an object
132       is carried out in two steps: in the first the object is "allocate"d, in
133       the second it is "instantiate"d.
134
135       The restriction is that during the allocation step you cannot use any
136       reference to any Perl object that can be referenced from any other
137       place. This restriction is applied since that object may not exist yet.
138
139       Correspondingly, during instantiation step the previosly allocated
140       object should be "filled", i.e., it can be changed in any way such that
141       the references to this object remain valid.
142
143       The methods are called like this:
144
145         $pre_object_ref = Package->Allocate($pre_pre_object_ref);
146               # Returns reference
147         Package->Instantiate($pre_object_ref,$cooky);
148               # Converts into reference to blessed object
149
150       The reverse operations are
151
152         $object_ref->FreezeEmpty($cooky);
153         $object_ref->FreezeInstance($cooky);
154
155       during these calls object can "freezeScalar" some information (in a
156       usual way) that will be used during "Allocate" and "Instantiate" calls
157       (via "thawScalar"). Note that the return value of "FreezeEmpty" is
158       cached during the phase of creation of uninialized objects. This must
159       be used like this: the return value is the reference to the created
160       object, so it is not destructed until other objects are created, thus
161       the frozen values of the different objects will not share the same
162       references. Example of bad result:
163
164         $o1->FreezeEmpty($cooky)
165
166       freezes "{}", and "$o2->FreezeEmpty($cooky)" makes the same. Now nobody
167       guaranties that that these two copies of "{}" are different, unless a
168       reference to the first one is preserved during the call to
169       "$o2->FreezeEmpty($cooky)". If "$o1->FreezeEmpty($cooky)" returns the
170       value of "{}" it uses, it will be preserved by the engine.
171
172       The helper function "FreezeThaw::copyContents" is provided for
173       simplification of instantiation. The syntax is
174
175         FreezeThaw::copyContents $to, $from;
176
177       The function copies contents the object $from point to into what the
178       object $to points to (including package for blessed references). Both
179       arguments should be references.
180
181       The default methods are provided. They do the following:
182
183       "FreezeEmpty"
184                   Freezes an empty object of underlying type.
185
186       "FreezeInstance"
187                   Calls "Freeze".
188
189       "Allocate"  Thaws what was frozen by "FreezeEmpty".
190
191       "Instantiate"
192                   Thaws what was frozen by "FreezeInstance", uses
193                   "copyContents" to transfer this to the $pre_object.
194

BUGS and LIMITATIONS

196       A lot of objects are blessed in some obscure packages by XSUB typemaps.
197       It is not clear how to (automatically) prevent the "UNIVERSAL" methods
198       to be called for objects in these packages.
199
200       The objects which can survive freeze()/thaw() cycle must also survive a
201       change of a "member" to an equal member.  Say, after
202
203         $a = [a => 3];
204         $a->{b} = \ $a->{a};
205
206       $a satisfies
207
208         $a->{b} == \ $a->{a}
209
210       This property will be broken by freeze()/thaw(), but it is also broken
211       by
212
213         $a->{a} = delete $a->{a};
214
215
216
217perl v5.34.0                      2021-07-22                     FreezeThaw(3)
Impressum