]> Sergey Matveev's repositories - public-inbox.git/commitdiff
favor `do {}' over `eval {}' for localized slurp
authorEric Wong <e@yhbt.net>
Sat, 18 Apr 2020 03:38:50 +0000 (03:38 +0000)
committerEric Wong <e@yhbt.net>
Sun, 19 Apr 2020 08:51:24 +0000 (08:51 +0000)
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
script/public-inbox-mda
script/public-inbox-purge
scripts/import_maildir
scripts/import_slrnspool
scripts/slrnspool2maildir
scripts/ssoma-replay
t/v2reindex.t
t/watch_maildir_v2.t

index 0d6c989b8ee00be88449d128f2e890239154d034..4c10b68b994506bd4949e197028bd7caa2d8a7f1 100644 (file)
@@ -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 ($$$$) {
index f37c74925f05383273535f58d6394443bd76af8b..54d0af01414a64c9458a7cf9d3005143bc5f9616 100755 (executable)
@@ -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);
index c9b69c3d938955065e0ebdcc435d913d54548153..8301b06d359aaa9ddf162dfa656ccda0871e1925 100755 (executable)
@@ -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;
 
index fbf3f6495d1a54663aff2c9e8fef4762d4fb097a..f4e8254348ef64f7d258be4473a54ba1d6c54746 100755 (executable)
@@ -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;
index e569d0044ad993d4866b149c07907640ade8afeb..480e7b4fa5ade57aa42d36de01e9861a964e75cd 100755 (executable)
@@ -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);
 
index 0c21806ad5f2aec675f48a7fcce6f38893be654f..8e444e84d57a2a3db6739b87dec2adde5e679f7e 100755 (executable)
@@ -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
index 3e928084bb90dcba67712bc3d8129224651ad7da..071214230ac42a9b1bb1ce7e79e54d986d898892 100755 (executable)
@@ -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
index 7c14117af0514bb1cccff3c3515d17990c776d0f..b6164ff810febd2936236e7ac2bd42f0d2e3bdc8 100644 (file)
@@ -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 => [
index b2514c16ad4827bba720532070e9ee8fdc721b1e..685cd6edc2644c50e276fd46ed2714d3f3b01575 100644 (file)
@@ -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');