From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id C31431F451 for ; Tue, 9 Jan 2024 08:49:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1704790178; bh=GOBMpHqSjTYmFEvAZ5jM3p1lHSupAZc6/kEvXqeZQUg=; h=From:To:Subject:Date:From; b=hJR7k86YW2xBpk9KQig7p6sREDx+tagflbn4kLWsvSoG0GrYCNvtMdLVtt41mFpMT 7Yi1+zjdS+hDE0czPBX9LCws8tpUOVQvXNgRKXR+bUlDUXSeJjSc3d2vyjNjEMySmY BA75dYUHxh2+vqFYYzIrB/RMeQ6LCPRCri0apvSA= From: Eric Wong To: spew@80x24.org Subject: [PATCH] compat: add hash_value from Hash::Util Date: Tue, 9 Jan 2024 08:49:38 +0000 Message-ID: <20240109084938.1059967-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 # License: AGPL-3.0+ -# 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 +# License: AGPL-3.0+ +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