7 skip_all("VMS too picky about line endings for record-oriented pipes")
13 my $Perl = which_perl();
21 (my $data2 = $data) =~ s/\n/\n\n/g;
23 my $t1 = { data => $data, write_c => [1,2,length $data], read_c => [1,2,3,length $data]};
24 my $t2 = { data => $data2, write_c => [1,2,length $data2], read_c => [1,2,3,length $data2]};
26 $_->{write_c} = [1..length($_->{data})],
27 $_->{read_c} = [1..length($_->{data})+1, 0xe000] # Need <0xffff for REx
30 my $c; # len write tests, for each: one _all test, and 3 each len+2
31 $c += @{$_->{write_c}} * (1 + 3*@{$_->{read_c}}) for $t1, $t2;
32 $c *= 3*2*2; # $how_w, file/pipe, 2 reports
34 $c += 6; # Tests with sleep()...
39 $set_out = "binmode STDOUT, ':crlf'"
40 if defined $main::use_crlf && $main::use_crlf == 1;
42 sub testread ($$$$$$$) {
43 my ($fh, $str, $read_c, $how_r, $write_c, $how_w, $why) = @_;
45 if ($how_r eq 'readline_all') {
46 $buf .= $_ while <$fh>;
47 } elsif ($how_r eq 'readline') {
49 $buf .= $_ while <$fh>;
50 } elsif ($how_r eq 'read') {
52 $buf .= $in while $c = read($fh, $in, $read_c);
53 } elsif ($how_r eq 'sysread') {
55 $buf .= $in while $c = sysread($fh, $in, $read_c);
57 die "Unrecognized read: '$how_r'";
59 close $fh or die "close: $!";
60 # The only contamination allowed is with sysread/prints
61 $buf =~ s/\r\n/\n/g if $how_r eq 'sysread' and $how_w =~ /print/;
62 is(length $buf, length $str, "length with wrc=$write_c, rdc=$read_c, $how_w, $how_r, $why");
63 is($buf, $str, "content with wrc=$write_c, rdc=$read_c, $how_w, $how_r, $why");
66 sub testpipe ($$$$$$) {
67 my ($str, $write_c, $read_c, $how_w, $how_r, $why) = @_;
68 (my $quoted = $str) =~ s/\n/\\n/g;;
70 if ($how_w eq 'print') { # AUTOFLUSH???
71 # Should be shell-neutral:
72 open $fh, '-|', qq[$Perl -we "$set_out;print for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!";
73 } elsif ($how_w eq 'print/flush') {
74 # shell-neutral and miniperl-enabled autoflush? qq(\x24\x7c) eq '$|'
75 open $fh, '-|', qq[$Perl -we "$set_out;eval qq(\\x24\\x7c = 1) or die;print for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!";
76 } elsif ($how_w eq 'syswrite') {
77 ### How to protect \$_
78 open $fh, '-|', qq[$Perl -we "$set_out;eval qq(sub w {syswrite STDOUT, \\x24_} 1) or die; w() for grep length, split /(.{1,$write_c})/s, qq($quoted)"] or die "open: $!";
80 die "Unrecognized write: '$how_w'";
83 if defined $main::use_crlf && $main::use_crlf == 1;
84 testread($fh, $str, $read_c, $how_r, $write_c, $how_w, "pipe$why");
87 sub testfile ($$$$$$) {
88 my ($str, $write_c, $read_c, $how_w, $how_r, $why) = @_;
89 my @data = grep length, split /(.{1,$write_c})/s, $str;
91 my $filename = tempfile();
92 open my $fh, '>', $filename or die;
95 if defined $main::use_crlf && $main::use_crlf == 1;
96 if ($how_w eq 'print') { # AUTOFLUSH???
98 print $fh $_ for @data;
99 } elsif ($how_w eq 'print/flush') {
101 print $fh $_ for @data;
102 } elsif ($how_w eq 'syswrite') {
103 syswrite $fh, $_ for @data;
105 die "Unrecognized write: '$how_w'";
107 close $fh or die "close: $!";
108 open $fh, '<', $filename or die;
110 if defined $main::use_crlf && $main::use_crlf == 1;
111 testread($fh, $str, $read_c, $how_r, $write_c, $how_w, "file$why");
114 # shell-neutral and miniperl-enabled autoflush? qq(\x24\x7c) eq '$|'
115 open my $fh, '-|', qq[$Perl -we "eval qq(\\x24\\x7c = 1) or die; binmode STDOUT; sleep 1, print for split //, qq(a\nb\n\nc\n\n\n)"] or die "open: $!";
117 binmode $fh, q(:crlf);
121 push @c, ord $c while $c = getc $fh;
123 is(scalar @c, 9, 'got 9 chars');
124 is("@c", '97 10 98 10 10 99 10 10 10', 'got expected chars');
125 ok(close($fh), 'close');
128 my $t = ($t1, $t2)[$s-1];
129 my $str = $t->{data};
130 my $r = $t->{read_c};
131 my $w = $t->{write_c};
132 for my $read_c (@$r) {
133 for my $write_c (@$w) {
134 for my $how_r (qw(readline_all readline read sysread)) {
135 next if $how_r eq 'readline_all' and $read_c != 1;
136 for my $how_w (qw(print print/flush syswrite)) {
137 testfile($str, $write_c, $read_c, $how_w, $how_r, $s);
138 testpipe($str, $write_c, $read_c, $how_w, $how_r, $s);