Demi Demi - 1 month ago 23
Perl Question

Trouble with IPC::Open3

I am writing a simple script using IPC::Open3. The script produces no output to either stdout or stderr, while I would expect output to both.

The complete source code:

#!/usr/bin/env perl
#===============================================================================
#
# FILE: open3.pl
#
# USAGE: ./open3.pl
#
# DESCRIPTION: Test of IPC::Open3
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Demi Marie Obenour
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 09/25/2016 11:22:26 AM
# REVISION: ---
#===============================================================================

use strict;
use warnings;
use utf8;

use IPC::Open3;

pipe my $input, my $output or die $!;
my $pid = open3(\*STDIN, $output, \*STDERR, 'dd', 'if=/usr/include/unistd.h') or die $!;

while(<$input>) {
print $_."\n";
}
waitpid $pid, 0;


I am fairly certain that I am using IPC::Open3 incorrectly. However, I am still confused as to what I should be doing.

Answer

It's the pipe. Without knowing why it's there I can't say more. This works fine.

my $reader;
my $pid =  open3(\*STDIN, $reader, \*STDERR, 'dd', 'if=/usr/include/unistd.h') or die $!;

while(<$reader>) {
    print $_."\n";
}
waitpid $pid, 0;

I realize it's probably just an example, but in case it's not... this is complete overkill for what you're doing. You can accomplish that with backticks.

print `dd if=/usr/include/unistd.h`

IPC::Open3 is a bit overcomplicated. There are better modules such as IPC::Run and IPC::Run3.

use strict;
use warnings;

use IPC::Run3;

run3(['perl', '-e', 'warn "Warning!"; print "Normal\n";'],
     \*STDIN, \*STDOUT, \*STDERR
);