9 use Fcntl qw( :DEFAULT ) ;
10 use POSIX qw( :fcntl_h ) ;
13 use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION ) ;
14 @ISA = qw( Exporter ) ;
18 @EXPORT_OK = qw( slurp prepend_file ) ;
19 %EXPORT_TAGS = ( 'all' => [
20 qw( read_file write_file overwrite_file append_file read_dir ) ] ) ;
21 @EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
23 my $max_fast_slurp_size = 1024 * 100 ;
25 my $is_win32 = $^O =~ /win32/i ;
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:
34 unless( defined &SEEK_SET ) {
35 *SEEK_SET = sub { 0 };
36 *SEEK_CUR = sub { 1 };
37 *SEEK_END = sub { 2 };
40 unless( defined &O_BINARY ) {
41 *O_BINARY = sub { 0 };
42 *O_RDONLY = sub { 0 };
43 *O_WRONLY = sub { 1 };
46 unless ( defined &O_APPEND ) {
48 if ( $^O =~ /olaris/ ) {
49 *O_APPEND = sub { 8 };
50 *O_CREAT = sub { 256 };
51 *O_EXCL = sub { 1024 };
53 elsif ( $^O =~ /inux/ ) {
54 *O_APPEND = sub { 1024 };
55 *O_CREAT = sub { 64 };
56 *O_EXCL = sub { 128 };
58 elsif ( $^O =~ /BSD/i ) {
59 *O_APPEND = sub { 8 };
60 *O_CREAT = sub { 512 };
61 *O_EXCL = sub { 2048 };
66 # print "OS [$^O]\n" ;
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" ;
76 *slurp = \&read_file ;
80 my $file_name = shift ;
81 my $opts = ( ref $_[0] eq 'HASH' ) ? shift : { @_ } ;
83 # this is the optimized read_file for shorter files.
84 # the test for -s > 0 is to allow pseudo files to be read with the
85 # regular loop since they return a size of 0.
87 if ( !ref $file_name && -e $file_name && -s _ > 0 &&
88 -s _ < $max_fast_slurp_size && !%{$opts} && !wantarray ) {
92 unless( sysopen( $fh, $file_name, O_RDONLY ) ) {
94 @_ = ( $opts, "read_file '$file_name' - sysopen: $!");
98 my $read_cnt = sysread( $fh, my $buf, -s _ ) ;
100 unless ( defined $read_cnt ) {
103 "read_file '$file_name' - small sysread: $!");
107 $buf =~ s/\015\012/\n/g if $is_win32 ;
111 # set the buffer to either the passed in one or ours and init it to the null
115 my $buf_ref = $opts->{'buf_ref'} || \$buf ;
118 my( $read_fh, $size_left, $blk_size ) ;
120 # deal with ref for a file name
121 # it could be an open handle or an overloaded object
123 if ( ref $file_name ) {
125 my $ref_result = _check_ref( $file_name ) ;
127 if ( ref $ref_result ) {
129 # we got an error, deal with it
131 @_ = ( $opts, $ref_result ) ;
137 # we got an overloaded object and the result is the stringified value
138 # use it as the file name
140 $file_name = $ref_result ;
144 # here we have just an open handle. set $read_fh so we don't do a sysopen
146 $read_fh = $file_name ;
147 $blk_size = $opts->{'blk_size'} || 1024 * 1024 ;
148 $size_left = $blk_size ;
152 # see if we have a path we need to open
154 unless ( $read_fh ) {
156 # a regular file. set the sysopen mode
158 my $mode = O_RDONLY ;
160 #printf "RD: BINARY %x MODE %x\n", O_BINARY, $mode ;
163 unless ( sysopen( $read_fh, $file_name, $mode ) ) {
164 @_ = ( $opts, "read_file '$file_name' - sysopen: $!");
168 if ( my $binmode = $opts->{'binmode'} ) {
169 binmode( $read_fh, $binmode ) ;
172 # get the size of the file for use in the read loop
174 $size_left = -s $read_fh ;
176 #print "SIZE $size_left\n" ;
178 # we need a blk_size if the size is 0 so we can handle pseudofiles like in
179 # /proc. these show as 0 size but have data to be slurped.
181 unless( $size_left ) {
183 $blk_size = $opts->{'blk_size'} || 1024 * 1024 ;
184 $size_left = $blk_size ;
189 # if ( $size_left < 10000 && keys %{$opts} == 0 && !wantarray ) {
191 # my $read_cnt = sysread( $read_fh, my $buf, $size_left ) ;
193 # unless ( defined $read_cnt ) {
195 # @_ = ( $opts, "read_file '$file_name' - small2 sysread: $!");
202 # infinite read loop. we exit when we are done slurping
206 # do the read and see how much we got
208 my $read_cnt = sysread( $read_fh, ${$buf_ref},
209 $size_left, length ${$buf_ref} ) ;
211 unless ( defined $read_cnt ) {
213 @_ = ( $opts, "read_file '$file_name' - loop sysread: $!");
217 # good read. see if we hit EOF (nothing left to read)
219 last if $read_cnt == 0 ;
221 # loop if we are slurping a handle. we don't track $size_left then.
225 # count down how much we read and loop if we have more to read.
227 $size_left -= $read_cnt ;
228 last if $size_left <= 0 ;
231 # fix up cr/lf to be a newline if this is a windows text file
233 ${$buf_ref} =~ s/\015\012/\n/g if $is_win32 && !$opts->{'binmode'} ;
236 $sep = '\n\n+' if defined $sep && $sep eq '' ;
238 # see if caller wants lines
240 if( wantarray || $opts->{'array_ref'} ) {
244 my @lines = length(${$buf_ref}) ?
245 ${$buf_ref} =~ /(.*?$sep|.+)/sg : () ;
247 # caller wants an array ref
249 return \@lines if $opts->{'array_ref'} ;
251 # caller wants list of lines
256 # caller wants a scalar ref to the slurped text
258 return $buf_ref if $opts->{'scalar_ref'} ;
260 # caller wants a scalar with the slurped text (normal scalar context)
262 return ${$buf_ref} if defined wantarray ;
264 # caller passed in an i/o buffer by reference (normal void context)
269 # errors in this sub are returned as scalar refs
270 # a normal IO/GLOB handle is an empty return
271 # an overloaded object returns its stringified as a scalarfilename
277 # check if we are reading from a handle (GLOB or IO object)
279 if ( eval { $handle->isa( 'GLOB' ) || $handle->isa( 'IO' ) } ) {
281 # we have a handle. deal with seeking to it if it is DATA
283 my $err = _seek_data_handle( $handle ) ;
285 # return the error string if any
287 return \$err if $err ;
289 # we have good handle
293 eval { require overload } ;
295 # return an error if we can't load the overload pragma
296 # or if the object isn't overloaded
298 return \"Bad handle '$handle' is not a GLOB or IO object or overloaded"
299 if $@ || !overload::Overloaded( $handle ) ;
301 # must be overloaded so return its stringified value
306 sub _seek_data_handle {
310 # DEEP DARK MAGIC. this checks the UNTAINT IO flag of a
311 # glob/handle. only the DATA handle is untainted (since it is from
312 # trusted data in the source file). this allows us to test if this is
313 # the DATA handle and then to do a sysseek to make sure it gets
314 # slurped correctly. on some systems, the buffered i/o pointer is not
315 # left at the same place as the fd pointer. this sysseek makes them
316 # the same so slurping with sysread will work.
323 Can't find B.pm with this Perl: $!.
324 That module is needed to properly slurp the DATA handle.
328 if ( B::svref_2object( $handle )->IO->IoFLAGS & 16 ) {
330 # set the seek position to the current tell.
332 unless( sysseek( $handle, tell( $handle ), SEEK_SET ) ) {
333 return "read_file '$handle' - sysseek: $!" ;
337 # seek was successful, return no error string
345 my $file_name = shift ;
347 # get the optional argument hash ref from @_ or an empty hash ref.
349 my $opts = ( ref $_[0] eq 'HASH' ) ? shift : {} ;
351 my( $buf_ref, $write_fh, $no_truncate, $orig_file_name, $data_is_ref ) ;
353 # get the buffer ref - it depends on how the data is passed into write_file
354 # after this if/else $buf_ref will have a scalar ref to the data.
356 if ( ref $opts->{'buf_ref'} eq 'SCALAR' ) {
358 # a scalar ref passed in %opts has the data
359 # note that the data was passed by ref
361 $buf_ref = $opts->{'buf_ref'} ;
364 elsif ( ref $_[0] eq 'SCALAR' ) {
366 # the first value in @_ is the scalar ref to the data
367 # note that the data was passed by ref
372 elsif ( ref $_[0] eq 'ARRAY' ) {
374 # the first value in @_ is the array ref to the data so join it.
376 ${$buf_ref} = join '', @{$_[0]} ;
380 # good old @_ has all the data so join it.
382 ${$buf_ref} = join '', @_ ;
385 # deal with ref for a file name
387 if ( ref $file_name ) {
389 my $ref_result = _check_ref( $file_name ) ;
391 if ( ref $ref_result ) {
393 # we got an error, deal with it
395 @_ = ( $opts, $ref_result ) ;
401 # we got an overloaded object and the result is the stringified value
402 # use it as the file name
404 $file_name = $ref_result ;
408 # we now have a proper handle ref.
409 # make sure we don't call truncate on it.
411 $write_fh = $file_name ;
416 # see if we have a path we need to open
418 unless( $write_fh ) {
420 # spew to regular file.
422 if ( $opts->{'atomic'} ) {
424 # in atomic mode, we spew to a temp file so make one and save the original
426 $orig_file_name = $file_name ;
427 $file_name .= ".$$" ;
430 # set the mode for the sysopen
432 my $mode = O_WRONLY | O_CREAT ;
433 $mode |= O_APPEND if $opts->{'append'} ;
434 $mode |= O_EXCL if $opts->{'no_clobber'} ;
436 my $perms = $opts->{perms} ;
437 $perms = 0666 unless defined $perms ;
439 #printf "WR: BINARY %x MODE %x\n", O_BINARY, $mode ;
441 # open the file and handle any error.
444 unless ( sysopen( $write_fh, $file_name, $mode, $perms ) ) {
446 @_ = ( $opts, "write_file '$file_name' - sysopen: $!");
451 if ( my $binmode = $opts->{'binmode'} ) {
452 binmode( $write_fh, $binmode ) ;
455 sysseek( $write_fh, 0, SEEK_END ) if $opts->{'append'} ;
457 #print 'WR before data ', unpack( 'H*', ${$buf_ref}), "\n" ;
459 # fix up newline to write cr/lf if this is a windows text file
461 if ( $is_win32 && !$opts->{'binmode'} ) {
463 # copy the write data if it was passed by ref so we don't clobber the
465 $buf_ref = \do{ my $copy = ${$buf_ref}; } if $data_is_ref ;
466 ${$buf_ref} =~ s/\n/\015\012/g ;
469 #print 'after data ', unpack( 'H*', ${$buf_ref}), "\n" ;
471 # get the size of how much we are writing and init the offset into that buffer
473 my $size_left = length( ${$buf_ref} ) ;
476 # loop until we have no more data left to write
480 # do the write and track how much we just wrote
482 my $write_cnt = syswrite( $write_fh, ${$buf_ref},
483 $size_left, $offset ) ;
485 unless ( defined $write_cnt ) {
487 @_ = ( $opts, "write_file '$file_name' - syswrite: $!");
491 # track how much left to write and where to write from in the buffer
493 $size_left -= $write_cnt ;
494 $offset += $write_cnt ;
496 } while( $size_left > 0 ) ;
498 # we truncate regular files in case we overwrite a long file with a shorter file
499 # so seek to the current position to get it (same as tell()).
502 sysseek( $write_fh, 0, SEEK_CUR ) ) unless $no_truncate ;
506 # handle the atomic mode - move the temp file to the original filename.
508 if ( $opts->{'atomic'} && !rename( $file_name, $orig_file_name ) ) {
510 @_ = ( $opts, "write_file '$file_name' - rename: $!" ) ;
517 # this is for backwards compatibility with the previous File::Slurp module.
518 # write_file always overwrites an existing file
520 *overwrite_file = \&write_file ;
522 # the current write_file has an append mode so we use that. this
523 # supports the same API with an optional second argument which is a
524 # hash ref of options.
528 # get the optional opts hash ref
530 if ( ref $opts eq 'HASH' ) {
532 # we were passed an opts ref so just mark the append mode
534 $opts->{append} = 1 ;
538 # no opts hash so insert one with the append mode
540 splice( @_, 1, 0, { append => 1 } ) ;
543 # magic goto the main write_file sub. this overlays the sub without touching
549 # prepend data to the beginning of a file
553 my $file_name = shift ;
555 #print "FILE $file_name\n" ;
557 my $opts = ( ref $_[0] eq 'HASH' ) ? shift : {} ;
559 # delete unsupported options
562 grep $_ ne 'err_mode' && $_ ne 'binmode', keys %{$opts} ;
564 delete @{$opts}{@bad_opts} ;
566 my $prepend_data = shift ;
567 $prepend_data = '' unless defined $prepend_data ;
568 $prepend_data = ${$prepend_data} if ref $prepend_data eq 'SCALAR' ;
570 #print "PRE [$prepend_data]\n" ;
572 my $err_mode = delete $opts->{err_mode} ;
573 $opts->{ err_mode } = 'croak' ;
574 $opts->{ scalar_ref } = 1 ;
576 my $existing_data = eval { read_file( $file_name, $opts ) } ;
580 @_ = ( { err_mode => $err_mode },
581 "prepend_file '$file_name' - read_file: $!" ) ;
585 #print "EXIST [$$existing_data]\n" ;
587 $opts->{atomic} = 1 ;
589 eval { write_file( $file_name, $opts,
590 $prepend_data, $$existing_data ) ;
595 @_ = ( { err_mode => $err_mode },
596 "prepend_file '$file_name' - write_file: $!" ) ;
600 return $write_result ;
603 # edit a file as a scalar in $_
605 sub edit_file(&$;$) {
607 my( $edit_code, $file_name, $opts ) = @_ ;
608 $opts = {} unless ref $opts eq 'HASH' ;
610 # my $edit_code = shift ;
611 # my $file_name = shift ;
612 # my $opts = ( ref $_[0] eq 'HASH' ) ? shift : {} ;
614 #print "FILE $file_name\n" ;
616 # delete unsupported options
619 grep $_ ne 'err_mode' && $_ ne 'binmode', keys %{$opts} ;
621 delete @{$opts}{@bad_opts} ;
623 # keep the user err_mode and force croaking on internal errors
625 my $err_mode = delete $opts->{err_mode} ;
626 $opts->{ err_mode } = 'croak' ;
628 # get a scalar ref for speed and slurp the file into a scalar
630 $opts->{ scalar_ref } = 1 ;
631 my $existing_data = eval { read_file( $file_name, $opts ) } ;
635 @_ = ( { err_mode => $err_mode },
636 "edit_file '$file_name' - read_file: $!" ) ;
640 #print "EXIST [$$existing_data]\n" ;
642 my( $edited_data ) = map { $edit_code->(); $_ } $$existing_data ;
644 $opts->{atomic} = 1 ;
646 eval { write_file( $file_name, $opts, $edited_data ) } ;
650 @_ = ( { err_mode => $err_mode },
651 "edit_file '$file_name' - write_file: $!" ) ;
655 return $write_result ;
658 sub edit_file_lines(&$;$) {
660 my( $edit_code, $file_name, $opts ) = @_ ;
661 $opts = {} unless ref $opts eq 'HASH' ;
663 # my $edit_code = shift ;
664 # my $file_name = shift ;
665 # my $opts = ( ref $_[0] eq 'HASH' ) ? shift : {} ;
667 #print "FILE $file_name\n" ;
669 # delete unsupported options
672 grep $_ ne 'err_mode' && $_ ne 'binmode', keys %{$opts} ;
674 delete @{$opts}{@bad_opts} ;
676 # keep the user err_mode and force croaking on internal errors
678 my $err_mode = delete $opts->{err_mode} ;
679 $opts->{ err_mode } = 'croak' ;
681 # get an array ref for speed and slurp the file into lines
683 $opts->{ array_ref } = 1 ;
684 my $existing_data = eval { read_file( $file_name, $opts ) } ;
688 @_ = ( { err_mode => $err_mode },
689 "edit_file_lines '$file_name' - read_file: $!" ) ;
693 #print "EXIST [$$existing_data]\n" ;
695 my @edited_data = map { $edit_code->(); $_ } @$existing_data ;
697 $opts->{atomic} = 1 ;
699 eval { write_file( $file_name, $opts, @edited_data ) } ;
703 @_ = ( { err_mode => $err_mode },
704 "edit_file_lines '$file_name' - write_file: $!" ) ;
708 return $write_result ;
711 # basic wrapper around opendir/readdir
716 my $opts = ( ref $_[0] eq 'HASH' ) ? shift : { @_ } ;
718 # this handle will be destroyed upon return
722 # open the dir and handle any errors
724 unless ( opendir( DIRH, $dir ) ) {
726 @_ = ( $opts, "read_dir '$dir' - opendir: $!" ) ;
730 my @dir_entries = readdir(DIRH) ;
732 @dir_entries = grep( $_ ne "." && $_ ne "..", @dir_entries )
733 unless $opts->{'keep_dot_dot'} ;
735 return @dir_entries if wantarray ;
736 return \@dir_entries ;
739 # error handling section
741 # all the error handling uses magic goto so the caller will get the
742 # error message as if from their code and not this module. if we just
743 # did a call on the error code, the carp/croak would report it from
744 # this module since the error sub is one level down on the call stack
745 # from read_file/write_file/read_dir.
755 my( $opts, $err_msg ) = @_ ;
757 # get the error function to use
759 my $func = $err_func{ $opts->{'err_mode'} || 'croak' } ;
761 # if we didn't find it in our error function hash, they must have set
762 # it to quiet and we don't do anything.
764 return unless $func ;
766 # call the carp/croak function
768 $func->($err_msg) if $func ;
770 # return a hard undef (in list context this will be a single value of
771 # undef which is not a legal in-band value)
781 File::Slurp - Simple and Efficient Reading/Writing/Modifying of Complete Files
787 # read in a whole file into a scalar
789 my $text = read_file( 'filename' ) ;
791 # read in a whole file into an array of lines
793 my @lines = read_file( 'filename' ) ;
795 # write out a whole file from a scalar
797 write_file( 'filename', $text ) ;
799 # write out a whole file from an array of lines
801 write_file( 'filename', @lines ) ;
803 # Here is a simple and fast way to load and save a simple config file
804 # made of key=value lines.
806 my %conf = read_file( $file_name ) =~ /^(\w+)=(\.*)$/mg ;
807 write_file( $file_name, {atomic => 1}, map "$_=$conf{$_}\n", keys %conf ;
809 # insert text at the beginning of a file
811 prepend_file( 'filename', $text ) ;
813 # read in a whole directory of file names (skipping . and ..)
815 my @files = read_dir( '/path/to/dir' ) ;
819 This module provides subs that allow you to read or write entire files
820 with one simple call. They are designed to be simple to use, have
821 flexible ways to pass in or get the file contents and to be very
822 efficient. There is also a sub to read in all the files in a
823 directory other than C<.> and C<..>
825 These slurp/spew subs work for files, pipes and sockets, stdio,
826 pseudo-files, and the DATA handle. Read more about why slurping files is
827 a good thing in the file 'slurp_article.pod' in the extras/ directory.
829 If you are interested in how fast these calls work, check out the
830 slurp_bench.pl program in the extras/ directory. It compares many
831 different forms of slurping. You can select the I/O direction, context
832 and file sizes. Use the --help option to see how to run it.
836 This sub reads in an entire file and returns its contents to the
837 caller. In scalar context it returns the entire file as a single
838 scalar. In list context it will return a list of lines (using the
839 current value of $/ as the separator including support for paragraph
840 mode when it is set to '').
842 my $text = read_file( 'filename' ) ;
843 my $bin = read_file( 'filename' { binmode => ':raw' } ) ;
844 my @lines = read_file( 'filename' ) ;
845 my $lines = read_file( 'filename', array_ref => 1 ) ;
847 The first argument is the file to slurp in. If the next argument is a
848 hash reference, then it is used as the options. Otherwise the rest of
849 the argument list are is used as key/value options.
851 If the file argument is a handle (if it is a ref and is an IO or GLOB
852 object), then that handle is slurped in. This mode is supported so you
853 slurp handles such as C<DATA> and C<STDIN>. See the test handle.t for
854 an example that does C<open( '-|' )> and the child process spews data
855 to the parant which slurps it in. All of the options that control how
856 the data is returned to the caller still work in this case.
858 If the first argument is an overloaded object then its stringified value
859 is used for the filename and that file is opened. This is a new feature
860 in 9999.14. See the stringify.t test for an example.
862 By default C<read_file> returns an undef in scalar contex or a single
863 undef in list context if it encounters an error. Those are both
864 impossible to get with a clean read_file call which means you can check
865 the return value and always know if you had an error. You can change how
866 errors are handled with the C<err_mode> option.
868 Speed Note: If you call read_file and just get a scalar return value
869 it is now optimized to handle shorter files. This is only used if no
870 options are used, the file is shorter then 100k bytes, the filename is
871 a plain scalar and a scalar file is returned. If you want the fastest
872 slurping, use the C<buf_ref> or C<scalar_ref> options (see below)
874 NOTE: as of version 9999.06, read_file works correctly on the C<DATA>
875 handle. It used to need a sysseek workaround but that is now handled
876 when needed by the module itself.
878 You can optionally request that C<slurp()> is exported to your code. This
879 is an alias for read_file and is meant to be forward compatible with
880 Perl 6 (which will have slurp() built-in).
882 The options for C<read_file> are:
886 If you set the binmode option, then its value is passed to a call to
887 binmode on the opened handle. You can use this to set the file to be
888 read in binary mode, utf8, etc. See perldoc -f binmode for more.
890 my $bin_data = read_file( $bin_file, binmode => ':raw' ) ;
891 my $utf_text = read_file( $bin_file, binmode => ':utf8' ) ;
895 If this boolean option is set, the return value (only in scalar
896 context) will be an array reference which contains the lines of the
897 slurped file. The following two calls are equivalent:
899 my $lines_ref = read_file( $bin_file, array_ref => 1 ) ;
900 my $lines_ref = [ read_file( $bin_file ) ] ;
904 If this boolean option is set, the return value (only in scalar
905 context) will be an scalar reference to a string which is the contents
906 of the slurped file. This will usually be faster than returning the
907 plain scalar. It will also save memory as it will not make a copy of
908 the file to return. Run the extras/slurp_bench.pl script to see speed
911 my $text_ref = read_file( $bin_file, scalar_ref => 1 ) ;
915 You can use this option to pass in a scalar reference and the slurped
916 file contents will be stored in the scalar. This can be used in
917 conjunction with any of the other options. This saves an extra copy of
918 the slurped file and can lower ram usage vs returning the file. It is
919 usually the fastest way to read a file into a scalar. Run the
920 extras/slurp_bench.pl script to see speed comparisons.
923 read_file( $bin_file, buf_ref => \$buffer ) ;
927 You can use this option to set the block size used when slurping from
928 an already open handle (like \*STDIN). It defaults to 1MB.
930 my $text_ref = read_file( $bin_file, blk_size => 10_000_000,
935 You can use this option to control how read_file behaves when an error
936 occurs. This option defaults to 'croak'. You can set it to 'carp' or to
937 'quiet to have no special error handling. This code wants to carp and
938 then read another file if it fails.
940 my $text_ref = read_file( $file, err_mode => 'carp' ) ;
941 unless ( $text_ref ) {
943 # read a different file but croak if not found
944 $text_ref = read_file( $another_file ) ;
947 # process ${$text_ref}
951 This sub writes out an entire file in one call.
953 write_file( 'filename', @data ) ;
955 The first argument to C<write_file> is the filename. The next argument
956 is an optional hash reference and it contains key/values that can
957 modify the behavior of C<write_file>. The rest of the argument list is
958 the data to be written to the file.
960 write_file( 'filename', {append => 1 }, @data ) ;
961 write_file( 'filename', {binmode => ':raw'}, $buffer ) ;
963 As a shortcut if the first data argument is a scalar or array reference,
964 it is used as the only data to be written to the file. Any following
965 arguments in @_ are ignored. This is a faster way to pass in the output
966 to be written to the file and is equivalent to the C<buf_ref> option of
967 C<read_file>. These following pairs are equivalent but the pass by
968 reference call will be faster in most cases (especially with larger
971 write_file( 'filename', \$buffer ) ;
972 write_file( 'filename', $buffer ) ;
974 write_file( 'filename', \@lines ) ;
975 write_file( 'filename', @lines ) ;
977 If the first argument is a handle (if it is a ref and is an IO or GLOB
978 object), then that handle is written to. This mode is supported so you
979 spew to handles such as \*STDOUT. See the test handle.t for an example
980 that does C<open( '-|' )> and child process spews data to the parent
981 which slurps it in. All of the options that control how the data are
982 passed into C<write_file> still work in this case.
984 If the first argument is an overloaded object then its stringified value
985 is used for the filename and that file is opened. This is new feature
986 in 9999.14. See the stringify.t test for an example.
988 By default C<write_file> returns 1 upon successfully writing the file or
989 undef if it encountered an error. You can change how errors are handled
990 with the C<err_mode> option.
996 If you set the binmode option, then its value is passed to a call to
997 binmode on the opened handle. You can use this to set the file to be
998 read in binary mode, utf8, etc. See perldoc -f binmode for more.
1000 write_file( $bin_file, {binmode => ':raw'}, @data ) ;
1001 write_file( $bin_file, {binmode => ':utf8'}, $utf_text ) ;
1005 The perms option sets the permissions of newly-created files. This value
1006 is modified by your process's umask and defaults to 0666 (same as
1009 NOTE: this option is new as of File::Slurp version 9999.14;
1013 You can use this option to pass in a scalar reference which has the
1014 data to be written. If this is set then any data arguments (including
1015 the scalar reference shortcut) in @_ will be ignored. These are
1018 write_file( $bin_file, { buf_ref => \$buffer } ) ;
1019 write_file( $bin_file, \$buffer ) ;
1020 write_file( $bin_file, $buffer ) ;
1024 If you set this boolean option, the file will be written to in an
1025 atomic fashion. A temporary file name is created by appending the pid
1026 ($$) to the file name argument and that file is spewed to. After the
1027 file is closed it is renamed to the original file name (and rename is
1028 an atomic operation on most OS's). If the program using this were to
1029 crash in the middle of this, then the file with the pid suffix could
1034 If you set this boolean option, the data will be written at the end of
1035 the current file. Internally this sets the sysopen mode flag O_APPEND.
1037 write_file( $file, {append => 1}, @data ) ;
1040 can import append_file and it does the same thing.
1044 If you set this boolean option, an existing file will not be overwritten.
1046 write_file( $file, {no_clobber => 1}, @data ) ;
1050 You can use this option to control how C<write_file> behaves when an
1051 error occurs. This option defaults to 'croak'. You can set it to
1052 'carp' or to 'quiet' to have no error handling other than the return
1053 value. If the first call to C<write_file> fails it will carp and then
1054 write to another file. If the second call to C<write_file> fails, it
1057 unless ( write_file( $file, { err_mode => 'carp', \$data ) ;
1059 # write a different file but croak if not found
1060 write_file( $other_file, \$data ) ;
1063 =head2 overwrite_file
1065 This sub is just a typeglob alias to write_file since write_file
1066 always overwrites an existing file. This sub is supported for
1067 backwards compatibility with the original version of this module. See
1068 write_file for its API and behavior.
1072 This sub will write its data to the end of the file. It is a wrapper
1073 around write_file and it has the same API so see that for the full
1074 documentation. These calls are equivalent:
1076 append_file( $file, @data ) ;
1077 write_file( $file, {append => 1}, @data ) ;
1082 This sub writes data to the beginning of a file. The previously existing
1083 data is written after that so the effect is prepending data in front of
1084 a file. It is a counterpart to the append_file sub in this module. It
1085 works by first using C<read_file> to slurp in the file and then calling
1086 C<write_file> with the new data and the existing file data.
1088 The first argument to C<prepend_file> is the filename. The next argument
1089 is an optional hash reference and it contains key/values that can modify
1090 the behavior of C<prepend_file>. The rest of the argument list is the
1091 data to be written to the file and that is passed to C<write_file> as is
1092 (see that for allowed data).
1094 Only the C<binmode> and C<err_mode> options are supported. The
1095 C<write_file> call has the C<atomic> option set so you will always have
1096 a consistant file. See above for more about those options.
1098 C<prepend_file> is not exported by default, you need to import it
1101 use File::Slurp qw( prepend_file ) ;
1102 prepend_file( $file, $header ) ;
1103 prepend_file( $file, \@lines ) ;
1104 prepend_file( $file, { binmode => 'raw:'}, $bin_data ) ;
1108 This sub reads all the file names from directory and returns them to
1109 the caller but C<.> and C<..> are removed by default.
1111 my @files = read_dir( '/path/to/dir' ) ;
1113 The first argument is the path to the directory to read. If the next
1114 argument is a hash reference, then it is used as the options.
1115 Otherwise the rest of the argument list are is used as key/value
1118 In list context C<read_dir> returns a list of the entries in the
1119 directory. In a scalar context it returns an array reference which has
1124 If the C<err_mode> option is set, it selects how errors are handled (see
1125 C<err_mode> in C<read_file> or C<write_file>).
1129 If this boolean option is set, C<.> and C<..> are not removed from the
1132 my @all_files = read_dir( '/path/to/dir', keep_dot_dot => 1 ) ;
1136 read_file write_file overwrite_file append_file read_dir
1144 An article on file slurping in extras/slurp_article.pod. There is
1145 also a benchmarking script in extras/slurp_bench.pl.
1149 If run under Perl 5.004, slurping from the DATA handle will fail as
1150 that requires B.pm which didn't get into core until 5.005.
1154 Uri Guttman, E<lt>uri AT stemsystems DOT comE<gt>