nfreeze#

Serialise a referenced Perl structure to bytes in network (big-endian) byte order for portable transport.

Synopsis#

use Storable qw(nfreeze thaw);
my $bytes = nfreeze(\@rows);
my $back  = thaw($bytes);     # thaw auto-detects byte order

What you get back#

An opaque byte string prefixed with a marker that identifies the payload as network-order. The reader (thaw, retrieve, fd_retrieve) inspects that marker and byte-swaps as needed, so the same bytes reproduce the structure on any architecture.

Examples#

Send serialised data over a socket between hosts of different endianness:

my $bytes = nfreeze(\%payload);
$socket->send($bytes);

## on the other side, possibly on a different CPU:

my $payload = thaw($received);

Store and retrieve using the portable pair:

open my $fh, ">", "data.bin" or die $!;
print $fh nfreeze(\@records);
close $fh;

Edge cases#

  • Argument must be a reference; a plain scalar croaks with Not a reference.

  • Marginally slower than freeze on little-endian hosts because every multi-byte field is byte-swapped on the way out.

  • The produced bytes differ from those of freeze even when run on a big-endian host, because nfreeze writes a leading marker byte.

Differences from upstream#

  • Byte layout is not wire-compatible with upstream Storable’s network format. nfreeze output round-trips only through pperl’s thaw. Pinned by t/81-xs-native/Storable/270-nfreeze-byteorder.t.

See also#

  • freeze — faster, same-architecture variant.

  • thaw — matching reader; auto-detects byte order.

  • nstore — write portable bytes directly to a file.

  • nstore_fd — write portable bytes to a file descriptor.