dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] compat: add hash_value from Hash::Util
@ 2024-01-09  8:49 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2024-01-09  8:49 UTC (permalink / raw)
  To: spew

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] compat: add hash_value from Hash::Util
@ 2024-01-28 21:47 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2024-01-28 21:47 UTC (permalink / raw)
  To: spew

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-28 21:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-09  8:49 [PATCH] compat: add hash_value from Hash::Util Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2024-01-28 21:47 Eric Wong

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).