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