From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 8078A1FA0A for ; Sat, 18 Apr 2020 03:38:54 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 5/8] favor `do {}' over `eval {}' for localized slurp Date: Sat, 18 Apr 2020 03:38:50 +0000 Message-Id: <20200418033853.9798-6-e@yhbt.net> In-Reply-To: <20200418033853.9798-1-e@yhbt.net> References: <20200418033853.9798-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: I did not know to use the return value of `do' back in the day. There's probably no practical difference in these cases, but `eval' is overkill for these uses and may hide actual errors. We can get rid of a few redundant `scalar' ops and pass scalar refs to Email::MIME->new to avoid copies in a few more places, too. --- script/public-inbox-learn | 6 +++--- script/public-inbox-mda | 2 +- script/public-inbox-purge | 2 +- scripts/import_maildir | 4 ++-- scripts/import_slrnspool | 2 +- scripts/slrnspool2maildir | 2 +- scripts/ssoma-replay | 5 +---- t/v2reindex.t | 3 +-- t/watch_maildir_v2.t | 2 +- 9 files changed, 12 insertions(+), 16 deletions(-) diff --git a/script/public-inbox-learn b/script/public-inbox-learn index 0d6c989b..4c10b68b 100644 --- a/script/public-inbox-learn +++ b/script/public-inbox-learn @@ -20,9 +20,9 @@ if ($train !~ /\A(?:ham|spam|rm)\z/) { my $spamc = PublicInbox::Spamcheck::Spamc->new; my $pi_config = PublicInbox::Config->new; my $err; -my $mime = PublicInbox::MIME->new(eval { +my $mime = PublicInbox::MIME->new(do{ local $/; - my $data = scalar ; + my $data = ; $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s; if ($train ne 'rm') { @@ -36,7 +36,7 @@ my $mime = PublicInbox::MIME->new(eval { }; $err = $@; } - $data + \$data }); sub remove_or_add ($$$$) { diff --git a/script/public-inbox-mda b/script/public-inbox-mda index f37c7492..54d0af01 100755 --- a/script/public-inbox-mda +++ b/script/public-inbox-mda @@ -29,7 +29,7 @@ use PublicInbox::Spamcheck; # in case there's bugs in our code or user error. my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/"; $ems = PublicInbox::Emergency->new($emergency); -my $str = eval { local $/; }; +my $str = do { local $/; }; $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s; $ems->prepare(\$str); my $simple = Email::Simple->new(\$str); diff --git a/script/public-inbox-purge b/script/public-inbox-purge index c9b69c3d..8301b06d 100755 --- a/script/public-inbox-purge +++ b/script/public-inbox-purge @@ -21,7 +21,7 @@ GetOptions($opt, @PublicInbox::AdminEdit::OPT) or my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt); PublicInbox::AdminEdit::check_editable(\@ibxs); -my $data = do { local $/; scalar }; +my $data = do { local $/; }; $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s; my $n_purged = 0; diff --git a/scripts/import_maildir b/scripts/import_maildir index fbf3f649..f4e82543 100755 --- a/scripts/import_maildir +++ b/scripts/import_maildir @@ -28,7 +28,7 @@ my @msgs; foreach my $sub (qw(cur new)) { foreach my $fn (glob("$dir/$sub/*")) { open my $fh, '<', $fn or next; - my $s = Email::Simple->new(eval { local $/; <$fh> }); + my $s = Email::Simple->new(do { local $/; <$fh> }); my $date = $s->header('Date'); my $t = eval { str2time($date) }; defined $t or next; @@ -45,7 +45,7 @@ my $im = PublicInbox::Import->new($git, $name, $email); while (my $ary = pop @msgs) { my $fn = "$dir/$ary->[1]"; open my $fh, '<', $fn or next; - my $mime = PublicInbox::MIME->new(eval { local $/; <$fh> }); + my $mime = PublicInbox::MIME->new(do { local $/; <$fh> }); $im->add($mime); } $im->done; diff --git a/scripts/import_slrnspool b/scripts/import_slrnspool index e569d004..480e7b4f 100755 --- a/scripts/import_slrnspool +++ b/scripts/import_slrnspool @@ -70,7 +70,7 @@ for (; $exit == 0 && $n < $max; $n++) { $max = $n + $max_gap; print STDERR $fn, "\n"; - my $mime = PublicInbox::MIME->new(eval { local $/; <$fh> }); + my $mime = PublicInbox::MIME->new(do { local $/; <$fh> }); $filter->scrub($mime); $im->add($mime); diff --git a/scripts/slrnspool2maildir b/scripts/slrnspool2maildir index 0c21806a..8e444e84 100755 --- a/scripts/slrnspool2maildir +++ b/scripts/slrnspool2maildir @@ -23,7 +23,7 @@ foreach my $sub (qw(cur new tmp)) { foreach my $n (grep(/\d+\z/, glob("$spool/*"))) { if (open my $fh, '<', $n) { - my $f = Email::Filter->new(data => eval { local $/; <$fh> }); + my $f = Email::Filter->new(data => do { local $/; <$fh> }); my $s = $f->simple; # gmane rewrites Received headers, which increases spamminess diff --git a/scripts/ssoma-replay b/scripts/ssoma-replay index 3e928084..07121423 100755 --- a/scripts/ssoma-replay +++ b/scripts/ssoma-replay @@ -30,10 +30,7 @@ use Email::Simple; use URI::Escape qw/uri_escape_utf8/; use File::Temp qw/tempfile/; my ($fh, $filename) = tempfile('ssoma-replay-XXXXXXXX', TMPDIR => 1); -my $msg = eval { - local $/; - Email::Simple->new(); -}; +my $msg = Email::Simple->new(do { local $/; }); select $fh; # Note: the archive URL makes assumptions about where the diff --git a/t/v2reindex.t b/t/v2reindex.t index 7c14117a..b6164ff8 100644 --- a/t/v2reindex.t +++ b/t/v2reindex.t @@ -18,12 +18,11 @@ my $ibx_config = { -primary_address => 'test@example.com', indexlevel => 'full', }; -my $agpl = eval { +my $agpl = do { open my $fh, '<', 'COPYING' or die "can't open COPYING: $!"; local $/; <$fh>; }; -$agpl or die "AGPL or die :P\n"; my $phrase = q("defending all users' freedom"); my $mime = PublicInbox::MIME->create( header => [ diff --git a/t/watch_maildir_v2.t b/t/watch_maildir_v2.t index b2514c16..685cd6ed 100644 --- a/t/watch_maildir_v2.t +++ b/t/watch_maildir_v2.t @@ -125,7 +125,7 @@ More majordomo info at http://vger.kernel.org/majordomo-info.html\n); { my $patch = 't/data/0001.patch'; open my $fh, '<', $patch or die "failed to open $patch: $!\n"; - $msg = eval { local $/; <$fh> }; + $msg = do { local $/; <$fh> }; PublicInbox::Emergency->new($maildir)->prepare(\$msg); PublicInbox::WatchMaildir->new($config)->scan('full'); my ($nr, $msgs) = $srch->reopen->query('dfpost:6e006fd7');