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