[libpath-tiny-perl] 03/11: Implement a digest method
Jonas Smedegaard
js at alioth.debian.org
Wed Aug 7 16:10:30 UTC 2013
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository libpath-tiny-perl.
commit 8eab7e8cf4700f17d6f6c5a6b145fdecf33a1c75
Author: David Golden <dagolden at cpan.org>
Date: Tue Jun 18 11:53:37 2013 -0400
Implement a digest method
The digest method returns a hexadecimal string digest. This was an
80/20 optimization in line with the Tiny philosophy to keep things
simple and return values comparable to command line tools and what
people would see on a website with a digest to verify a file.
The default algorithm is 'SHA-256' or Digest module constructors may be
given as arguments.
Those with more specific needs should just use Digest directly.
---
lib/Path/Tiny.pm | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/lib/Path/Tiny.pm b/lib/Path/Tiny.pm
index 68f8865..ea655a3 100644
--- a/lib/Path/Tiny.pm
+++ b/lib/Path/Tiny.pm
@@ -403,6 +403,24 @@ sub copy {
or Carp::croak("copy failed for $self to $dest: $!");
}
+=method digest
+
+ $obj = path("/tmp/foo.txt")->digest; # SHA-256
+ $obj = path("/tmp/foo.txt")->digest("MD5"); # user-selected
+
+Returns a hexadecimal digest for a file. Any arguments are passed to the
+constructor for L<Digest> to select an algorithm. If no arguments are given,
+the default is SHA-256.
+
+=cut
+
+sub digest {
+ my ($self, $alg, @args) = @_;
+ $alg = 'SHA-256' unless defined $alg;
+ require Digest;
+ return Digest->new($alg, @args)->add( $self->slurp_raw )->hexdigest;
+}
+
=method dirname
$name = path("/tmp/foo.txt")->dirname; # "/tmp/"
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libpath-tiny-perl.git
More information about the Pkg-perl-cvs-commits
mailing list