From dd986388c89a46417c7513478adcf1da1d4b92ae Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 9 Jun 2023 10:31:08 +0000 Subject: add compat package for List::Util::uniqstr This will make it easier to switch in the far future while making callers easier-to-read (and more callers will be added). Anyways, Perl 5.26 is a long time away for enterprise users; but isolating compatibility code away can improve readability of code we actually care about in the meantime. --- lib/PublicInbox/CodeSearchIdx.pm | 7 +++---- lib/PublicInbox/Compat.pm | 24 ++++++++++++++++++++++++ lib/PublicInbox/Inbox.pm | 10 +++------- lib/PublicInbox/LeiImport.pm | 4 ++-- lib/PublicInbox/LeiImportKw.pm | 4 ++-- lib/PublicInbox/LeiMailSync.pm | 17 ++++++++--------- lib/PublicInbox/SolverGit.pm | 4 ++-- 7 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 lib/PublicInbox/Compat.pm (limited to 'lib') diff --git a/lib/PublicInbox/CodeSearchIdx.pm b/lib/PublicInbox/CodeSearchIdx.pm index 22097d03..ba14e52a 100644 --- a/lib/PublicInbox/CodeSearchIdx.pm +++ b/lib/PublicInbox/CodeSearchIdx.pm @@ -31,6 +31,7 @@ use PublicInbox::OnDestroy; use PublicInbox::CidxLogP; use PublicInbox::CidxComm; use PublicInbox::Git qw(%OFMT2HEXLEN); +use PublicInbox::Compat qw(uniqstr); use Socket qw(MSG_EOR); use Carp (); our ( @@ -578,8 +579,7 @@ sub load_existing ($) { # for -u/--update } push @$dirs, @cur; } - my %uniq; # List::Util::uniq requires Perl 5.26+ - @$dirs = grep { !$uniq{$_}++ } @$dirs; + @$dirs = uniqstr @$dirs; } # SIG handlers: @@ -912,8 +912,7 @@ sub cidx_run { # main entry point $_ =~ /$re/ ? (push(@excl, $_), 0) : 1; } @{$self->{git_dirs}}; warn("# excluding $_\n") for @excl; - my %uniq; # List::Util::uniq requires Perl 5.26+ - @GIT_DIR_GONE = grep { !$uniq{$_}++ } (@GIT_DIR_GONE, @excl); + @GIT_DIR_GONE = uniqstr @GIT_DIR_GONE, @excl; } local $NCHANGE = 0; local $LIVE_JOBS = $self->{-opt}->{jobs} || diff --git a/lib/PublicInbox/Compat.pm b/lib/PublicInbox/Compat.pm new file mode 100644 index 00000000..78cba90e --- /dev/null +++ b/lib/PublicInbox/Compat.pm @@ -0,0 +1,24 @@ +# Copyright (C) all contributors +# License: AGPL-3.0+ + +# compatibility code for old Perl and standard modules, mainly +# List::Util but maybe other stuff +package PublicInbox::Compat; +use v5.12; +use parent qw(Exporter); +require List::Util; + +our @EXPORT_OK = qw(uniqstr); + +# uniqstr is in List::Util 1.45+, which means Perl 5.26+; +# so maybe 2030 for us since we need to support enterprise distros. +# We can use uniqstr everywhere in our codebase and don't need +# to account for special cases of `uniqnum' nor `uniq' in List::Util +# even if they make more sense in some contexts +no warnings 'once'; +*uniqstr = List::Util->can('uniqstr') // sub (@) { + my %seen; + grep { !$seen{$_}++ } @_; +}; + +1; diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm index cb98d2ad..9afbb478 100644 --- a/lib/PublicInbox/Inbox.pm +++ b/lib/PublicInbox/Inbox.pm @@ -10,6 +10,7 @@ use PublicInbox::MID qw(mid2path); use PublicInbox::Eml; use List::Util qw(max); use Carp qw(croak); +use PublicInbox::Compat qw(uniqstr); # returns true if further checking is required sub check_inodes ($) { @@ -250,11 +251,7 @@ EOM # nntp://news.example.com/alt.example push @m, $u; } - - # List::Util::uniq requires Perl 5.26+, maybe we - # can use it by 2030 or so - my %seen; - @urls = grep { !$seen{$_}++ } (@urls, @m); + @urls = uniqstr @urls, @m; } \@urls; } @@ -274,8 +271,7 @@ sub pop3_url { @urls = map { m!\Apop3?s?://! ? $_ : "pop3://$_" } @$ps; if (my $mi = $self->{'pop3mirror'}) { my @m = map { m!\Apop3?s?://! ? $_ : "pop3://$_" } @$mi; - my %seen; # List::Util::uniq requires Perl 5.26+ - @urls = grep { !$seen{$_}++ } (@urls, @m); + @urls = uniqstr @urls, @m; } my $n = 0; for (@urls) { $n += s!/+\z!! } diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm index 9053048a..a324a652 100644 --- a/lib/PublicInbox/LeiImport.pm +++ b/lib/PublicInbox/LeiImport.pm @@ -7,6 +7,7 @@ use strict; use v5.10.1; use parent qw(PublicInbox::IPC PublicInbox::LeiInput); use PublicInbox::InboxWritable qw(eml_from_path); +use PublicInbox::Compat qw(uniqstr); # /^input_/ subs are used by (or override) PublicInbox::LeiInput superclass @@ -40,8 +41,7 @@ sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn my @oidbin = $lms ? $lms->name_oidbin($folder, $bn) : (); @oidbin > 1 and warn("W: $folder/*/$$bn not unique:\n", map { "\t".unpack('H*', $_)."\n" } @oidbin); - my %seen; - my @docids = sort { $a <=> $b } grep { !$seen{$_}++ } + my @docids = sort { $a <=> $b } uniqstr map { $lse->over->oidbin_exists($_) } @oidbin; my $vmd = $self->{-import_kw} ? { kw => $kw } : undef; if (scalar @docids) { diff --git a/lib/PublicInbox/LeiImportKw.pm b/lib/PublicInbox/LeiImportKw.pm index 4dd938f5..4b8e69fb 100644 --- a/lib/PublicInbox/LeiImportKw.pm +++ b/lib/PublicInbox/LeiImportKw.pm @@ -7,6 +7,7 @@ package PublicInbox::LeiImportKw; use strict; use v5.10.1; use parent qw(PublicInbox::IPC); +use PublicInbox::Compat qw(uniqstr); sub new { my ($cls, $lei) = @_; @@ -38,8 +39,7 @@ sub ck_update_kw { # via wq_io_do my $uid_url = "$url/;UID=$uid"; @oidbin > 1 and warn("W: $uid_url not unique:\n", map { "\t".unpack('H*', $_)."\n" } @oidbin); - my %seen; - my @docids = sort { $a <=> $b } grep { !$seen{$_}++ } + my @docids = sort { $a <=> $b } uniqstr map { $self->{over}->oidbin_exists($_) } @oidbin; $self->{lse}->kw_changed(undef, $kw, \@docids) or return; $self->{verbose} and $self->{lei}->qerr("# $uid_url => @$kw\n"); diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm index 308b1695..415459d5 100644 --- a/lib/PublicInbox/LeiMailSync.pm +++ b/lib/PublicInbox/LeiMailSync.pm @@ -6,6 +6,7 @@ package PublicInbox::LeiMailSync; use strict; use v5.10.1; use parent qw(PublicInbox::Lock); +use PublicInbox::Compat qw(uniqstr); use DBI qw(:sql_types); # SQL_BLOB use PublicInbox::ContentHash qw(git_sha); use Carp (); @@ -543,20 +544,19 @@ EOM --all=@no not accepted (must be `local' and/or `remote') EOM } - my (%seen, @inc); my @all = $self->folders; for my $ok (@ok) { if ($ok eq 'local') { - @inc = grep(!m!\A[a-z0-9\+]+://!i, @all); + push @$folders, grep(!m!\A[a-z0-9\+]+://!i, @all); } elsif ($ok eq 'remote') { - @inc = grep(m!\A[a-z0-9\+]+://!i, @all); + push @$folders, grep(m!\A[a-z0-9\+]+://!i, @all); } elsif ($ok ne '') { return $lei->fail("--all=$all not understood"); } else { - @inc = @all; + push @$folders, @all; } - push(@$folders, (grep { !$seen{$_}++ } @inc)); } + @$folders = uniqstr @$folders; scalar(@$folders) || $lei->fail(<execute($fid, $uid); - my %uniq; # for public-inbox <= 1.7.0 - grep { !$uniq{$_}++ } map { $_->[0] } @{$sth->fetchall_arrayref}; + # for public-inbox <= 1.7.0: + uniqstr(map { $_->[0] } @{$sth->fetchall_arrayref}); } sub name_oidbin ($$$) { @@ -674,8 +674,7 @@ EOM $sth->bind_param(2, $nm, SQL_VARCHAR); $sth->execute; my @old = map { $_->[0] } @{$sth->fetchall_arrayref}; - my %uniq; # for public-inbox <= 1.7.0 - grep { !$uniq{$_}++ } (@bin, @old); + uniqstr @bin, @old # for public-inbox <= 1.7.0 } sub imap_oidhex { diff --git a/lib/PublicInbox/SolverGit.pm b/lib/PublicInbox/SolverGit.pm index ebb5cdff..5f317f51 100644 --- a/lib/PublicInbox/SolverGit.pm +++ b/lib/PublicInbox/SolverGit.pm @@ -18,6 +18,7 @@ use PublicInbox::Qspawn; use PublicInbox::Tmpfile; use PublicInbox::GitAsyncCat; use PublicInbox::Eml; +use PublicInbox::Compat qw(uniqstr); use URI::Escape qw(uri_escape_utf8); # POSIX requires _POSIX_ARG_MAX >= 4096, and xargs is required to @@ -556,8 +557,7 @@ sub extract_diffs_done { my $diffs = delete $self->{tmp_diffs}; if (scalar @$diffs) { unshift @{$self->{patches}}, @$diffs; - my %seen; # List::Util::uniq requires Perl 5.26+ :< - my @u = grep { !$seen{$_}++ } map { di_url($self, $_) } @$diffs; + my @u = uniqstr(map { di_url($self, $_) } @$diffs); dbg($self, "found $want->{oid_b} in " . join(" ||\n\t", @u)); ++$self->{nr_p}; -- cgit v1.2.3-24-ge0c7