user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 5/8] favor `do {}' over `eval {}' for localized slurp
  2020-04-18  3:38  7% [PATCH 0/8] some small yak shaving things Eric Wong
@ 2020-04-18  3:38  5% ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2020-04-18  3:38 UTC (permalink / raw)
  To: meta

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');

^ permalink raw reply related	[relevance 5%]

* [PATCH 0/8] some small yak shaving things
@ 2020-04-18  3:38  7% Eric Wong
  2020-04-18  3:38  5% ` [PATCH 5/8] favor `do {}' over `eval {}' for localized slurp Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2020-04-18  3:38 UTC (permalink / raw)
  To: meta

Eric Wong (8):
  inboxwritable: mime_from_path: reuse in more places
  searchidx: die on cat-file failures
  inbox: do not memoize description or cloneurl if missing
  inbox: replace `eval {}' with `do {}' where appropriate
  favor `do {}' over `eval {}' for localized slurp
  wwwatomstream: move {emit_header} field to $self
  mbox: use per-message line-ending for From_ line
  reduce scope of mbox From_ line removal

 Documentation/mknews.perl               |  2 +-
 MANIFEST                                |  4 ++--
 lib/PublicInbox/Inbox.pm                | 27 ++++++++++++------------
 lib/PublicInbox/InboxWritable.pm        |  4 ++--
 lib/PublicInbox/Mbox.pm                 |  7 +++++--
 lib/PublicInbox/NNTP.pm                 |  2 ++
 lib/PublicInbox/SearchIdx.pm            | 14 +++++--------
 lib/PublicInbox/WatchMaildir.pm         |  6 +++---
 lib/PublicInbox/WwwAtomStream.pm        |  5 ++---
 script/public-inbox-edit                |  5 ++---
 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/inbox.t                               | 19 +++++++++++++++++
 t/{iso-2202-jp.mbox => iso-2202-jp.eml} |  1 -
 t/mda.t                                 | 10 ++++-----
 t/msg_iter.t                            | 18 ++++++----------
 t/nntpd-tls.t                           |  8 +++----
 t/psgi_v2.t                             | 28 ++++++++++++++++---------
 t/search.t                              | 12 ++++-------
 t/solver_git.t                          |  4 ++--
 t/{utf8.mbox => utf8.eml}               |  1 -
 t/v2reindex.t                           |  3 +--
 t/watch_maildir_v2.t                    |  2 +-
 28 files changed, 105 insertions(+), 100 deletions(-)
 rename t/{iso-2202-jp.mbox => iso-2202-jp.eml} (84%)
 rename t/{utf8.mbox => utf8.eml} (90%)

^ permalink raw reply	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2020-04-18  3:38  7% [PATCH 0/8] some small yak shaving things Eric Wong
2020-04-18  3:38  5% ` [PATCH 5/8] favor `do {}' over `eval {}' for localized slurp Eric Wong

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