new#

Create a fresh MD5 context object, or reset an existing one to empty.

Synopsis#

my $ctx = Digest::MD5->new;   # class method — new empty context
$ctx->new;                    # instance method — reset in place

What you get back#

A blessed Digest::MD5 reference whose internal state is the MD5 initial vector — i.e. the digest of the empty string is md5(""). Called on an existing instance, new resets that instance and returns it; no second object is allocated. The common idiom Digest::MD5->new->add($data)->hexdigest relies on this chaining.

Examples#

Fresh context, then feed and finalise:

my $ctx = Digest::MD5->new;
$ctx->add("hello");
print $ctx->hexdigest;   # 5d41402abc4b2a76b9719d911017c592

Reset an existing context instead of allocating a new one:

$ctx->add("round 1")->hexdigest;   # digest + auto-reset
$ctx->new;                          # explicit reset (idiomatic no-op here)
$ctx->add("round 2");

Chain straight through in one expression:

my $hex = Digest::MD5->new->add("foo", "bar")->hexdigest;

Edge cases#

  • Called on a subclass name, new blesses into that subclass, so MyDigest->new returns a MyDigest object whose internals are a Digest::MD5 context.

  • Called as an instance method, the same object is returned — the caller’s variable is unchanged.

Differences from upstream#

Fully compatible with upstream Digest::MD5 2.58.

See also#

  • clone — copy the current context instead of resetting it

  • reset — explicit alias for the instance-method form of new

  • add — feed data into the context you just created

  • hexdigest — common companion call for one-shot chained use