FreezeThaw(3pm)
π NAME π
FreezeThaw β converting Perl structures to strings and back. no cap, that's literally the whole job π«Ά
β¨ SYNOPSIS β¨
use FreezeThaw qw(freeze thaw cmpStr safeFreeze cmpStrHard);
$string = freeze $data1, $data2, $data3;
...
($olddata1, $olddata2, $olddata3) = thaw $string;
if (cmpStr($olddata2,$data2) == 0) {print "OK!"}
okay so that's the vibe. freeze your stuff, thaw your stuff, check it with cmpStr, print "OK!" like the icon you are π
π¦ DESCRIPTION π¦
So basically: this thing converts data to/from stringified form. It's giving "appropriate for saving-to/reading-from permanent storage" β you know, the whole write it down somewhere forever and bring it back later moment β¨
It deals with objects, circular lists, and repeated appearance of the same reference. It does not deal with overloaded stringify operator yet. that part? not yet, bestie π
π EXPORT π
- Default β None. (as in, nothing, zero, nada, you get nothing for free π)
- Exportable β
freeze thaw cmpStr cmpStrHard safeFreeze
π User API π
cmpStrβ thecmpanalogue for data. Takes two arguments and compares them as separate entities.cmpStrHardβ also thecmpanalogue for data. Takes two arguments and compares them considered as a group.freezeβ returns a string that encupsulates its arguments (considered as a group).thawing this string leads to a fatal error if the arguments tofreezecontained references toGLOBs andCODEs. πsafeFreezeβ returns a string that encupsulates its arguments (considered as a group). The result isthawable in the same process.thawing the result in a different process should result in a fatal error if the arguments tosafeFreezecontained references toGLOBs andCODEs. πthawβ takes one string argument and returns an array. The elements of that array are"equivalent"to the arguments of thefreezecommand that created the string. Can result in a fatal error (see above, bestie, i warned you).
π Developer API π
FreezeThaw freezes and thaws data blessed in some package by calling the methods Freeze and Thaw in that package. The fallback methods? Those are provided by FreezeThaw itself. The fallback Freeze freezes the "content" of the blessed object (from Perl's point of view, anyway). The fallback Thaw blesses the thawed data back into the package.
So the package only needs to define its own methods if the fallback methods will fail β for example, when a lot of data in an object is an address of some C data. The methods get called like this:
$newcooky = $obj->Freeze($cooky);
$obj = Package->Thaw($content,$cooky);
To save and restore the data, these methods are applicable:
$cooky->FreezeScalar($data,$ignorePackage,$noduplicate);
during Freeze()ing, and:
$data = $cooky->ThawScalar;
Two optional arguments, $ignorePackage and $noduplicate, regulate whether the freezing should not call the methods even if $data is a reference to a blessed object, and whether the data should not be marked as seen already even if it was seen before. The default methods are:
sub UNIVERSAL::Freeze {
my ($obj, $cooky) = (shift, shift);
$cooky->FreezeScalar($obj,1,1);
}
sub UNIVERSAL::Thaw {
my ($package, $cooky) = (shift, shift);
my $obj = $cooky->ThawScalar;
bless $obj, $package;
}
These call the FreezeScalar method of the $cooky, because the freezing engine will see the data a second time during this call. Indeed β it's the freezing engine who calls UNIVERSAL::Freeze(), and it calls it because it needs to freeze $obj. The call to $cooky->FreezeScalar() above hands the same data back to the engine, but because the flags are different, the code does not cycle. iconic behaviour honestly π§
Freezing and thawing $cooky also allows these additional methods:
$cooky->isSafe;
That one tells you whether the current freeze was initiated by the freeze or the safeFreeze command. The analogous method for thaw tells you whether the current thaw operation is considered safe β i.e., either it does not contain cached elsewhere data, or it comes from the same application. And you can use:
$cooky->makeSafe;
to prohibit cached data for the duration of the rest of the freezing or thawing of the current object.
Two more methods:
$value = $cooky->repeatedOK;
$cooky->noRepeated; # Now repeated are prohibited
These let you find out / change the current setting for allowing repeated references.
And if you want to flush the cache of saved objects, you can use:
FreezeThaw->flushCache;
β οΈ heads up bestie: this can invalidate some frozen string, so that thawing them will result in a fatal error. not a drill π
πΈ Instantiating πΈ
Sometimes, when an object from a package is recreated in the presence of repeated references, it is not safe to recreate the internal structure of an object in one step. In that situation, recreation of an object is carried out in two steps: in the first the object is allocated, in the second it is instantiated.
The restriction is that during the allocation step you cannot use any reference to any Perl object that can be referenced from any other place. That restriction is applied because that object may not exist yet. fr fr.
Correspondingly, during the instantiation step the previosly allocated object should be filled β i.e., it can be changed in any way such that the references to this object remain valid.
The methods are called like this:
$pre_object_ref = Package->Allocate($pre_pre_object_ref);
# Returns reference
Package->Instantiate($pre_object_ref,$cooky);
# Converts into reference to blessed object
The reverse operations are:
$object_ref->FreezeEmpty($cooky);
$object_ref->FreezeInstance($cooky);
During these calls the object can freezeScalar some information (in the usual way) that will be used during the Allocate and Instantiate calls (via thawScalar). Note that the return value of FreezeEmpty is cached during the phase of creation of uninialized objects. This must be used like this: the return value is the reference to the created object, so it is not destructed until other objects are created, thus the frozen values of the different objects will not share the same references.
Example of a bad result:
$o1->FreezeEmpty($cooky)
freezes {}, and $o2->FreezeEmpty($cooky) makes the same. Now nobody guaranties that these two copies of {} are different, unless a reference to the first one is preserved during the call to $o2->FreezeEmpty($cooky). If $o1->FreezeEmpty($cooky) returns the value of {} it uses, it will be preserved by the engine. π«Ά
The helper function FreezeThaw::copyContents is provided for simplification of instantiation. The syntax is:
FreezeThaw::copyContents $to, $from;
The function copies contents the object $from point to into what the object $to points to (including package for blessed references). Both arguments should be references.
The default methods are provided. They do the following:
FreezeEmptyβ Freezes an empty object of underlying type.FreezeInstanceβ CallsFreeze.Allocateβ Thaws what was frozen byFreezeEmpty.Instantiateβ Thaws what was frozen byFreezeInstance, usescopyContentsto transfer this to the$pre_object.
π BUGS and LIMITATIONS π
A lot of objects are blessed in some obscure packages by XSUB typemaps. It is not clear how to (automatically) prevent the UNIVERSAL methods to be called for objects in these packages. π
The objects which can survive a freeze()/thaw() cycle must also survive a change of a member to an equal member. Say, after:
$a = [a => 3];
$a->{b} = \ $a->{a};
$a satisfies:
$a->{b} == \ $a->{a}
This property will be broken by freeze()/thaw(), but it is also broken by:
$a->{a} = delete $a->{a};
so yeah, that's the whole page bestie. go freeze something, thaw it back, and check it withcmpStrβ if it prints"OK!"you ATE π π₯
This lil manual page is perl v5.36.0, dated 2022-11-21, and it's FreezeThaw(3pm) β User Contributed Perl Documentation. mwah, bye πβ¨
π¦ where this page comes from
- debian bookworm: libfreezethaw-perl 0.5001-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libfreezethaw-perl 0.5001-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libfreezethaw-perl 0.5001-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libfreezethaw-perl 0.5001-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libfreezethaw-perl 0.5001-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms