5 use Benchmark qw( timethese cmpthese ) ;
8 use Fcntl qw( :DEFAULT :seek );
12 my $dur = shift || -2 ;
14 my $file = 'slurp_data' ;
16 my @lines = ( 'abc' x 30 . "\n") x 100 ;
17 my $text = join( '', @lines ) ;
19 bench_list_spew( 'SHORT' ) ;
20 bench_scalar_spew( 'SHORT' ) ;
22 File::Slurp::write_file( $file, $text ) ;
24 bench_scalar_slurp( 'SHORT' ) ;
25 bench_list_slurp( 'SHORT' ) ;
27 @lines = ( 'abc' x 40 . "\n") x 1000 ;
28 $text = join( '', @lines ) ;
30 bench_list_spew( 'LONG' ) ;
31 bench_scalar_spew( 'LONG' ) ;
33 File::Slurp::write_file( $file, $text ) ;
35 bench_scalar_slurp( 'LONG' ) ;
36 bench_list_slurp( 'LONG' ) ;
44 print "\n\nList Spew of $size file\n\n" ;
46 my $result = timethese( $dur, {
49 sub { File::Slurp::write_file( $file, @lines ) },
52 sub { print_file( $file, @lines ) },
55 sub { print_join_file( $file, @lines ) },
58 sub { syswrite_file( $file, @lines ) },
61 sub { cpan_write_file( $file, @lines ) },
68 sub bench_scalar_spew {
72 print "\n\nScalar Spew of $size file\n\n" ;
74 my $result = timethese( $dur, {
77 sub { File::Slurp::write_file( $file, $text ) },
80 sub { File::Slurp::write_file( $file, \$text ) },
83 sub { print_file( $file, $text ) },
86 sub { print_join_file( $file, $text ) },
89 sub { syswrite_file( $file, $text ) },
92 sub { syswrite_file2( $file, $text ) },
95 sub { cpan_write_file( $file, $text ) },
102 sub bench_scalar_slurp {
106 print "\n\nScalar Slurp of $size file\n\n" ;
110 my $result = timethese( $dur, {
113 sub { my $text = File::Slurp::read_file( $file ) },
117 File::Slurp::read_file( $file, buf_ref => \$text ) },
120 File::Slurp::read_file( $file, buf_ref => \$buffer ) },
123 File::Slurp::read_file( $file, scalar_ref => 1 ) },
126 sub { my $text = read_file( $file ) },
129 sub { my $text = sysread_file( $file ) },
132 sub { my $text = cpan_read_file( $file ) },
135 sub { my $text = cpan_slurp_to_scalar( $file ) },
138 sub { my $text = file_contents( $file ) },
140 file_contents_no_OO =>
141 sub { my $text = file_contents_no_OO( $file ) },
144 cmpthese( $result ) ;
147 sub bench_list_slurp {
151 print "\n\nList Slurp of $size file\n\n" ;
153 my $result = timethese( $dur, {
156 sub { my @lines = File::Slurp::read_file( $file ) },
159 sub { my $lines_ref =
160 File::Slurp::read_file( $file, array_ref => 1 ) },
163 sub { my $lines_ref =
164 [ File::Slurp::read_file( $file ) ] },
167 sub { my @lines = read_file( $file ) },
170 sub { my @lines = sysread_file( $file ) },
173 sub { my @lines = cpan_read_file( $file ) },
175 cpan_slurp_to_array =>
176 sub { my @lines = cpan_slurp_to_array( $file ) },
178 cpan_slurp_to_array_ref =>
179 sub { my $lines_ref = cpan_slurp_to_array( $file ) },
182 cmpthese( $result ) ;
185 ######################################
186 # uri's old fast slurp
190 my( $file_name ) = shift ;
193 open( FH, $file_name ) || carp "can't open $file_name $!" ;
195 return <FH> if wantarray ;
199 read( FH, $buf, -s FH ) ;
205 my( $file_name ) = shift ;
208 open( FH, $file_name ) || carp "can't open $file_name $!" ;
210 return <FH> if wantarray ;
214 sysread( FH, $buf, -s FH ) ;
218 ######################################
219 # from File::Slurp.pm on cpan
225 local($/) = wantarray ? $/ : undef;
230 open(F, "<$file") || croak "open $file: $!";
232 close(F) || croak "close $file: $!";
234 return $r[0] unless wantarray;
244 open(F, ">$f") || croak "open >$f: $!";
245 (print F @data) || croak "write $f: $!";
246 close(F) || croak "close $f: $!";
251 ######################################
252 # from Slurp.pm on cpan
255 local( $/, @ARGV ) = ( wantarray ? $/ : undef, @_ );
259 sub cpan_slurp_to_array {
260 my @array = slurp( @_ );
261 return wantarray ? @array : \@array;
264 sub cpan_slurp_to_scalar {
265 my $scalar = slurp( @_ );
269 ######################################
270 # very slow slurp code used by a client
274 my $fh = new FileHandle $file or
275 warn("Util::file_contents:Can't open file $file"), return '';
276 return join '', <$fh>;
279 # same code but doesn't use FileHandle.pm
281 sub file_contents_no_OO {
285 open( FH, $file ) || carp "can't open $file $!" ;
287 return join '', <FH>;
290 ##########################
294 my( $file_name ) = shift ;
298 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;
305 my( $file_name ) = shift ;
309 my $mode = ( -e $file_name ) ? '<' : '>' ;
311 open( FH, "+$mode$file_name" ) || carp "can't create $file_name $!" ;
316 sub print_join_file {
318 my( $file_name ) = shift ;
322 my $mode = ( -e $file_name ) ? '<' : '>' ;
324 open( FH, "+$mode$file_name" ) || carp "can't create $file_name $!" ;
326 print FH join( '', @_ ) ;
332 my( $file_name ) = shift ;
336 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;
338 syswrite( FH, join( '', @_ ) ) ;
343 my( $file_name ) = shift ;
347 sysopen( FH, $file_name, O_WRONLY | O_CREAT ) ||
348 carp "can't create $file_name $!" ;
350 syswrite( FH, join( '', @_ ) ) ;