updated docs to cover new binmode behavior
[urisagit/Perl-Docs.git] / lib / File / Slurp.pm
CommitLineData
635c7876 1package File::Slurp;
2
e2c51d31 3my $printed ;
4
635c7876 5use strict;
6
7use Carp ;
635c7876 8use Fcntl qw( :DEFAULT ) ;
e2c51d31 9use POSIX qw( :fcntl_h ) ;
635c7876 10use Symbol ;
11
e2c51d31 12use base 'Exporter' ;
13use vars qw( %EXPORT_TAGS @EXPORT_OK $VERSION @EXPORT ) ;
14
15%EXPORT_TAGS = ( 'all' => [
16 qw( read_file write_file overwrite_file append_file read_dir ) ] ) ;
17
18@EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
19@EXPORT_OK = qw( slurp ) ;
20
8ed110f9 21$VERSION = '9999.14';
e2c51d31 22
9aab46ab 23our $max_fast_slurp_size = 1024 * 100 ;
24
635c7876 25my $is_win32 = $^O =~ /win32/i ;
26
27# Install subs for various constants that aren't set in older perls
28# (< 5.005). Fcntl on old perls uses Exporter to define subs without a
29# () prototype These can't be overridden with the constant pragma or
30# we get a prototype mismatch. Hence this less than aesthetically
31# appealing BEGIN block:
32
33BEGIN {
8ed110f9 34 unless( defined &SEEK_SET ) {
635c7876 35 *SEEK_SET = sub { 0 };
36 *SEEK_CUR = sub { 1 };
37 *SEEK_END = sub { 2 };
38 }
39
8ed110f9 40 unless( defined &O_BINARY ) {
635c7876 41 *O_BINARY = sub { 0 };
42 *O_RDONLY = sub { 0 };
43 *O_WRONLY = sub { 1 };
44 }
45
8ed110f9 46 unless ( defined O_APPEND ) {
635c7876 47
48 if ( $^O =~ /olaris/ ) {
49 *O_APPEND = sub { 8 };
50 *O_CREAT = sub { 256 };
51 *O_EXCL = sub { 1024 };
52 }
53 elsif ( $^O =~ /inux/ ) {
54 *O_APPEND = sub { 1024 };
55 *O_CREAT = sub { 64 };
56 *O_EXCL = sub { 128 };
57 }
58 elsif ( $^O =~ /BSD/i ) {
59 *O_APPEND = sub { 8 };
60 *O_CREAT = sub { 512 };
61 *O_EXCL = sub { 2048 };
62 }
63 }
64}
65
66# print "OS [$^O]\n" ;
67
68# print "O_BINARY = ", O_BINARY(), "\n" ;
69# print "O_RDONLY = ", O_RDONLY(), "\n" ;
70# print "O_WRONLY = ", O_WRONLY(), "\n" ;
71# print "O_APPEND = ", O_APPEND(), "\n" ;
72# print "O_CREAT ", O_CREAT(), "\n" ;
73# print "O_EXCL ", O_EXCL(), "\n" ;
74
635c7876 75
76*slurp = \&read_file ;
77
78sub read_file {
79
80 my( $file_name, %args ) = @_ ;
81
8ed110f9 82 if ( !ref $file_name && 0 &&
9aab46ab 83 -e $file_name && -s _ < $max_fast_slurp_size && ! %args && !wantarray ) {
e2c51d31 84
85 local( *FH ) ;
86
e2c51d31 87 unless( open( FH, $file_name ) ) {
88
89 @_ = ( \%args, "read_file '$file_name' - sysopen: $!");
90 goto &_error ;
91 }
92
e2c51d31 93 my $read_cnt = sysread( FH, my $buf, -s _ ) ;
94
95 unless ( defined $read_cnt ) {
96
97# handle the read error
98
8ed110f9 99 @_ = ( \%args,
100 "read_file '$file_name' - small sysread: $!");
e2c51d31 101 goto &_error ;
102 }
103
104 return $buf ;
105 }
106
635c7876 107# set the buffer to either the passed in one or ours and init it to the null
108# string
109
110 my $buf ;
111 my $buf_ref = $args{'buf_ref'} || \$buf ;
112 ${$buf_ref} = '' ;
113
114 my( $read_fh, $size_left, $blk_size ) ;
115
116# check if we are reading from a handle (glob ref or IO:: object)
117
118 if ( ref $file_name ) {
119
120# slurping a handle so use it and don't open anything.
121# set the block size so we know it is a handle and read that amount
122
123 $read_fh = $file_name ;
124 $blk_size = $args{'blk_size'} || 1024 * 1024 ;
125 $size_left = $blk_size ;
126
127# DEEP DARK MAGIC. this checks the UNTAINT IO flag of a
128# glob/handle. only the DATA handle is untainted (since it is from
129# trusted data in the source file). this allows us to test if this is
130# the DATA handle and then to do a sysseek to make sure it gets
131# slurped correctly. on some systems, the buffered i/o pointer is not
132# left at the same place as the fd pointer. this sysseek makes them
133# the same so slurping with sysread will work.
134
135 eval{ require B } ;
136
137 if ( $@ ) {
138
139 @_ = ( \%args, <<ERR ) ;
140Can't find B.pm with this Perl: $!.
141That module is needed to slurp the DATA handle.
142ERR
143 goto &_error ;
144 }
145
146 if ( B::svref_2object( $read_fh )->IO->IoFLAGS & 16 ) {
147
148# set the seek position to the current tell.
149
150 sysseek( $read_fh, tell( $read_fh ), SEEK_SET ) ||
151 croak "sysseek $!" ;
152 }
153 }
154 else {
155
156# a regular file. set the sysopen mode
157
158 my $mode = O_RDONLY ;
635c7876 159
160#printf "RD: BINARY %x MODE %x\n", O_BINARY, $mode ;
161
162# open the file and handle any error
163
164 $read_fh = gensym ;
165 unless ( sysopen( $read_fh, $file_name, $mode ) ) {
166 @_ = ( \%args, "read_file '$file_name' - sysopen: $!");
167 goto &_error ;
168 }
169
cee624ab 170 if ( my $binmode = $args{'binmode'} ) {
171 binmode( $read_fh, $binmode ) ;
172 }
173
635c7876 174# get the size of the file for use in the read loop
175
176 $size_left = -s $read_fh ;
177
f9940db7 178#print "SIZE $size_left\n" ;
8ed110f9 179
635c7876 180
f9940db7 181# we need a blk_size if the size is 0 so we can handle pseudofiles like in
182# /proc. these show as 0 size but have data to be slurped.
183
184 unless( $size_left ) {
185
186 $blk_size = $args{'blk_size'} || 1024 * 1024 ;
187 $size_left = $blk_size ;
188 }
e2c51d31 189 }
190
191
8ed110f9 192# if ( $size_left < 10000 && keys %args == 0 && !wantarray ) {
e2c51d31 193
8ed110f9 194# #print "OPT\n" and $printed++ unless $printed ;
e2c51d31 195
8ed110f9 196# my $read_cnt = sysread( $read_fh, my $buf, $size_left ) ;
e2c51d31 197
8ed110f9 198# unless ( defined $read_cnt ) {
e2c51d31 199
8ed110f9 200# # handle the read error
e2c51d31 201
8ed110f9 202# @_ = ( \%args, "read_file '$file_name' - small2 sysread: $!");
203# goto &_error ;
204# }
e2c51d31 205
8ed110f9 206# return $buf ;
207# }
635c7876 208
209# infinite read loop. we exit when we are done slurping
210
211 while( 1 ) {
212
213# do the read and see how much we got
214
215 my $read_cnt = sysread( $read_fh, ${$buf_ref},
216 $size_left, length ${$buf_ref} ) ;
217
e2c51d31 218 unless ( defined $read_cnt ) {
219
220# handle the read error
221
8ed110f9 222 @_ = ( \%args, "read_file '$file_name' - loop sysread: $!");
e2c51d31 223 goto &_error ;
224 }
635c7876 225
226# good read. see if we hit EOF (nothing left to read)
227
e2c51d31 228 last if $read_cnt == 0 ;
635c7876 229
230# loop if we are slurping a handle. we don't track $size_left then.
231
e2c51d31 232 next if $blk_size ;
635c7876 233
234# count down how much we read and loop if we have more to read.
635c7876 235
e2c51d31 236 $size_left -= $read_cnt ;
237 last if $size_left <= 0 ;
635c7876 238 }
239
240# fix up cr/lf to be a newline if this is a windows text file
241
242 ${$buf_ref} =~ s/\015\012/\n/g if $is_win32 && !$args{'binmode'} ;
243
244# this is the 5 returns in a row. each handles one possible
245# combination of caller context and requested return type
246
247 my $sep = $/ ;
248 $sep = '\n\n+' if defined $sep && $sep eq '' ;
249
250# caller wants to get an array ref of lines
251
252# this split doesn't work since it tries to use variable length lookbehind
253# the m// line works.
254# return [ split( m|(?<=$sep)|, ${$buf_ref} ) ] if $args{'array_ref'} ;
255 return [ length(${$buf_ref}) ? ${$buf_ref} =~ /(.*?$sep|.+)/sg : () ]
256 if $args{'array_ref'} ;
257
258# caller wants a list of lines (normal list context)
259
260# same problem with this split as before.
261# return split( m|(?<=$sep)|, ${$buf_ref} ) if wantarray ;
262 return length(${$buf_ref}) ? ${$buf_ref} =~ /(.*?$sep|.+)/sg : ()
263 if wantarray ;
264
265# caller wants a scalar ref to the slurped text
266
267 return $buf_ref if $args{'scalar_ref'} ;
268
269# caller wants a scalar with the slurped text (normal scalar context)
270
271 return ${$buf_ref} if defined wantarray ;
272
273# caller passed in an i/o buffer by reference (normal void context)
274
275 return ;
276}
277
278sub write_file {
279
280 my $file_name = shift ;
281
282# get the optional argument hash ref from @_ or an empty hash ref.
283
284 my $args = ( ref $_[0] eq 'HASH' ) ? shift : {} ;
285
286 my( $buf_ref, $write_fh, $no_truncate, $orig_file_name, $data_is_ref ) ;
287
288# get the buffer ref - it depends on how the data is passed into write_file
289# after this if/else $buf_ref will have a scalar ref to the data.
290
291 if ( ref $args->{'buf_ref'} eq 'SCALAR' ) {
292
293# a scalar ref passed in %args has the data
294# note that the data was passed by ref
295
296 $buf_ref = $args->{'buf_ref'} ;
297 $data_is_ref = 1 ;
298 }
299 elsif ( ref $_[0] eq 'SCALAR' ) {
300
301# the first value in @_ is the scalar ref to the data
302# note that the data was passed by ref
303
304 $buf_ref = shift ;
305 $data_is_ref = 1 ;
306 }
307 elsif ( ref $_[0] eq 'ARRAY' ) {
308
309# the first value in @_ is the array ref to the data so join it.
310
311 ${$buf_ref} = join '', @{$_[0]} ;
312 }
313 else {
314
315# good old @_ has all the data so join it.
316
317 ${$buf_ref} = join '', @_ ;
318 }
319
320# see if we were passed a open handle to spew to.
321
322 if ( ref $file_name ) {
323
324# we have a handle. make sure we don't call truncate on it.
325
326 $write_fh = $file_name ;
327 $no_truncate = 1 ;
328 }
329 else {
330
331# spew to regular file.
332
333 if ( $args->{'atomic'} ) {
334
335# in atomic mode, we spew to a temp file so make one and save the original
336# file name.
337 $orig_file_name = $file_name ;
338 $file_name .= ".$$" ;
339 }
340
341# set the mode for the sysopen
342
343 my $mode = O_WRONLY | O_CREAT ;
635c7876 344 $mode |= O_APPEND if $args->{'append'} ;
345 $mode |= O_EXCL if $args->{'no_clobber'} ;
346
347#printf "WR: BINARY %x MODE %x\n", O_BINARY, $mode ;
348
349# open the file and handle any error.
350
351 $write_fh = gensym ;
352 unless ( sysopen( $write_fh, $file_name, $mode ) ) {
353 @_ = ( $args, "write_file '$file_name' - sysopen: $!");
354 goto &_error ;
355 }
356 }
357
9aab46ab 358 if ( my $binmode = $args->{'binmode'} ) {
cee624ab 359 binmode( $write_fh, $binmode ) ;
360 }
361
635c7876 362 sysseek( $write_fh, 0, SEEK_END ) if $args->{'append'} ;
363
364
365#print 'WR before data ', unpack( 'H*', ${$buf_ref}), "\n" ;
366
367# fix up newline to write cr/lf if this is a windows text file
368
369 if ( $is_win32 && !$args->{'binmode'} ) {
370
371# copy the write data if it was passed by ref so we don't clobber the
372# caller's data
373 $buf_ref = \do{ my $copy = ${$buf_ref}; } if $data_is_ref ;
374 ${$buf_ref} =~ s/\n/\015\012/g ;
375 }
376
377#print 'after data ', unpack( 'H*', ${$buf_ref}), "\n" ;
378
379# get the size of how much we are writing and init the offset into that buffer
380
381 my $size_left = length( ${$buf_ref} ) ;
382 my $offset = 0 ;
383
384# loop until we have no more data left to write
385
386 do {
387
388# do the write and track how much we just wrote
389
390 my $write_cnt = syswrite( $write_fh, ${$buf_ref},
391 $size_left, $offset ) ;
392
393 unless ( defined $write_cnt ) {
394
395# the write failed
396 @_ = ( $args, "write_file '$file_name' - syswrite: $!");
397 goto &_error ;
398 }
399
400# track much left to write and where to write from in the buffer
401
402 $size_left -= $write_cnt ;
403 $offset += $write_cnt ;
404
405 } while( $size_left > 0 ) ;
406
407# we truncate regular files in case we overwrite a long file with a shorter file
408# so seek to the current position to get it (same as tell()).
409
410 truncate( $write_fh,
411 sysseek( $write_fh, 0, SEEK_CUR ) ) unless $no_truncate ;
412
413 close( $write_fh ) ;
414
415# handle the atomic mode - move the temp file to the original filename.
416
e2c51d31 417 if ( $args->{'atomic'} && !rename( $file_name, $orig_file_name ) ) {
418
419
420 @_ = ( $args, "write_file '$file_name' - rename: $!" ) ;
421 goto &_error ;
422 }
635c7876 423
424 return 1 ;
425}
426
427# this is for backwards compatibility with the previous File::Slurp module.
428# write_file always overwrites an existing file
429
430*overwrite_file = \&write_file ;
431
432# the current write_file has an append mode so we use that. this
433# supports the same API with an optional second argument which is a
434# hash ref of options.
435
436sub append_file {
437
438# get the optional args hash ref
439 my $args = $_[1] ;
440 if ( ref $args eq 'HASH' ) {
441
442# we were passed an args ref so just mark the append mode
443
444 $args->{append} = 1 ;
445 }
446 else {
447
448# no args hash so insert one with the append mode
449
450 splice( @_, 1, 0, { append => 1 } ) ;
451 }
452
453# magic goto the main write_file sub. this overlays the sub without touching
454# the stack or @_
455
456 goto &write_file
457}
458
459# basic wrapper around opendir/readdir
460
461sub read_dir {
462
463 my ($dir, %args ) = @_;
464
465# this handle will be destroyed upon return
466
467 local(*DIRH);
468
469# open the dir and handle any errors
470
471 unless ( opendir( DIRH, $dir ) ) {
472
473 @_ = ( \%args, "read_dir '$dir' - opendir: $!" ) ;
474 goto &_error ;
475 }
476
477 my @dir_entries = readdir(DIRH) ;
478
479 @dir_entries = grep( $_ ne "." && $_ ne "..", @dir_entries )
480 unless $args{'keep_dot_dot'} ;
481
482 return @dir_entries if wantarray ;
483 return \@dir_entries ;
484}
485
486# error handling section
487#
488# all the error handling uses magic goto so the caller will get the
489# error message as if from their code and not this module. if we just
490# did a call on the error code, the carp/croak would report it from
491# this module since the error sub is one level down on the call stack
492# from read_file/write_file/read_dir.
493
494
495my %err_func = (
496 'carp' => \&carp,
497 'croak' => \&croak,
498) ;
499
500sub _error {
501
502 my( $args, $err_msg ) = @_ ;
503
504# get the error function to use
505
506 my $func = $err_func{ $args->{'err_mode'} || 'croak' } ;
507
508# if we didn't find it in our error function hash, they must have set
509# it to quiet and we don't do anything.
510
511 return unless $func ;
512
513# call the carp/croak function
514
515 $func->($err_msg) ;
516
517# return a hard undef (in list context this will be a single value of
518# undef which is not a legal in-band value)
519
520 return undef ;
521}
522
5231;
524__END__
525
526=head1 NAME
527
528File::Slurp - Efficient Reading/Writing of Complete Files
529
530=head1 SYNOPSIS
531
532 use File::Slurp;
533
534 my $text = read_file( 'filename' ) ;
535 my @lines = read_file( 'filename' ) ;
536
537 write_file( 'filename', @lines ) ;
538
539 use File::Slurp qw( slurp ) ;
540
541 my $text = slurp( 'filename' ) ;
542
543
544=head1 DESCRIPTION
545
546This module provides subs that allow you to read or write entire files
547with one simple call. They are designed to be simple to use, have
548flexible ways to pass in or get the file contents and to be very
549efficient. There is also a sub to read in all the files in a
550directory other than C<.> and C<..>
551
552These slurp/spew subs work for files, pipes and
553sockets, and stdio, pseudo-files, and DATA.
554
555=head2 B<read_file>
556
557This sub reads in an entire file and returns its contents to the
558caller. In list context it will return a list of lines (using the
559current value of $/ as the separator including support for paragraph
560mode when it is set to ''). In scalar context it returns the entire
561file as a single scalar.
562
563 my $text = read_file( 'filename' ) ;
564 my @lines = read_file( 'filename' ) ;
565
566The first argument to C<read_file> is the filename and the rest of the
567arguments are key/value pairs which are optional and which modify the
568behavior of the call. Other than binmode the options all control how
569the slurped file is returned to the caller.
570
571If the first argument is a file handle reference or I/O object (if ref
572is true), then that handle is slurped in. This mode is supported so
573you slurp handles such as C<DATA>, C<STDIN>. See the test handle.t
574for an example that does C<open( '-|' )> and child process spews data
575to the parant which slurps it in. All of the options that control how
576the data is returned to the caller still work in this case.
577
578NOTE: as of version 9999.06, read_file works correctly on the C<DATA>
579handle. It used to need a sysseek workaround but that is now handled
580when needed by the module itself.
581
582You can optionally request that C<slurp()> is exported to your code. This
583is an alias for read_file and is meant to be forward compatible with
584Perl 6 (which will have slurp() built-in).
585
586The options are:
587
588=head3 binmode
589
9aab46ab 590If you set the binmode option, then the option will be passed to a
591binmode call on the opened filehandle.
635c7876 592
593 my $bin_data = read_file( $bin_file, binmode => ':raw' ) ;
9aab46ab 594 my $utf_text = read_file( $bin_file, binmode => ':utf8' ) ;
635c7876 595
596=head3 array_ref
597
598If this boolean option is set, the return value (only in scalar
599context) will be an array reference which contains the lines of the
600slurped file. The following two calls are equivalent:
601
602 my $lines_ref = read_file( $bin_file, array_ref => 1 ) ;
603 my $lines_ref = [ read_file( $bin_file ) ] ;
604
605=head3 scalar_ref
606
607If this boolean option is set, the return value (only in scalar
608context) will be an scalar reference to a string which is the contents
609of the slurped file. This will usually be faster than returning the
610plain scalar.
611
612 my $text_ref = read_file( $bin_file, scalar_ref => 1 ) ;
613
614=head3 buf_ref
615
616You can use this option to pass in a scalar reference and the slurped
617file contents will be stored in the scalar. This can be used in
618conjunction with any of the other options.
619
620 my $text_ref = read_file( $bin_file, buf_ref => \$buffer,
621 array_ref => 1 ) ;
622 my @lines = read_file( $bin_file, buf_ref => \$buffer ) ;
623
624=head3 blk_size
625
626You can use this option to set the block size used when slurping from an already open handle (like \*STDIN). It defaults to 1MB.
627
628 my $text_ref = read_file( $bin_file, blk_size => 10_000_000,
629 array_ref => 1 ) ;
630
631=head3 err_mode
632
633You can use this option to control how read_file behaves when an error
634occurs. This option defaults to 'croak'. You can set it to 'carp' or
635to 'quiet to have no error handling. This code wants to carp and then
636read abother file if it fails.
637
638 my $text_ref = read_file( $file, err_mode => 'carp' ) ;
639 unless ( $text_ref ) {
640
641 # read a different file but croak if not found
642 $text_ref = read_file( $another_file ) ;
643 }
644
645 # process ${$text_ref}
646
647=head2 B<write_file>
648
649This sub writes out an entire file in one call.
650
651 write_file( 'filename', @data ) ;
652
653The first argument to C<write_file> is the filename. The next argument
654is an optional hash reference and it contains key/values that can
655modify the behavior of C<write_file>. The rest of the argument list is
656the data to be written to the file.
657
658 write_file( 'filename', {append => 1 }, @data ) ;
659 write_file( 'filename', {binmode => ':raw' }, $buffer ) ;
660
661As a shortcut if the first data argument is a scalar or array
662reference, it is used as the only data to be written to the file. Any
663following arguments in @_ are ignored. This is a faster way to pass in
664the output to be written to the file and is equivilent to the
665C<buf_ref> option. These following pairs are equivilent but the pass
666by reference call will be faster in most cases (especially with larger
667files).
668
669 write_file( 'filename', \$buffer ) ;
670 write_file( 'filename', $buffer ) ;
671
672 write_file( 'filename', \@lines ) ;
673 write_file( 'filename', @lines ) ;
674
675If the first argument is a file handle reference or I/O object (if ref
676is true), then that handle is slurped in. This mode is supported so
677you spew to handles such as \*STDOUT. See the test handle.t for an
678example that does C<open( '-|' )> and child process spews data to the
679parant which slurps it in. All of the options that control how the
680data is passes into C<write_file> still work in this case.
681
682C<write_file> returns 1 upon successfully writing the file or undef if
683it encountered an error.
684
685The options are:
686
687=head3 binmode
688
689If you set the binmode option, then the file will be written in binary
690mode.
691
692 write_file( $bin_file, {binmode => ':raw'}, @data ) ;
693
694NOTE: this actually sets the O_BINARY mode flag for sysopen. It
695probably should call binmode and pass its argument to support other
696file modes.
697
698=head3 buf_ref
699
700You can use this option to pass in a scalar reference which has the
701data to be written. If this is set then any data arguments (including
702the scalar reference shortcut) in @_ will be ignored. These are
703equivilent:
704
705 write_file( $bin_file, { buf_ref => \$buffer } ) ;
706 write_file( $bin_file, \$buffer ) ;
707 write_file( $bin_file, $buffer ) ;
708
709=head3 atomic
710
711If you set this boolean option, the file will be written to in an
712atomic fashion. A temporary file name is created by appending the pid
713($$) to the file name argument and that file is spewed to. After the
714file is closed it is renamed to the original file name (and rename is
715an atomic operation on most OS's). If the program using this were to
716crash in the middle of this, then the file with the pid suffix could
717be left behind.
718
719=head3 append
720
721If you set this boolean option, the data will be written at the end of
722the current file.
723
724 write_file( $file, {append => 1}, @data ) ;
725
726C<write_file> croaks if it cannot open the file. It returns true if it
727succeeded in writing out the file and undef if there was an
728error. (Yes, I know if it croaks it can't return anything but that is
729for when I add the options to select the error handling mode).
730
731=head3 no_clobber
732
733If you set this boolean option, an existing file will not be overwritten.
734
735 write_file( $file, {no_clobber => 1}, @data ) ;
736
737=head3 err_mode
738
739You can use this option to control how C<write_file> behaves when an
740error occurs. This option defaults to 'croak'. You can set it to
741'carp' or to 'quiet' to have no error handling other than the return
742value. If the first call to C<write_file> fails it will carp and then
743write to another file. If the second call to C<write_file> fails, it
744will croak.
745
746 unless ( write_file( $file, { err_mode => 'carp', \$data ) ;
747
748 # write a different file but croak if not found
749 write_file( $other_file, \$data ) ;
750 }
751
752=head2 overwrite_file
753
754This sub is just a typeglob alias to write_file since write_file
755always overwrites an existing file. This sub is supported for
756backwards compatibility with the original version of this module. See
757write_file for its API and behavior.
758
759=head2 append_file
760
761This sub will write its data to the end of the file. It is a wrapper
762around write_file and it has the same API so see that for the full
763documentation. These calls are equivilent:
764
765 append_file( $file, @data ) ;
766 write_file( $file, {append => 1}, @data ) ;
767
768=head2 read_dir
769
770This sub reads all the file names from directory and returns them to
771the caller but C<.> and C<..> are removed by default.
772
773 my @files = read_dir( '/path/to/dir' ) ;
774
775It croaks if it cannot open the directory.
776
777In a list context C<read_dir> returns a list of the entries in the
778directory. In a scalar context it returns an array reference which has
779the entries.
780
781=head3 keep_dot_dot
782
783If this boolean option is set, C<.> and C<..> are not removed from the
784list of files.
785
786 my @all_files = read_dir( '/path/to/dir', keep_dot_dot => 1 ) ;
787
788=head2 EXPORT
789
790 read_file write_file overwrite_file append_file read_dir
791
792=head2 SEE ALSO
793
794An article on file slurping in extras/slurp_article.pod. There is
795also a benchmarking script in extras/slurp_bench.pl.
796
797=head2 BUGS
798
799If run under Perl 5.004, slurping from the DATA handle will fail as
800that requires B.pm which didn't get into core until 5.005.
801
802=head1 AUTHOR
803
804Uri Guttman, E<lt>uri@stemsystems.comE<gt>
805
806=cut