How can I redirect output from one file descriptor to another?

I want to configure a process pipeline from Perl (runs on Linux), consisting of two parts running at different times.

For example:

Run the consumer process:

open( OUT, "| tar xvf - " ) || die "Failed: tar: $!";

then start the renewal process much later:

open( IN, "gpg -d $file |" ) || die "Failed: gpg: $!";

but then somehow redirect the output from gpg to the input to tar.

I can do this by building a loop:

while (<IN> ) {
  print OUT;
}

But I would like to know if I can somehow stick the two processes along with the redirect.

+3
source share
3 answers

Add

pipe( IN, OUT );

Before two open operations. What is it!

If you want to do something more complex, I would recommend the IPC :: Run CPAN module:

http://search.cpan.org/dist/IPC-Run/

, .

+11

, FIFO.

use POSIX qw(mkfifo);
mkfifo($path, 0700) or die "mkfifo $path failed: $!";

FIFO $path. , .

+1

Proc:: SafeExec, . :

use strict;
use warnings;

use Proc::SafeExec;

open(my $ls, "-|", "ls", "-l") or die "Err: $!";
open(my $fh, ">", "tmp.txt") or die "Err: $!";

my $p = Proc::SafeExec->new({
  exec => [qw(sed -e s/a/b/)],
  stdin => $ls,
  stdout => $fh,
});
$p->wait();

IPC:: Run, ... IPC:: Run :

use IPC::Run qw(run);

run [qw(ls -l)], "|", [qw(sed -e s/a/b/)], ">", "tmp.txt";
+1

Source: https://habr.com/ru/post/1704334/


All Articles