user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@yhbt.net>
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	[thread overview]
Message-ID: <20200418033853.9798-6-e@yhbt.net> (raw)
In-Reply-To: <20200418033853.9798-1-e@yhbt.net>

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 <STDIN>;
+	my $data = <STDIN>;
 	$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 $/; <STDIN> };
+my $str = do { local $/; <STDIN> };
 $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 <STDIN> };
+my $data = do { local $/; <STDIN> };
 $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(<STDIN>);
-};
+my $msg = Email::Simple->new(do { local $/; <STDIN> });
 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');

  parent reply	other threads:[~2020-04-18  3:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-18  3:38 [PATCH 0/8] some small yak shaving things Eric Wong
2020-04-18  3:38 ` [PATCH 1/8] inboxwritable: mime_from_path: reuse in more places Eric Wong
2020-04-18  3:38 ` [PATCH 2/8] searchidx: die on cat-file failures Eric Wong
2020-04-18  3:38 ` [PATCH 3/8] inbox: don't memoize missing description|cloneurl Eric Wong
2020-04-18  3:38 ` [PATCH 4/8] inbox: replace `eval {}' with `do {}' where appropriate Eric Wong
2020-04-18  3:38 ` Eric Wong [this message]
2020-04-18  3:38 ` [PATCH 6/8] wwwatomstream: move {emit_header} field to $self Eric Wong
2020-04-18  3:38 ` [PATCH 7/8] mbox: use per-message line-ending for From_ line Eric Wong
2020-04-18  3:38 ` [PATCH 8/8] reduce scope of mbox From_ line removal 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

  List information: http://public-inbox.org/README

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

  git send-email \
    --in-reply-to=20200418033853.9798-6-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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