Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect the output from one filehandle into another?

I want to set up a pipeline of processes from within Perl (running on Linux), consisting of two parts run at separate times.

Eg:

Start the consumer process:

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

then much later start the producer process:

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

but then somehow redirect the output from gpg into 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 glue the two processes together with redirection.

like image 240
Martin Avatar asked Jan 27 '26 21:01

Martin


1 Answers

Add

pipe( IN, OUT );

Before the two open statements. That's it!

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

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

It lets you start processes, tie their input and outputs together, and add logging or redirection at any point in the chain.

like image 195
rjh Avatar answered Jan 29 '26 12:01

rjh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!