diff options
author | Eric Wong <e@80x24.org> | 2023-09-27 06:02:48 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2023-09-28 02:34:57 +0000 |
commit | 10ca39f5bddcb414dac2a3fcee4cc53844c74fc1 (patch) | |
tree | feb7b4dc50ffd3e5c1bb6da13a10cf97bc2e7ba9 /lib/PublicInbox/Spawn.pm | |
parent | 3cea12e29643a02bc9d33802896d3dd12c8bc8fa (diff) | |
download | public-inbox-10ca39f5bddcb414dac2a3fcee4cc53844c74fc1.tar.gz |
This makes interesting parts of our code easier to read IMHO. We can take advantage of `local' while avoiding `fileno' calls since it's called in spawn() anyways to reduce LoC even further.
Diffstat (limited to 'lib/PublicInbox/Spawn.pm')
-rw-r--r-- | lib/PublicInbox/Spawn.pm | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm index 38619fc2..75ef0137 100644 --- a/lib/PublicInbox/Spawn.pm +++ b/lib/PublicInbox/Spawn.pm @@ -17,12 +17,11 @@ package PublicInbox::Spawn; use v5.12; use parent qw(Exporter); -use Symbol qw(gensym); use Fcntl qw(LOCK_EX SEEK_SET); use IO::Handle (); use Carp qw(croak); use PublicInbox::ProcessPipe; -our @EXPORT_OK = qw(which spawn popen_rd run_die run_wait); +our @EXPORT_OK = qw(which spawn popen_rd popen_wr run_die run_wait); our @RLIMITS = qw(RLIMIT_CPU RLIMIT_CORE RLIMIT_DATA); BEGIN { @@ -332,7 +331,6 @@ sub spawn ($;$$) { my ($cmd, $env, $opts) = @_; my $f = which($cmd->[0]) // die "$cmd->[0]: command not found\n"; my @env; - $opts ||= {}; my %env = (%ENV, $env ? %$env : ()); while (my ($k, $v) = each %env) { push @env, "$k=$v" if defined($v); @@ -366,14 +364,14 @@ sub spawn ($;$$) { sub popen_rd { my ($cmd, $env, $opt) = @_; - pipe(my ($r, $w)) or die "pipe: $!\n"; - $opt ||= {}; - $opt->{1} = fileno($w); - my $pid = spawn($cmd, $env, $opt); - return ($r, $pid) if wantarray; - my $s = gensym; - tie *$s, 'PublicInbox::ProcessPipe', $pid, $r, @{$opt->{cb_arg} // []}; - $s; + pipe(my $r, local $opt->{1}) or die "pipe: $!\n"; + PublicInbox::ProcessPipe->maybe_new(spawn($cmd, $env, $opt), $r, $opt) +} + +sub popen_wr { + my ($cmd, $env, $opt) = @_; + pipe(local $opt->{0}, my $w) or die "pipe: $!\n"; + PublicInbox::ProcessPipe->maybe_new(spawn($cmd, $env, $opt), $w, $opt) } sub run_wait ($;$$) { |