1FreezeThaw(3) User Contributed Perl Documentation FreezeThaw(3)
2
3
4
6 FreezeThaw - converting Perl structures to strings and back.
7
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
16 Converts data to/from stringified form, appropriate for saving-to/read‐
17 ing-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
23 Default None.
24
25 Exportable "freeze thaw cmpStr cmpStrHard safeFreeze".
26
28 "cmpStr" analogue of "cmp" for data. Takes two arguments and com‐
29 pares them as separate entities.
30
31 "cmpStrHard"
32 analogue of "cmp" for data. Takes two arguments and com‐
33 pares them considered as a group.
34
35 "freeze" returns a string that encupsulates its arguments (consid‐
36 ered as a group). "thaw"ing this string leads to a fatal
37 error if arguments to "freeze" contained references to
38 "GLOB"s and "CODE"s.
39
40 "safeFreeze"
41 returns a string that encupsulates its arguments (consid‐
42 ered as a group). The result is "thaw"able in the same
43 process. "thaw"ing the result in a different process should
44 result in a fatal error if arguments to "safeFreeze" con‐
45 tained references to "GLOB"s and "CODE"s.
46
47 "thaw" takes one string argument and returns an array. The ele‐
48 ments 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
53 "FreezeThaw" "freeze"s and "thaw"s data blessed in some package by
54 calling methods "Freeze" and "Thaw" in the package. The fallback meth‐
55 ods 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() han‐
94 dles the same data back to engine, but because flags are different, the
95 code does not cycle.
96
97 Freezing and thawing $cooky also allows the following additional meth‐
98 ods:
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 ref‐
119 erences.
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
130 Sometimes, when an object from a package is recreated in presense of
131 repeated references, it is not safe to recreate the internal structure
132 of an object in one step. In such a situation recreation of an object
133 is carried out in two steps: in the first the object is "allocate"d, in
134 the second it is "instantiate"d.
135
136 The restriction is that during the allocation step you cannot use any
137 reference to any Perl object that can be referenced from any other
138 place. This restriction is applied since that object may not exist yet.
139
140 Correspondingly, during instantiation step the previosly allocated
141 object should be "filled", i.e., it can be changed in any way such that
142 the references to this object remain valid.
143
144 The methods are called like this:
145
146 $pre_object_ref = Package->Allocate($pre_pre_object_ref);
147 # Returns reference
148 Package->Instantiate($pre_object_ref,$cooky);
149 # Converts into reference to blessed object
150
151 The reverse operations are
152
153 $object_ref->FreezeEmpty($cooky);
154 $object_ref->FreezeInstance($cooky);
155
156 during these calls object can "freezeScalar" some information (in a
157 usual way) that will be used during "Allocate" and "Instantiate" calls
158 (via "thawScalar"). Note that the return value of "FreezeEmpty" is
159 cached during the phase of creation of uninialized objects. This must
160 be used like this: the return value is the reference to the created
161 object, so it is not destructed until other objects are created, thus
162 the frozen values of the different objects will not share the same ref‐
163 erences. Example of bad result:
164
165 $o1->FreezeEmpty($cooky)
166
167 freezes "{}", and "$o2->FreezeEmpty($cooky)" makes the same. Now nobody
168 guaranties that that these two copies of "{}" are different, unless a
169 reference to the first one is preserved during the call to
170 "$o2->FreezeEmpty($cooky)". If "$o1->FreezeEmpty($cooky)" returns the
171 value of "{}" it uses, it will be preserved by the engine.
172
173 The helper function "FreezeThaw::copyContents" is provided for simpli‐
174 fication of instantiation. The syntax is
175
176 FreezeThaw::copyContents $to, $from;
177
178 The function copies contents the object $from point to into what the
179 object $to points to (including package for blessed references). Both
180 arguments should be references.
181
182 The default methods are provided. They do the following:
183
184 "FreezeEmpty"
185 Freezes an empty object of underlying type.
186
187 "FreezeInstance"
188 Calls "Freeze".
189
190 "Allocate" Thaws what was frozen by "FreezeEmpty".
191
192 "Instantiate"
193 Thaws what was frozen by "FreezeInstance", uses "copyCon‐
194 tents" to transfer this to the $pre_object.
195
197 A lot of objects are blessed in some obscure packages by XSUB typemaps.
198 It is not clear how to (automatically) prevent the "UNIVERSAL" methods
199 to be called for objects in these packages.
200
201 The objects which can survive freeze()/thaw() cycle must also survive a
202 change of a "member" to an equal member. Say, after
203
204 $a = [a => 3];
205 $a->{b} = \ $a->{a};
206
207 $a satisfies
208
209 $a->{b} == \ $a->{a}
210
211 This property will be broken by freeze()/thaw(), but it is also broken
212 by
213
214 $a->{a} = delete $a->{a};
215
216
217
218perl v5.8.8 2002-03-29 FreezeThaw(3)