File Coverage

inc/File/Slurp.pm
Criterion Covered Total %
statement 88 179 49.1
branch 34 92 36.9
condition 5 20 25.0
subroutine 10 13 76.9
pod 4 4 100.0
total 141 308 45.7


line stmt bran cond sub pod time code
1             #line 1
2             package File::Slurp;
3 4     4   3267  
  4         6  
  4         150  
4             use strict;
5 4     4   22  
  4         7  
  4         252  
6 4     4   3692 use Carp ;
  4         35248  
  4         35  
7 4     4   6215 use POSIX qw( :fcntl_h ) ;
  4         11  
  4         2288  
8 4     4   25 use Fcntl qw( :DEFAULT ) ;
  4         9  
  4         1928  
9             use Symbol ;
10              
11             my $is_win32 = $^O =~ /win32/i ;
12              
13             # Install subs for various constants that aren't set in older perls
14             # (< 5.005). Fcntl on old perls uses Exporter to define subs without a
15             # () prototype These can't be overridden with the constant pragma or
16             # we get a prototype mismatch. Hence this less than aesthetically
17             # appealing BEGIN block:
18              
19 4 50   4   12 BEGIN {
  4         28  
20 0         0 unless( eval { defined SEEK_SET() } ) {
  0         0  
21 0         0 *SEEK_SET = sub { 0 };
  0         0  
22 0         0 *SEEK_CUR = sub { 1 };
  0         0  
23             *SEEK_END = sub { 2 };
24             }
25 4 50       11  
  4         17  
26 0         0 unless( eval { defined O_BINARY() } ) {
  0         0  
27 0         0 *O_BINARY = sub { 0 };
  0         0  
28 0         0 *O_RDONLY = sub { 0 };
  0         0  
29             *O_WRONLY = sub { 1 };
30             }
31 4 50       7  
  4         107  
32             unless ( eval { defined O_APPEND() } ) {
33 0 0       0  
    0          
    0          
34 0         0 if ( $^O =~ /olaris/ ) {
  0         0  
35 0         0 *O_APPEND = sub { 8 };
  0         0  
36 0         0 *O_CREAT = sub { 256 };
  0         0  
37             *O_EXCL = sub { 1024 };
38             }
39 0         0 elsif ( $^O =~ /inux/ ) {
  0         0  
40 0         0 *O_APPEND = sub { 1024 };
  0         0  
41 0         0 *O_CREAT = sub { 64 };
  0         0  
42             *O_EXCL = sub { 128 };
43             }
44 0         0 elsif ( $^O =~ /BSD/i ) {
  0         0  
45 0         0 *O_APPEND = sub { 8 };
  0         0  
46 0         0 *O_CREAT = sub { 512 };
  0         0  
47             *O_EXCL = sub { 2048 };
48             }
49             }
50             }
51              
52             # print "OS [$^O]\n" ;
53              
54             # print "O_BINARY = ", O_BINARY(), "\n" ;
55             # print "O_RDONLY = ", O_RDONLY(), "\n" ;
56             # print "O_WRONLY = ", O_WRONLY(), "\n" ;
57             # print "O_APPEND = ", O_APPEND(), "\n" ;
58             # print "O_CREAT ", O_CREAT(), "\n" ;
59             # print "O_EXCL ", O_EXCL(), "\n" ;
60 4     4   25  
  4         7  
  4         390  
61 4     4   23 use base 'Exporter' ;
  4         8  
  4         6108  
62             use vars qw( %EXPORT_TAGS @EXPORT_OK $VERSION @EXPORT ) ;
63              
64             %EXPORT_TAGS = ( 'all' => [
65             qw( read_file write_file overwrite_file append_file read_dir ) ] ) ;
66              
67             @EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
68             @EXPORT_OK = qw( slurp ) ;
69              
70             $VERSION = '9999.13';
71              
72             *slurp = \&read_file ;
73              
74             sub read_file {
75 4     4 1 330  
76             my( $file_name, %args ) = @_ ;
77              
78             # set the buffer to either the passed in one or ours and init it to the null
79             # string
80 4         6  
81 4   50     20 my $buf ;
82 4         5 my $buf_ref = $args{'buf_ref'} || \$buf ;
  4         6  
83             ${$buf_ref} = '' ;
84 4         6  
85             my( $read_fh, $size_left, $blk_size ) ;
86              
87             # check if we are reading from a handle (glob ref or IO:: object)
88 4 50       10  
89             if ( ref $file_name ) {
90              
91             # slurping a handle so use it and don't open anything.
92             # set the block size so we know it is a handle and read that amount
93 0         0  
94 0   0     0 $read_fh = $file_name ;
95 0         0 $blk_size = $args{'blk_size'} || 1024 * 1024 ;
96             $size_left = $blk_size ;
97              
98             # DEEP DARK MAGIC. this checks the UNTAINT IO flag of a
99             # glob/handle. only the DATA handle is untainted (since it is from
100             # trusted data in the source file). this allows us to test if this is
101             # the DATA handle and then to do a sysseek to make sure it gets
102             # slurped correctly. on some systems, the buffered i/o pointer is not
103             # left at the same place as the fd pointer. this sysseek makes them
104             # the same so slurping with sysread will work.
105 0         0  
  0         0  
106             eval{ require B } ;
107 0 0       0  
108             if ( $@ ) {
109 0         0  
110             @_ = ( \%args, <<ERR ) ;
111             Can't find B.pm with this Perl: $!.
112             That module is needed to slurp the DATA handle.
113 0         0 ERR
114             goto &_error ;
115             }
116 0 0       0  
117             if ( B::svref_2object( $read_fh )->IO->IoFLAGS & 16 ) {
118              
119             # set the seek position to the current tell.
120 0 0       0  
121             sysseek( $read_fh, tell( $read_fh ), SEEK_SET ) ||
122             croak "sysseek $!" ;
123             }
124             }
125             else {
126              
127             # a regular file. set the sysopen mode
128 4         5  
129 4 50       9 my $mode = O_RDONLY ;
130             $mode |= O_BINARY if $args{'binmode'} ;
131              
132             #printf "RD: BINARY %x MODE %x\n", O_BINARY, $mode ;
133              
134             # open the file and handle any error
135 4         16  
136 4 50       217 $read_fh = gensym ;
137 0         0 unless ( sysopen( $read_fh, $file_name, $mode ) ) {
138 0         0 @_ = ( \%args, "read_file '$file_name' - sysopen: $!");
139             goto &_error ;
140             }
141              
142             # get the size of the file for use in the read loop
143 4         31  
144             $size_left = -s $read_fh ;
145 4 100       10  
146             unless( $size_left ) {
147 2   50     11  
148 2         3 $blk_size = $args{'blk_size'} || 1024 * 1024 ;
149             $size_left = $blk_size ;
150             }
151             }
152              
153             # infinite read loop. we exit when we are done slurping
154 4         4  
155             while( 1 ) {
156              
157             # do the read and see how much we got
158 4         5  
  4         70  
159 4         5 my $read_cnt = sysread( $read_fh, ${$buf_ref},
160             $size_left, length ${$buf_ref} ) ;
161 4 50       10  
162             if ( defined $read_cnt ) {
163              
164             # good read. see if we hit EOF (nothing left to read)
165 4 100       11  
166             last if $read_cnt == 0 ;
167              
168             # loop if we are slurping a handle. we don't track $size_left then.
169 2 50       5  
170             next if $blk_size ;
171              
172 2         3 # count down how much we read and loop if we have more to read.
173 2 50       5 $size_left -= $read_cnt ;
174 0         0 last if $size_left <= 0 ;
175             next ;
176             }
177              
178             # handle the read error
179 0         0  
180 0         0 @_ = ( \%args, "read_file '$file_name' - sysread: $!");
181             goto &_error ;
182             }
183              
184             # fix up cr/lf to be a newline if this is a windows text file
185 4 50 33     18  
  0         0  
186             ${$buf_ref} =~ s/\015\012/\n/g if $is_win32 && !$args{'binmode'} ;
187              
188             # this is the 5 returns in a row. each handles one possible
189             # combination of caller context and requested return type
190 4         9  
191 4 50 33     27 my $sep = $/ ;
192             $sep = '\n\n+' if defined $sep && $sep eq '' ;
193              
194             # caller wants to get an array ref of lines
195              
196             # this split doesn't work since it tries to use variable length lookbehind
197             # the m// line works.
198 4 0       10 # return [ split( m|(?<=$sep)|, ${$buf_ref} ) ] if $args{'array_ref'} ;
  0 50       0  
  0         0  
199             return [ length(${$buf_ref}) ? ${$buf_ref} =~ /(.*?$sep|.+)/sg : () ]
200             if $args{'array_ref'} ;
201              
202             # caller wants a list of lines (normal list context)
203              
204             # same problem with this split as before.
205 4 0       5 # return split( m|(?<=$sep)|, ${$buf_ref} ) if wantarray ;
  0 50       0  
  0         0  
206             return length(${$buf_ref}) ? ${$buf_ref} =~ /(.*?$sep|.+)/sg : ()
207             if wantarray ;
208              
209             # caller wants a scalar ref to the slurped text
210 4 50       8  
211             return $buf_ref if $args{'scalar_ref'} ;
212              
213             # caller wants a scalar with the slurped text (normal scalar context)
214 4 50       9  
  4         52  
215             return ${$buf_ref} if defined wantarray ;
216              
217             # caller passed in an i/o buffer by reference (normal void context)
218 0         0  
219             return ;
220             }
221              
222             sub write_file {
223 6     6 1 834  
224             my $file_name = shift ;
225              
226             # get the optional argument hash ref from @_ or an empty hash ref.
227 6 50       200  
228             my $args = ( ref $_[0] eq 'HASH' ) ? shift : {} ;
229 6         12  
230             my( $buf_ref, $write_fh, $no_truncate, $orig_file_name, $data_is_ref ) ;
231              
232             # get the buffer ref - it depends on how the data is passed into write_file
233             # after this if/else $buf_ref will have a scalar ref to the data.
234 6 50       366  
    50          
    50          
235             if ( ref $args->{'buf_ref'} eq 'SCALAR' ) {
236              
237             # a scalar ref passed in %args has the data
238             # note that the data was passed by ref
239 0         0  
240 0         0 $buf_ref = $args->{'buf_ref'} ;
241             $data_is_ref = 1 ;
242             }
243             elsif ( ref $_[0] eq 'SCALAR' ) {
244              
245             # the first value in @_ is the scalar ref to the data
246             # note that the data was passed by ref
247 0         0  
248 0         0 $buf_ref = shift ;
249             $data_is_ref = 1 ;
250             }
251             elsif ( ref $_[0] eq 'ARRAY' ) {
252              
253             # the first value in @_ is the array ref to the data so join it.
254 0         0  
  0         0  
  0         0  
255             ${$buf_ref} = join '', @{$_[0]} ;
256             }
257             else {
258              
259             # good old @_ has all the data so join it.
260 6         190  
  6         16  
261             ${$buf_ref} = join '', @_ ;
262             }
263              
264             # see if we were passed a open handle to spew to.
265 6 50       570  
266             if ( ref $file_name ) {
267              
268             # we have a handle. make sure we don't call truncate on it.
269 0         0  
270 0         0 $write_fh = $file_name ;
271             $no_truncate = 1 ;
272             }
273             else {
274              
275             # spew to regular file.
276 6 50       21  
277             if ( $args->{'atomic'} ) {
278              
279             # in atomic mode, we spew to a temp file so make one and save the original
280 0         0 # file name.
281 0         0 $orig_file_name = $file_name ;
282             $file_name .= ".$$" ;
283             }
284              
285             # set the mode for the sysopen
286 6         10  
287 6 50       17 my $mode = O_WRONLY | O_CREAT ;
288 6 50       15 $mode |= O_BINARY if $args->{'binmode'} ;
289 6 50       14 $mode |= O_APPEND if $args->{'append'} ;
290             $mode |= O_EXCL if $args->{'no_clobber'} ;
291              
292             #printf "WR: BINARY %x MODE %x\n", O_BINARY, $mode ;
293              
294             # open the file and handle any error.
295 6         25  
296 6 50       559 $write_fh = gensym ;
297 0         0 unless ( sysopen( $write_fh, $file_name, $mode ) ) {
298 0         0 @_ = ( $args, "write_file '$file_name' - sysopen: $!");
299             goto &_error ;
300             }
301             }
302 6 50       23  
303             sysseek( $write_fh, 0, SEEK_END ) if $args->{'append'} ;
304              
305              
306             #print 'WR before data ', unpack( 'H*', ${$buf_ref}), "\n" ;
307              
308             # fix up newline to write cr/lf if this is a windows text file
309 6 50 33     20  
310             if ( $is_win32 && !$args->{'binmode'} ) {
311              
312             # copy the write data if it was passed by ref so we don't clobber the
313 0 0       0 # caller's data
  0         0  
  0         0  
314 0         0 $buf_ref = \do{ my $copy = ${$buf_ref}; } if $data_is_ref ;
  0         0  
315             ${$buf_ref} =~ s/\n/\015\012/g ;
316             }
317              
318             #print 'after data ', unpack( 'H*', ${$buf_ref}), "\n" ;
319              
320             # get the size of how much we are writing and init the offset into that buffer
321 6         10  
  6         13  
322 6         9 my $size_left = length( ${$buf_ref} ) ;
323             my $offset = 0 ;
324              
325             # loop until we have no more data left to write
326 6         11  
327             do {
328              
329             # do the write and track how much we just wrote
330 6         10  
  6         145  
331             my $write_cnt = syswrite( $write_fh, ${$buf_ref},
332             $size_left, $offset ) ;
333 6 50       21  
334             unless ( defined $write_cnt ) {
335              
336 0         0 # the write failed
337 0         0 @_ = ( $args, "write_file '$file_name' - syswrite: $!");
338             goto &_error ;
339             }
340              
341             # track much left to write and where to write from in the buffer
342 6         9  
343 6         30 $size_left -= $write_cnt ;
344             $offset += $write_cnt ;
345              
346             } while( $size_left > 0 ) ;
347              
348             # we truncate regular files in case we overwrite a long file with a shorter file
349             # so seek to the current position to get it (same as tell()).
350 6 50       184  
351             truncate( $write_fh,
352             sysseek( $write_fh, 0, SEEK_CUR ) ) unless $no_truncate ;
353 6         63  
354             close( $write_fh ) ;
355              
356             # handle the atomic mode - move the temp file to the original filename.
357 6 50       20  
358             rename( $file_name, $orig_file_name ) if $args->{'atomic'} ;
359 6         30  
360             return 1 ;
361             }
362              
363             # this is for backwards compatibility with the previous File::Slurp module.
364             # write_file always overwrites an existing file
365              
366             *overwrite_file = \&write_file ;
367              
368             # the current write_file has an append mode so we use that. this
369             # supports the same API with an optional second argument which is a
370             # hash ref of options.
371              
372             sub append_file {
373              
374 0     0 1   # get the optional args hash ref
375 0 0         my $args = $_[1] ;
376             if ( ref $args eq 'HASH' ) {
377              
378             # we were passed an args ref so just mark the append mode
379 0            
380             $args->{append} = 1 ;
381             }
382             else {
383              
384             # no args hash so insert one with the append mode
385 0            
386             splice( @_, 1, 0, { append => 1 } ) ;
387             }
388              
389             # magic goto the main write_file sub. this overlays the sub without touching
390             # the stack or @_
391 0            
392             goto &write_file
393             }
394              
395             # basic wrapper around opendir/readdir
396              
397             sub read_dir {
398 0     0 1    
399             my ($dir, %args ) = @_;
400              
401             # this handle will be destroyed upon return
402 0            
403             local(*DIRH);
404              
405             # open the dir and handle any errors
406 0 0          
407             unless ( opendir( DIRH, $dir ) ) {
408 0            
409 0           @_ = ( \%args, "read_dir '$dir' - opendir: $!" ) ;
410             goto &_error ;
411             }
412 0            
413             my @dir_entries = readdir(DIRH) ;
414 0 0 0        
415             @dir_entries = grep( $_ ne "." && $_ ne "..", @dir_entries )
416             unless $args{'keep_dot_dot'} ;
417 0 0          
418 0           return @dir_entries if wantarray ;
419             return \@dir_entries ;
420             }
421              
422             # error handling section
423             #
424             # all the error handling uses magic goto so the caller will get the
425             # error message as if from their code and not this module. if we just
426             # did a call on the error code, the carp/croak would report it from
427             # this module since the error sub is one level down on the call stack
428             # from read_file/write_file/read_dir.
429              
430              
431             my %err_func = (
432             'carp' => \&carp,
433             'croak' => \&croak,
434             ) ;
435              
436             sub _error {
437 0     0      
438             my( $args, $err_msg ) = @_ ;
439              
440             # get the error function to use
441 0   0        
442             my $func = $err_func{ $args->{'err_mode'} || 'croak' } ;
443              
444             # if we didn't find it in our error function hash, they must have set
445             # it to quiet and we don't do anything.
446 0 0          
447             return unless $func ;
448              
449             # call the carp/croak function
450 0            
451             $func->($err_msg) ;
452              
453             # return a hard undef (in list context this will be a single value of
454             # undef which is not a legal in-band value)
455 0            
456             return undef ;
457             }
458              
459             1;
460             __END__
461              
462             #line 744