1 #!/usr/local/bin/perl -w
7 use POSIX qw( :fcntl_h ) ;
12 # in case SEEK_SET isn't defined in older perls. it seems to always be 0
15 *SEEK_SET = sub() { 0 } unless defined \&SEEK_SET ;
25 plan( tests => scalar @pipe_data ) ;
29 #test_fork_pipe_slurp() ;
33 eval { test_socketpair_slurp() } ;
35 skip "socketpair not found in this Perl", scalar( @pipe_data ) if $@ ;
38 sub test_socketpair_slurp {
40 foreach my $data ( @pipe_data ) {
42 my $size = length( $data ) ;
44 my $read_fh = gensym ;
45 my $write_fh = gensym ;
47 socketpair( $read_fh, $write_fh,
48 AF_UNIX, SOCK_STREAM, PF_UNSPEC);
52 #warn "PARENT SOCKET\n" ;
54 my $read_buf = read_file( $read_fh ) ;
57 "socket slurp/spew of $size bytes" ) ;
63 #warn "CHILD SOCKET\n" ;
65 eval { write_file( $write_fh, $data ) } ;
73 my $data_seek = tell( \*DATA );
75 # first slurp in the lines
76 my @slurp_lines = read_file( \*DATA ) ;
78 # now seek back and read all the lines with the <> op and we make
81 seek( \*DATA, $data_seek, SEEK_SET ) || die "seek $!" ;
82 my @data_lines = <DATA> ;
83 my $data_text = join( '', @data_lines ) ;
85 # now slurp in as one string and test
87 sysseek( \*DATA, $data_seek, SEEK_SET ) || die "seek $!" ;
88 my $slurp_text = read_file( \*DATA ) ;
89 is( $slurp_text, $data_text, 'scalar slurp DATA' ) ;
91 # test the array slurp
93 ok( eq_array( \@data_lines, \@slurp_lines ), 'list slurp of DATA' ) ;
96 sub test_fork_pipe_slurp {
98 foreach my $data ( @pipe_data ) {
100 test_to_pipe( $data ) ;
101 test_from_pipe( $data ) ;
110 my $size = length( $data ) ;
112 if ( pipe_from_fork( \*READ_FH ) ) {
115 my $read_buf = read_file( \*READ_FH ) ;
116 warn "PARENT read\n" ;
118 is( $read_buf, $data, "pipe slurp/spew of $size bytes" ) ;
125 warn "CHILD write\n" ;
126 # write_file( \*STDOUT, $data ) ;
127 syswrite( \*STDOUT, $data, length( $data ) ) ;
137 my ( $parent_fh ) = @_ ;
141 pipe( $parent_fh, $child ) or die;
144 die "fork() failed: $!" unless defined $pid;
153 warn "CHILD FILENO ", fileno($child), "\n" ;
155 open(STDOUT, ">&=" . fileno($child)) or die "no fileno" ;
165 my $size = length( $data ) ;
167 if ( pipe_to_fork( \*WRITE_FH ) ) {
170 syswrite( \*WRITE_FH, $data, length( $data ) ) ;
171 # write_file( \*WRITE_FH, $data ) ;
172 warn "PARENT write\n" ;
174 # is( $read_buf, $data, "pipe slurp/spew of $size bytes" ) ;
181 warn "CHILD read FILENO ", fileno(\*STDIN), "\n" ;
183 my $read_buf = read_file( \*STDIN ) ;
184 is( $read_buf, $data, "pipe slurp/spew of $size bytes" ) ;
191 my ( $parent_fh ) = @_ ;
195 pipe( $child, $parent_fh ) or die ;
198 die "fork() failed: $!" unless defined $pid;
206 open(STDIN, "<&=" . fileno($child)) or die;
219 we don't test long handle slurps from DATA since i would have to type