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+00FF croak with Wide character in subroutine entry. Encode first: $ctx->add(encode_utf8($str)).

  • Empty strings are accepted and change nothing.

  • No arguments ($ctx->add with no extra args) is a no-op and returns the context.

  • undef arguments stringify to the empty string; warnings depend on the caller’s use warnings state.

Differences from upstream#

Fully compatible with upstream Digest::MD5 2.58.

See also#

  • add_bits — interface-compatibility wrapper for bit-oriented input

  • addfile — feed an entire file handle without buffering

  • new — typical predecessor

  • hexdigest — typical successor; auto-resets the context

  • clone — snapshot the intermediate state without stopping add