freeze#
Serialise a referenced Perl structure into an opaque byte string using the host’s native byte order.
Synopsis#
use Storable qw(freeze thaw);
my $bytes = freeze(\%config);
my $copy = thaw($bytes);
What you get back#
A single scalar holding the binary image of the structure. The
bytes are opaque — treat them as a blob, not text. They can be
stored, transmitted, or passed straight back to thaw. Native
byte order keeps the serialisation fast but the result is only
portable between machines with the same endianness and integer
width; for cross-architecture transport use nfreeze.
Examples#
Freeze and immediately thaw a hash reference:
my $bytes = freeze({ host => "db1", port => 5432 });
my $cfg = thaw($bytes);
print $cfg->{host}; # db1
Freeze a nested structure with shared references:
my $shared = [1, 2, 3];
my $bytes = freeze({ a => $shared, b => $shared });
my $h = thaw($bytes);
$h->{a}[0] = 99;
print $h->{b}[0]; # 99 — sharing preserved
Round-trip a blessed object:
my $obj = bless { id => 42 }, "Widget";
my $bytes = freeze(\$obj);
my $back = ${ thaw($bytes) }; # same class, same contents
Edge cases#
Argument must be a reference. Passing a plain scalar croaks with
Not a reference.Circular references are preserved; the reader rebuilds the same cycle.
Weak references survive the round trip as weak references.
Differences from upstream#
The on-disk layout differs from upstream’s format. Byte strings produced by this
freezecan only be thawed by pperl’sStorable, not by perl5’s. The reverse also holds. Pinned byt/81-xs-native/Storable/010-freeze-thaw.t.
See also#
thaw— the matching reader.nfreeze— portable (network-order) variant.store— write directly to a named file.dclone— freeze/thaw round-trip as a deep-copy shortcut.