add#
Feed one or more pieces of data into the MD5 context.
Synopsis#
$ctx->add($data);
$ctx->add($a, $b, $c); # same as add($a.$b.$c)
$ctx->add("a")->add("b")->add("c"); # chained equivalent
What you get back#
The same context object, so add calls chain and compose with
hexdigest / digest / b64digest. Every argument is flattened
into a byte stream in order; the digest depends only on the
concatenation, not on how you split it across calls or arguments.
Examples#
One-shot digest of a concatenated message:
my $hex = Digest::MD5->new->add("hello world")->hexdigest;
## 5eb63bbbe01eeed093cb22bb8f5acdc3
Stream a file line by line:
open(my $fh, '<:raw', $path) or die $!;
my $ctx = Digest::MD5->new;
while (my $line = <$fh>) {
$ctx->add($line);
}
print $ctx->hexdigest, "\n";
Multiple arguments are equivalent to concatenation — all four of these produce the same digest:
Digest::MD5->new->add("a")->add("b")->add("c")->hexdigest;
Digest::MD5->new->add("a", "b", "c")->hexdigest;
Digest::MD5->new->add("abc")->hexdigest;
md5_hex("abc");
Edge cases#
Wide characters. Arguments containing code points above
U+00FFcroak withWide character in subroutine entry. Encode first:$ctx->add(encode_utf8($str)).Empty strings are accepted and change nothing.
No arguments (
$ctx->addwith no extra args) is a no-op and returns the context.undefarguments stringify to the empty string; warnings depend on the caller’suse warningsstate.
Differences from upstream#
Fully compatible with upstream Digest::MD5 2.58.
See also#
add_bits— interface-compatibility wrapper for bit-oriented inputaddfile— feed an entire file handle without bufferingnew— typical predecessorhexdigest— typical successor; auto-resets the contextclone— snapshot the intermediate state without stoppingadd