Question

I have a file handle that contains compressed data in $my_fd. I would like to start a decompression program (such as "lzop -dc") with $my_fd redirected as standard input, so I can read the decompressed output in $out_fd. As I use STDIN elsewhere this code does not work (but it shows the idea):

# Save STDIN file handle
open(my $stdin_copy, "<&", "STDIN");                                                                                      
my $fd = $my_fd;                                                                                                          
# Replace STDIN with the file handle
open(STDIN, "<&", $fd);
# Start decompression with the fake STDIN
open($out_fd, "-|", $opt::decompress_program);                                                                             
# Put STDIN file handle back
open(STDIN, "<&", $stdin_copy);
# Do stuff on the decompressed data
while(<$out_fd>) { ... }
# Do more stuff on the original STDIN

Both input ($fd) and output ($out_fd) can be bigger than physical memory, so it is not an option to read it all in.

Background

This is to be used for --compress in GNU Parallel.

Was it helpful?

Solution

There's no need to clobber your STDIN. Use IPC::Open2 or IPC::Run to use an external program with arbitrary input/output streams.

use IPC::Open2;

# use $fd as input and $out_fd as output to external program
$pid = open2($fd, $out_fd, $opt::decompress_program);
close $fd;
while (<$out_fd>) {
   ...
}

(Use IPC::Open3 if you're interested in the standard error stream from the external program)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top