man FreezeThaw () - converting Perl structures to strings and back.

NAME

FreezeThaw - converting Perl structures to strings and back.

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!"}

DESCRIPTION

Converts data to/from stringified form, appropriate for saving-to/reading-from permanent storage.

Deals with objects, circular lists, repeated appearence of the same refence. Does not deal with overloaded stringify operator yet.

EXPORT

Default
None.
Exportable
CWfreeze thaw cmpStr cmpStrHard safeFreeze.

User API

analogue of CWcmp for data. Takes two arguments and compares them as separate entities. analogue of CWcmp for data. Takes two arguments and compares them considered as a group. returns a string that encupsulates its arguments (considered as a group). CWthawing this string leads to a fatal error if arguments to CWfreeze contained references to CWGLOBs and CWCODEs. returns a string that encupsulates its arguments (considered as a group). The result is CWthawable in the same process. CWthawing the result in a different process should result in a fatal error if arguments to CWsafeFreeze contained references to CWGLOBs and CWCODEs. takes one string argument and returns an array. The elements of the array are equivalent to arguments of the CWfreeze command that created the string. Can result in a fatal error (see above).

Developer API

CWFreezeThaw CWfreezes and CWthaws data blessed in some package by calling methods CWFreeze and CWThaw in the package. The fallback methods are provided by the CWFreezeThaw itself. The fallback CWFreeze freezes the content of blessed object (from Perl point of view). The fallback CWThaw blesses the CWthawed data back into the package.

So the package needs to define its own methods only if the fallback methods will fail (for example, for a lot of data the content of an object is an address of some C data). The methods are called like

  $newcooky = $obj->Freeze($cooky);
  $obj = Package->Thaw($content,$cooky);

To save and restore the data the following method are applicable:

  $cooky->FreezeScalar($data,$ignorePackage,$noduplicate);

during Freeze()ing, and

  $data = $cooky->ThawScalar;

Two optional arguments CW$ignorePackage and CW$noduplicate regulate whether the freezing should not call the methods even if CW$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

  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;
  }

call the CWFreezeScalar method of the CW$cooky since the freezing engine will see the data the second time during this call. Indeed, it is the freezing engine who calls UNIVERSAL::Freeze(), and it calls it because it needs to freeze CW$obj. The above call to CW$cooky->FreezeScalar() handles the same data back to engine, but because flags are different, the code does not cycle.

Freezing and thawing CW$cooky also allows the following additional methods:

  $cooky->isSafe;

to find out whether the current freeze was initiated by CWfreeze or CWsafeFreeze command. Analogous method for thaw CW$cooky returns whether the current thaw operation is considered safe (i.e., either does not contain cached elsewhere data, or comes from the same application). You can use

  $cooky->makeSafe;

to prohibit cached data for the duration of the rest of freezing or thawing of current object.

Two methods

  $value = $cooky->repeatedOK;
  $cooky->noRepeated;           # Now repeated are prohibited

allow to find out/change the current setting for allowing repeated references.

If you want to flush the cache of saved objects you can use

  FreezeThaw->flushCache;

this can invalidate some frozen string, so that thawing them will result in fatal error.

Instantiating

Sometimes, when an object from a package is recreated in presense of repeated references, it is not safe to recreate the internal structure of an object in one step. In such a situation recreation of an object is carried out in two steps: in the first the object is CWallocated, in the second it is CWinstantiated.

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. This restriction is applied since that object may not exist yet.

Correspondingly, during instantiation step the previosly allocated object should be CWfilled, 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 object can CWfreezeScalar some information (in a usual way) that will be used during CWAllocate and CWInstantiate calls (via CWthawScalar). Note that the return value of CWFreezeEmpty 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 bad result:

  $o1->FreezeEmpty($cooky)

freezes CW{}, and CW$o2->FreezeEmpty($cooky) makes the same. Now nobody guaranties that that these two copies of CW{} are different, unless a reference to the first one is preserved during the call to CW$o2->FreezeEmpty($cooky). If CW$o1->FreezeEmpty($cooky) returns the value of CW{} it uses, it will be preserved by the engine.

The helper function CWFreezeThaw::copyContents is provided for simplification of instantiation. The syntax is

  FreezeThaw::copyContents $to, $from;

The function copies contents the object CW$from point to into what the object CW$to points to (including package for blessed references). Both arguments should be references.

The default methods are provided. They do the following: Freezes an empty object of underlying type. Calls CWFreeze. Thaws what was frozen by CWFreezeEmpty. Thaws what was frozen by CWFreezeInstance, uses CWcopyContents to transfer this to the CW$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 CWUNIVERSAL methods to be called for objects in these packages.

The objects which can survive 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};