clone#
Make an independent copy of the current digest state.
Synopsis#
my $copy = $ctx->clone;
What you get back#
A new Digest::MD5 object whose internal state is a byte-for-byte
copy of the original — same accumulated length, same four state
words, same partial-block buffer. The two contexts are independent
afterwards: further add calls on one do not affect the other.
The class of the returned object matches the class of $ctx, so
cloning a subclass instance yields a subclass instance.
The primary use is peeking at an intermediate digest without
destroying the ongoing computation. Because digest / hexdigest
/ b64digest reset the context, the pattern
$ctx->clone->hexdigest is how you extract a snapshot and keep
hashing.
Examples#
Per-line running digest over a stream:
my $ctx = Digest::MD5->new;
while (my $line = <$fh>) {
$ctx->add($line);
print "after line $.: ", $ctx->clone->hexdigest, "\n";
}
my $final = $ctx->hexdigest; # digest of the whole stream
Fork a digest to feed two suffixes:
my $base = Digest::MD5->new;
$base->add("common prefix ");
my $a = $base->clone->add("suffix A")->hexdigest;
my $b = $base->clone->add("suffix B")->hexdigest;
Edge cases#
Cloning immediately after
newyields a context equivalent to a freshDigest::MD5->new.Cloning an object whose state was restored via
contextcopies that restored state, not a fresh one.
Differences from upstream#
Fully compatible with upstream Digest::MD5 2.58.
See also#
new— create a fresh empty context from scratchdigest— the destructive read that makescloneworth havingcontext— save/restore raw state if you need persistenceadd— continue feeding data into the clone or the original