use strict; use warnings; use Fcntl qw( F_GETFD F_SETFD FD_CLOEXEC ); use POSIX qw( dup2 ); sub keep_open_on_exec { my ($fh) = @_; my $flags = fcntl($fh, F_GETFD, 0); next if $flags & FD_CLOEXEC == 0; fcntl($fh, F_SETFD, $flags & ~FD_CLOEXEC); } open(my $src1_fh, '-|', echo => 'apples') or die("open: $!"); open(my $src2_fh, '-|', echo => 'oranges') or die("open: $!"); keep_open_on_exec($_) for $src1_fh, $src2_fh; system(diff => '/dev/fd/'.fileno($src1_fh), '/dev/fd/'.fileno($src2_fh)) >= 0 or die("system: $!"); printf("\$?=%04X\n", $?);