1#!./perl -w 2 3BEGIN { 4 require Config; import Config; 5 if (!$Config{'d_fork'} 6 # open2/3 supported on win32 (but not Borland due to CRT bugs) 7 && (($^O ne 'MSWin32' && $^O ne 'NetWare') || $Config{'cc'} =~ /^bcc/i)) 8 { 9 print "1..0\n"; 10 exit 0; 11 } 12 # make warnings fatal 13 $SIG{__WARN__} = sub { die @_ }; 14} 15 16use strict; 17use IO::Handle; 18use IPC::Open3; 19#require 'open3.pl'; use subs 'open3'; 20 21my $perl = $^X; 22 23sub ok { 24 my ($n, $result, $info) = @_; 25 if ($result) { 26 print "ok $n\n"; 27 } 28 else { 29 print "not ok $n\n"; 30 print "# $info\n" if $info; 31 } 32} 33 34sub cmd_line { 35 if ($^O eq 'MSWin32' || $^O eq 'NetWare') { 36 my $cmd = shift; 37 $cmd =~ tr/\r\n//d; 38 $cmd =~ s/"/\\"/g; 39 return qq/"$cmd"/; 40 } 41 else { 42 return $_[0]; 43 } 44} 45 46my ($pid, $reaped_pid); 47STDOUT->autoflush; 48STDERR->autoflush; 49 50print "1..22\n"; 51 52# basic 53ok 1, $pid = open3 'WRITE', 'READ', 'ERROR', $perl, '-e', cmd_line(<<'EOF'); 54 $| = 1; 55 print scalar <STDIN>; 56 print STDERR "hi error\n"; 57EOF 58ok 2, print WRITE "hi kid\n"; 59ok 3, <READ> =~ /^hi kid\r?\n$/; 60ok 4, <ERROR> =~ /^hi error\r?\n$/; 61ok 5, close(WRITE), $!; 62ok 6, close(READ), $!; 63ok 7, close(ERROR), $!; 64$reaped_pid = waitpid $pid, 0; 65ok 8, $reaped_pid == $pid, $reaped_pid; 66ok 9, $? == 0, $?; 67 68# read and error together, both named 69$pid = open3 'WRITE', 'READ', 'READ', $perl, '-e', cmd_line(<<'EOF'); 70 $| = 1; 71 print scalar <STDIN>; 72 print STDERR scalar <STDIN>; 73EOF 74print WRITE "ok 10\n"; 75print scalar <READ>; 76print WRITE "ok 11\n"; 77print scalar <READ>; 78waitpid $pid, 0; 79 80# read and error together, error empty 81$pid = open3 'WRITE', 'READ', '', $perl, '-e', cmd_line(<<'EOF'); 82 $| = 1; 83 print scalar <STDIN>; 84 print STDERR scalar <STDIN>; 85EOF 86print WRITE "ok 12\n"; 87print scalar <READ>; 88print WRITE "ok 13\n"; 89print scalar <READ>; 90waitpid $pid, 0; 91 92# dup writer 93ok 14, pipe PIPE_READ, PIPE_WRITE; 94$pid = open3 '<&PIPE_READ', 'READ', '', 95 $perl, '-e', cmd_line('print scalar <STDIN>'); 96close PIPE_READ; 97print PIPE_WRITE "ok 15\n"; 98close PIPE_WRITE; 99print scalar <READ>; 100waitpid $pid, 0; 101 102# dup reader 103$pid = open3 'WRITE', '>&STDOUT', 'ERROR', 104 $perl, '-e', cmd_line('print scalar <STDIN>'); 105print WRITE "ok 16\n"; 106waitpid $pid, 0; 107 108# dup error: This particular case, duping stderr onto the existing 109# stdout but putting stdout somewhere else, is a good case because it 110# used not to work. 111$pid = open3 'WRITE', 'READ', '>&STDOUT', 112 $perl, '-e', cmd_line('print STDERR scalar <STDIN>'); 113print WRITE "ok 17\n"; 114waitpid $pid, 0; 115 116# dup reader and error together, both named 117$pid = open3 'WRITE', '>&STDOUT', '>&STDOUT', $perl, '-e', cmd_line(<<'EOF'); 118 $| = 1; 119 print STDOUT scalar <STDIN>; 120 print STDERR scalar <STDIN>; 121EOF 122print WRITE "ok 18\n"; 123print WRITE "ok 19\n"; 124waitpid $pid, 0; 125 126# dup reader and error together, error empty 127$pid = open3 'WRITE', '>&STDOUT', '', $perl, '-e', cmd_line(<<'EOF'); 128 $| = 1; 129 print STDOUT scalar <STDIN>; 130 print STDERR scalar <STDIN>; 131EOF 132print WRITE "ok 20\n"; 133print WRITE "ok 21\n"; 134waitpid $pid, 0; 135 136# command line in single parameter variant of open3 137# for understanding of Config{'sh'} test see exec description in camel book 138my $cmd = 'print(scalar(<STDIN>))'; 139$cmd = $Config{'sh'} =~ /sh/ ? "'$cmd'" : cmd_line($cmd); 140eval{$pid = open3 'WRITE', '>&STDOUT', 'ERROR', "$perl -e " . $cmd; }; 141if ($@) { 142 print "error $@\n"; 143 print "not ok 22\n"; 144} 145else { 146 print WRITE "ok 22\n"; 147 waitpid $pid, 0; 148} 149