Frozen

 view release on metacpan or  search on metacpan

lib/Frozen.pm  view on Meta::CPAN

>> reads it instead of mapping it. It reports C<< $fz->is_mapped == 0 >>, and
the sharing claim does not apply to it.

=head1 WHAT IS REFUSED

At freeze: blessed references, code references, globs, filehandles, references
to scalars, cycles, structures deeper than 256, strings flagged UTF-8 whose
bytes are not valid UTF-8, NVs a double cannot hold at all (pass C<< lossy_nv
=> 1 >> to accept them), and anything over 2 GiB. Each names the path to the
offending value.

At open: a block that is not Frozen's, one written on the other endianness -
refused, never byteswapped - one whose format version this build does not
read, one using the reserved 64-bit offsets, and one whose recorded size
disagrees with its length.

=head1 CORRUPT INPUT

A Frozen block is a build artifact you ship, not a stranger's upload. The bar
is still B<never segfault>, because the realistic failure is a deploy that
half-wrote a file.

Structural validation - bounds, alignment, tag, and counts against the bytes
that remain - is always on. C<< $fz->verify >> is the opt-in O(n) pass:
checksum plus a walk of every reachable node. It is not done at open unless
you ask, because a checksum over fifty megabytes at every boot is exactly the
pass Frozen exists to remove.

Tested by truncating a block at every one of its 3,080 lengths and by 18,000
single-bit flips across six seeds, each in a child process so a crash is an
exit status rather than a dead test run. Clean under ASAN on Linux.

=head1 BUILDING A BLOCK

Both builders take the same options.

C<< flat => '.' >> builds an index over joined paths, which is what makes
C<get> one probe instead of one per segment.

A scalar is stored as the kind Perl says it B<is> - string, then integer, then
float - and never as one of the conversions cached on it along the way.
Reading a number as a string caches that string, and comparing a float against
an integer caches the truncated integer, but neither changed the value: C<1.5>
compared against C<1> is stored as C<1.5>, and C<5> that something printed is
stored as C<5>. C<'007'> is stored as its digits, because a string used as a
number is still a string, and storing C<7> would be what the user sees
changing on the way through. A perl before 5.36 cannot tell the printed C<5>
from the C<'007'>; see L</LIMITATIONS>.

C<< lossy_nv => 1 >> accepts the two NVs a double cannot hold: one past
C<DBL_MAX>, which is stored as an infinity, and a non-zero one under the
smallest denormal, which is stored as a zero.

A block holds an IEEE double. On a perl built C<-Duselongdouble> or
C<-Dusequadmath> the NV is wider than that, so storing one rounds to the
nearest double, and C<0.1> is kept to a double's precision rather than the
NV's. That is the format, not an error, and it is not what C<lossy_nv>
governs: refusing it would refuse C<0.1> outright on those perls. What is
refused without the flag is a narrowing that destroys the value rather than
its tail - a finite NV that would come back as an infinity, a non-zero one
that would come back as zero. An NV that is already infinite, and a NaN, are
stored as they are.

C<< stringify => 1 >> stores every defined non-reference scalar as its string
form, so C<5> is kept as C<"5"> and a string read answers for it. It is for a
reader that returns text and has no use for the difference between a number
and its digits - a translation catalogue, a table of labels - which would
otherwise have to walk its own data and stringify it first. C<undef> is
untouched, so a reader that refuses null can still see it.

Neither is exported. L<Storable> exports C<freeze> and C<thaw>, and a program
importing both modules would get the wrong function.

=head2 freeze

    my $bytes = Frozen->freeze($data, %opts);

Builds a block in memory and returns it as a string of bytes. Croaks, naming
the path to the offending value, for anything the format cannot hold - see
L</WHAT IS REFUSED>.

B<A block records what the SVs are, not what they held.> Asking an integer
for its string caches that string on the SV, so it is afterwards both, and
the block stores the string. C<< print $h->{n} >> is enough to do it. Two
freezes of "the same data" can therefore differ if anything looked at a value
in between. That matters only if you were going to treat the bytes as a
checksum of a Perl structure: they are a faithful record of the SVs as they
were, which is not the same promise.

    my $bytes = Frozen->freeze(
        { messages => { welcome => 'Hello', bye => 'Goodbye' } },
        flat => '.',
    );

    open my $fh, '>:raw', 'catalogue.frz' or die $!;
    print $fh $bytes;

    # numbers kept as text, for a reader that only returns text
    my $labels = Frozen->freeze({ port => 8080, name => 'web' },
                                stringify => 1);
    my $fz = Frozen->attach($labels);
    my ($p) = $fz->get('port');        # "8080", not 8080

=head2 freeze_to

    Frozen->freeze_to($path, $data, %opts);

The same build, written straight to a file. Writes to a temporary beside
C<$path> and renames, so a reader holds either the whole old file or the whole
new one - never a half-written block under a live mapping. This is the
supported way to deploy; see L</DEPLOYING A BLOCK>.

    Frozen->freeze_to('catalogue.frz', {
        messages => { welcome => 'Hello' },
        limits   => { rate => 100, burst => 20 },
    }, flat => '.');

=head1 OPENING A BLOCK

=head2 open



( run in 1.270 second using v1.01-cache-2.11-cpan-60884a8df76 )