File Coverage

blib/lib/File/Slurp/Tiny.pm
Criterion Covered Total %
statement 57 61 93.4
branch 23 34 67.6
condition 7 14 50.0
subroutine 10 10 100.0
pod 4 4 100.0
total 101 123 82.1


line stmt bran cond sub pod time code
1             package File::Slurp::Tiny;
2             $File::Slurp::Tiny::VERSION = '0.004';
3 1     1   3418 use strict;
  1         2  
  1         41  
4 1     1   8 use warnings;
  1         2  
  1         45  
5              
6 1     1   6 use Carp 'croak';
  1         8  
  1         95  
7 1     1   6 use Exporter 5.57 'import';
  1         44  
  1         64  
8 1     1   8 use File::Spec::Functions 'catfile';
  1         1  
  1         62  
9 1     1   1016 use FileHandle;
  1         14159  
  1         9  
10             our @EXPORT_OK = qw/read_file read_lines write_file read_dir/;
11              
12             my $default_layer = $^O eq 'MSWin32' ? ':crlf' : ':unix';
13              
14             sub read_file {
15 6     6 1 438 my ($filename, %options) = @_;
16 6   66     35 my $layer = $options{binmode} || $default_layer;
17 6 100       20 my $buf_ref = defined $options{buf_ref} ? $options{buf_ref} : \my $buf;
18              
19 6 50       218 open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
20 6 50       47 if (my $size = -s $fh) {
21 6         16 my ($pos, $read) = 0;
22 6   33     8 do {
23 6 50       9 defined($read = read $fh, ${$buf_ref}, $size - $pos, $pos) or croak "Couldn't read $filename: $!";
  6         76  
24 6         41 $pos += $read;
25             } while ($read && $pos < $size);
26             }
27             else {
28 0         0 ${$buf_ref} = do { local $/; <$fh> };
  0         0  
  0         0  
  0         0  
29             }
30 6         39 close $fh;
31 6 100 66     33 return if not defined wantarray or $options{buf_ref};
32 5 100       104 return $options{scalar_ref} ? $buf_ref : $buf;
33             }
34              
35             sub read_lines {
36 2     2 1 607 my ($filename, %options) = @_;
37 2   50     16 my $layer = delete $options{binmode} || ':';
38            
39 2 50       79 open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
40 2 100       56 return <$fh> if not %options;
41 1         38 my @buf = <$fh>;
42 1         12 close $fh;
43 1 50       7 chomp @buf if $options{chomp};
44 1 50       22 return $options{array_ref} ? \@buf : @buf;
45             }
46              
47             sub write_file {
48 2     2 1 791 my ($filename, undef, %options) = @_;
49 2   33     17 my $layer = $options{binmode} || $default_layer;
50 2 100       6 my $mode = $options{append} ? '>>' : '>';
51 2 50       9 my $buf_ref = defined $options{buf_ref} ? $options{buf_ref} : \$_[1];
52              
53 2 50       122 open my $fh, $mode.$layer, $filename or croak "Couldn't open $filename: $!";
54 2         21 $fh->autoflush(1);
55 2 50       107 print $fh ${$buf_ref} or croak "Couldn't write to $filename: $!";
  2         65  
56 2 50       22 close $fh or croak "Couldn't close $filename: $!";
57 2         11 return;
58             }
59              
60             sub read_dir {
61 2     2 1 8 my ($dirname, %options) = @_;
62 2 50       73 opendir my ($dir), $dirname or croak "Could not open $dirname: $!";
63 2         29 my @ret = grep { not m/ \A \.\.? \z /x } readdir $dir;
  6         27  
64 2 100       10 @ret = map { catfile($dirname, $_) } @ret if $options{prefix};
  1         11  
65 2         25 closedir $dir;
66 2         26 return @ret;
67             }
68              
69             1;
70              
71             # ABSTRACT: A simple, sane and efficient file slurper [DISCOURAGED]
72              
73             __END__
74              
75             =pod
76              
77             =encoding UTF-8
78              
79             =head1 NAME
80              
81             File::Slurp::Tiny - A simple, sane and efficient file slurper [DISCOURAGED]
82              
83             =head1 VERSION
84              
85             version 0.004
86              
87             =head1 SYNOPSIS
88              
89             use File::Slurp::Tiny 'read_file';
90             my $content = read_file($filename);
91              
92             =head1 DISCOURAGED
93              
94             B<This module is discouraged in favor of L<File::Slurper|File::Slurper>>. While a useful experiment, it turned out to be both too similar to File::Slurp (still containing most problematic features of File::Slurp's interface) and yet not similar enough to be a true drop-in replacement.
95              
96             Bugs will still be fixed, but new features will probably not be added.
97              
98             =head1 DESCRIPTION
99              
100             This module provides functions for fast and correct slurping and spewing. All functions are optionally exported.
101              
102             =head1 FUNCTIONS
103              
104             =head2 read_file($filename, %options)
105              
106             Reads file C<$filename> into a scalar. By default it returns this scalar. Can optionally take these named arguments:
107              
108             =over 4
109              
110             =item * binmode
111              
112             Set the layers to read the file with. The default will be something sensible on your platform.
113              
114             =item * buf_ref
115              
116             Pass a reference to a scalar to read the file into, instead of returning it by value. This has performance benefits.
117              
118             =item * scalar_ref
119              
120             If set to true, C<read_file> will return a reference to a scalar containing the file content.
121              
122             =back
123              
124             =head2 read_lines($filename, %options)
125              
126             Reads file C<$filename> into a list/array. By default it returns this list. Can optionally take these named arguments:
127              
128             =over 4
129              
130             =item * binmode
131              
132             Set the layers to read the file with. The default will be something sensible on your platform.
133              
134             =item * array_ref
135              
136             Pass a reference to an array to read the lines into, instead of returning them by value. This has performance benefits.
137              
138             =item * chomp
139              
140             C<chomp> the lines.
141              
142             =back
143              
144             =head2 write_file($filename, $content, %options)
145              
146             Open C<$filename>, and write C<$content> to it. Can optionally take this named argument:
147              
148             =over 4
149              
150             =item * binmode
151              
152             Set the layers to write the file with. The default will be something sensible on your platform.
153              
154             =back
155              
156             =head2 read_dir($dirname, %options)
157              
158             Open C<dirname> and return all entries except C<.> and C<..>. Can optionally take this named argument:
159              
160             =over 4
161              
162             =item * prefix
163              
164             This will prepend C<$dir> to the entries
165              
166             =back
167              
168             =head1 SEE ALSO
169              
170             =over 4
171              
172             =item * L<Path::Tiny>
173              
174             A minimalistic abstraction not only around
175              
176             =item * L<File::Slurp>
177              
178             Another file slurping tool.
179              
180             =back
181              
182             =head1 AUTHOR
183              
184             Leon Timmermans <leont@cpan.org>
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188             This software is copyright (c) 2013 by Leon Timmermans.
189              
190             This is free software; you can redistribute it and/or modify it under
191             the same terms as the Perl 5 programming language system itself.
192              
193             =cut