user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 4/4] use defined-or in a few more places
Date: Sun, 24 Jan 2021 22:41:58 -0800	[thread overview]
Message-ID: <20210125064158.15179-5-e@80x24.org> (raw)
In-Reply-To: <20210125064158.15179-1-e@80x24.org>

Mainly around fork() calls, but some nearby places as well.
---
 lib/PublicInbox/Spawn.pm      |  7 +++----
 lib/PublicInbox/TestCommon.pm | 10 ++++------
 lib/PublicInbox/Watch.pm      |  2 +-
 t/gcf2.t                      |  2 +-
 t/imap_tracker.t              |  2 +-
 t/run.perl                    |  2 +-
 xt/create-many-inboxes.t      |  3 +--
 7 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index 86f66605..ef4885c1 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -352,8 +352,7 @@ sub which ($) {
 
 sub spawn ($;$$) {
 	my ($cmd, $env, $opts) = @_;
-	my $f = which($cmd->[0]);
-	defined $f or die "$cmd->[0]: command not found\n";
+	my $f = which($cmd->[0]) // die "$cmd->[0]: command not found\n";
 	my @env;
 	$opts ||= {};
 
@@ -365,7 +364,7 @@ sub spawn ($;$$) {
 	for my $child_fd (0..2) {
 		my $parent_fd = $opts->{$child_fd};
 		if (defined($parent_fd) && $parent_fd !~ /\A[0-9]+\z/) {
-			defined(my $fd = fileno($parent_fd)) or
+			my $fd = fileno($parent_fd) //
 					die "$parent_fd not an IO GLOB? $!";
 			$parent_fd = $fd;
 		}
@@ -374,7 +373,7 @@ sub spawn ($;$$) {
 	my $rlim = [];
 
 	foreach my $l (@RLIMITS) {
-		defined(my $v = $opts->{$l}) or next;
+		my $v = $opts->{$l} // next;
 		my $r = eval "require BSD::Resource; BSD::Resource::$l();";
 		unless (defined $r) {
 			warn "$l undefined by BSD::Resource: $@\n";
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 16ae2650..40c2dc9e 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -254,8 +254,7 @@ sub run_script ($;$$) {
 		my $cmd = [ key2script($key), @argv ];
 		my $pid = PublicInbox::Spawn::spawn($cmd, $env, $spawn_opt);
 		if (defined $pid) {
-			my $r = waitpid($pid, 0);
-			defined($r) or die "waitpid: $!";
+			my $r = waitpid($pid, 0) // die "waitpid: $!";
 			$r == $pid or die "waitpid: expected $pid, got $r";
 		}
 	} else { # localize and run everything in the same process:
@@ -367,7 +366,7 @@ sub start_script {
 			}
 		}
 		if (@paths) {
-			defined($tail_pid = fork) or die "fork: $!\n";
+			$tail_pid = fork // die "fork: $!";
 			if ($tail_pid == 0) {
 				# make sure files exist, first
 				open my $fh, '>>', $_ for @paths;
@@ -378,7 +377,7 @@ sub start_script {
 			wait_for_tail($tail_pid, scalar @paths);
 		}
 	}
-	defined(my $pid = fork) or die "fork: $!\n";
+	my $pid = fork // die "fork: $!\n";
 	if ($pid == 0) {
 		eval { PublicInbox::DS->Reset };
 		# pretend to be systemd (cf. sd_listen_fds(3))
@@ -440,8 +439,7 @@ sub join {
 	my ($self, $sig) = @_;
 	my $pid = delete $self->{pid} or return;
 	CORE::kill($sig, $pid) if defined $sig;
-	my $ret = waitpid($pid, 0);
-	defined($ret) or die "waitpid($pid): $!";
+	my $ret = waitpid($pid, 0) // die "waitpid($pid): $!";
 	$ret == $pid or die "waitpid($pid) != $ret";
 }
 
diff --git a/lib/PublicInbox/Watch.pm b/lib/PublicInbox/Watch.pm
index 1de5018d..2b44ba43 100644
--- a/lib/PublicInbox/Watch.pm
+++ b/lib/PublicInbox/Watch.pm
@@ -626,7 +626,7 @@ sub imap_idle_fork ($$) {
 	my ($url, $intvl) = @$url_intvl;
 	pipe(my ($r, $w)) or die "pipe: $!";
 	my $seed = rand(0xffffffff);
-	defined(my $pid = fork) or die "fork: $!";
+	my $pid = fork // die "fork: $!";
 	if ($pid == 0) {
 		srand($seed);
 		eval { Net::SSLeay::randomize() };
diff --git a/t/gcf2.t b/t/gcf2.t
index fa907c8b..d12a4420 100644
--- a/t/gcf2.t
+++ b/t/gcf2.t
@@ -114,7 +114,7 @@ SKIP: {
 		$w->blocking($blk);
 		seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!";
 		truncate($fh, 0) or BAIL_OUT "truncate: $!";
-		defined(my $pid = fork) or BAIL_OUT "fork: $!";
+		my $pid = fork // BAIL_OUT "fork: $!";
 		if ($pid == 0) {
 			close $w;
 			tick; # wait for parent to block on writev
diff --git a/t/imap_tracker.t b/t/imap_tracker.t
index be7c6e65..90dea99f 100644
--- a/t/imap_tracker.t
+++ b/t/imap_tracker.t
@@ -29,7 +29,7 @@ SKIP: {
 	diag "TEST_STRESS_NPROC=$nproc TEST_STRESS_NR=$nr";
 	require POSIX;
 	for my $n (1..$nproc) {
-		defined(my $pid = fork) or BAIL_OUT "fork: $!";
+		my $pid = fork // BAIL_OUT "fork: $!";
 		if ($pid == 0) {
 			my $url = "imap://example.com/INBOX.$$";
 			my $uidval = time;
diff --git a/t/run.perl b/t/run.perl
index b7cb988b..96db3045 100755
--- a/t/run.perl
+++ b/t/run.perl
@@ -128,7 +128,7 @@ my $eof; # we stop respawning if true
 
 my $start_worker = sub {
 	my ($i, $j, $rd, $todo) = @_;
-	defined(my $pid = fork) or DIE "fork: $!";
+	my $pid = fork // DIE "fork: $!";
 	if ($pid == 0) {
 		$worker = $$;
 		while (1) {
diff --git a/xt/create-many-inboxes.t b/xt/create-many-inboxes.t
index 0c2de40d..f44334cc 100644
--- a/xt/create-many-inboxes.t
+++ b/xt/create-many-inboxes.t
@@ -68,7 +68,7 @@ my @children;
 for my $i (1..$nproc) {
 	my ($r, $w);
 	pipe($r, $w) or BAIL_OUT $!;
-	my $pid = fork;
+	my $pid = fork // BAIL_OUT "fork: $!";
 	if ($pid == 0) {
 		close $w;
 		while (my $i = <$r>) {
@@ -77,7 +77,6 @@ for my $i (1..$nproc) {
 		}
 		_exit(0);
 	}
-	defined $pid or BAIL_OUT "fork: $!";
 	close $r or BAIL_OUT $!;
 	push @children, [ $w, $pid ];
 	$w->autoflush(1);

      parent reply	other threads:[~2021-01-25  6:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-25  6:41 [PATCH 0/4] miscidx: lazy transactions to fix tests Eric Wong
2021-01-25  6:41 ` [PATCH 1/4] lei: use Time::HiRes stat for nanosecond resolution Eric Wong
2021-01-25  6:41 ` [PATCH 2/4] miscidx: switch to lazy transactions Eric Wong
2021-01-25  6:41 ` [PATCH 3/4] spawn: split() on regexp, not a literal string Eric Wong
2021-01-25  6:41 ` Eric Wong [this message]

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: https://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=20210125064158.15179-5-e@80x24.org \
    --to=e@80x24.org \
    --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).