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,
newblesses into that subclass, soMyDigest->newreturns aMyDigestobject whose internals are aDigest::MD5context.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 itreset— explicit alias for the instance-method form ofnewadd— feed data into the context you just createdhexdigest— common companion call for one-shot chained use