/usr/share/doc/libfile-slurp-perl/examples/handle.t is in libfile-slurp-perl 9999.19-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | #!/usr/local/bin/perl -w
use strict ;
use File::Slurp ;
use Carp ;
use POSIX qw( :fcntl_h ) ;
use Socket ;
use Symbol ;
use Test::More ;
# in case SEEK_SET isn't defined in older perls. it seems to always be 0
BEGIN {
*SEEK_SET = sub() { 0 } unless defined \&SEEK_SET ;
}
my @pipe_data = (
'',
'abc',
'abc' x 100_000,
'abc' x 1_000_000,
) ;
plan( tests => scalar @pipe_data ) ;
#test_data_slurp() ;
#test_fork_pipe_slurp() ;
SKIP: {
eval { test_socketpair_slurp() } ;
skip "socketpair not found in this Perl", scalar( @pipe_data ) if $@ ;
}
sub test_socketpair_slurp {
foreach my $data ( @pipe_data ) {
my $size = length( $data ) ;
my $read_fh = gensym ;
my $write_fh = gensym ;
socketpair( $read_fh, $write_fh,
AF_UNIX, SOCK_STREAM, PF_UNSPEC);
if ( fork() ) {
#warn "PARENT SOCKET\n" ;
close( $write_fh ) ;
my $read_buf = read_file( $read_fh ) ;
is( $read_buf, $data,
"socket slurp/spew of $size bytes" ) ;
}
else {
#child
#warn "CHILD SOCKET\n" ;
close( $read_fh ) ;
eval { write_file( $write_fh, $data ) } ;
exit() ;
}
}
}
sub test_data_slurp {
my $data_seek = tell( \*DATA );
# first slurp in the lines
my @slurp_lines = read_file( \*DATA ) ;
# now seek back and read all the lines with the <> op and we make
# golden data sets
seek( \*DATA, $data_seek, SEEK_SET ) || die "seek $!" ;
my @data_lines = <DATA> ;
my $data_text = join( '', @data_lines ) ;
# now slurp in as one string and test
sysseek( \*DATA, $data_seek, SEEK_SET ) || die "seek $!" ;
my $slurp_text = read_file( \*DATA ) ;
is( $slurp_text, $data_text, 'scalar slurp DATA' ) ;
# test the array slurp
ok( eq_array( \@data_lines, \@slurp_lines ), 'list slurp of DATA' ) ;
}
sub test_fork_pipe_slurp {
foreach my $data ( @pipe_data ) {
test_to_pipe( $data ) ;
test_from_pipe( $data ) ;
}
}
sub test_from_pipe {
my( $data ) = @_ ;
my $size = length( $data ) ;
if ( pipe_from_fork( \*READ_FH ) ) {
# parent
my $read_buf = read_file( \*READ_FH ) ;
warn "PARENT read\n" ;
is( $read_buf, $data, "pipe slurp/spew of $size bytes" ) ;
close \*READ_FH ;
# return ;
}
else {
# child
warn "CHILD write\n" ;
# write_file( \*STDOUT, $data ) ;
syswrite( \*STDOUT, $data, length( $data ) ) ;
close \*STDOUT;
exit(0);
}
}
sub pipe_from_fork {
my ( $parent_fh ) = @_ ;
my $child = gensym ;
pipe( $parent_fh, $child ) or die;
my $pid = fork();
die "fork() failed: $!" unless defined $pid;
if ($pid) {
warn "PARENT\n" ;
close $child;
return $pid ;
}
warn "CHILD FILENO ", fileno($child), "\n" ;
close $parent_fh ;
open(STDOUT, ">&=" . fileno($child)) or die "no fileno" ;
return ;
}
sub test_to_pipe {
my( $data ) = @_ ;
my $size = length( $data ) ;
if ( pipe_to_fork( \*WRITE_FH ) ) {
# parent
syswrite( \*WRITE_FH, $data, length( $data ) ) ;
# write_file( \*WRITE_FH, $data ) ;
warn "PARENT write\n" ;
# is( $read_buf, $data, "pipe slurp/spew of $size bytes" ) ;
close \*WRITE_FH ;
# return ;
}
else {
# child
warn "CHILD read FILENO ", fileno(\*STDIN), "\n" ;
my $read_buf = read_file( \*STDIN ) ;
is( $read_buf, $data, "pipe slurp/spew of $size bytes" ) ;
close \*STDIN;
exit(0);
}
}
sub pipe_to_fork {
my ( $parent_fh ) = @_ ;
my $child = gensym ;
pipe( $child, $parent_fh ) or die ;
my $pid = fork();
die "fork() failed: $!" unless defined $pid;
if ( $pid ) {
close $child;
return $pid ;
}
close $parent_fh ;
open(STDIN, "<&=" . fileno($child)) or die;
return ;
}
__DATA__
line one
second line
more lines
still more
enough lines
we don't test long handle slurps from DATA since i would have to type
too much stuff :-)
so we will stop here
|