dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] compat: add hash_value from Hash::Util
Date: Sun, 28 Jan 2024 21:47:03 +0000	[thread overview]
Message-ID: <20240128214703.271892-1-e@80x24.org> (raw)

This will be useful for adding support for a simple cache.
---
 MANIFEST                  |  1 +
 lib/PublicInbox/Compat.pm | 12 +++++++++---
 t/compat.t                | 10 ++++++++++
 3 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 t/compat.t

diff --git a/MANIFEST b/MANIFEST
index 051cd6f9..1943abf1 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -438,6 +438,7 @@ t/clone-coderepo-puh2.sh
 t/clone-coderepo.psgi
 t/clone-coderepo.t
 t/cmd_ipc.t
+t/compat.t
 t/config.t
 t/config_limiter.t
 t/content_hash.t
diff --git a/lib/PublicInbox/Compat.pm b/lib/PublicInbox/Compat.pm
index 78cba90e..eeb122cd 100644
--- a/lib/PublicInbox/Compat.pm
+++ b/lib/PublicInbox/Compat.pm
@@ -1,14 +1,14 @@
 # Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
-# compatibility code for old Perl and standard modules, mainly
-# List::Util but maybe other stuff
+# compatibility code for old Perl and standard modules
 package PublicInbox::Compat;
 use v5.12;
 use parent qw(Exporter);
 require List::Util;
+require Hash::Util;
 
-our @EXPORT_OK = qw(uniqstr);
+our @EXPORT_OK = qw(uniqstr hash_value);
 
 # uniqstr is in List::Util 1.45+, which means Perl 5.26+;
 # so maybe 2030 for us since we need to support enterprise distros.
@@ -21,4 +21,10 @@ no warnings 'once';
 	grep { !$seen{$_}++ } @_;
 };
 
+# Hash::Util::hash_value appeared in Perl 5.18+
+*hash_value = Hash::Util->can('hash_value') // sub {
+	require B;
+	hex(B::hash($_[0]));
+};
+
 1;
diff --git a/t/compat.t b/t/compat.t
new file mode 100644
index 00000000..b8886e14
--- /dev/null
+++ b/t/compat.t
@@ -0,0 +1,10 @@
+#!perl -w
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use v5.12;
+use Test::More;
+use_ok 'PublicInbox::Compat', qw(hash_value);
+
+like hash_value('hello'), qr/\A[0-9]+\z/, 'hash_value looks like an integer';
+
+done_testing

             reply	other threads:[~2024-01-28 21:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-28 21:47 Eric Wong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-01-09  8:49 [PATCH] compat: add hash_value from Hash::Util Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240128214703.271892-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).