File Coverage

blib/lib/File/Slurper.pm
Criterion Covered Total %
statement 70 78 89.7
branch 16 34 47.0
condition 7 12 58.3
subroutine 13 14 92.8
pod 6 6 100.0
total 112 144 77.7


line stmt bran cond sub pod time code
1             package File::Slurper;
2             $File::Slurper::VERSION = '0.012';
3 2     2   79357 use strict;
  2         10  
  2         55  
4 2     2   11 use warnings;
  2         2  
  2         50  
5              
6 2     2   10 use Carp 'croak';
  2         2  
  2         116  
7 2     2   11 use Exporter 5.57 'import';
  2         41  
  2         71  
8              
9 2     2   1091 use Encode 2.11 qw/FB_CROAK STOP_AT_PARTIAL/;
  2         20303  
  2         206  
10 2     2   901 use PerlIO::encoding;
  2         870  
  2         494  
11              
12             our @EXPORT_OK = qw/read_binary read_text read_lines write_binary write_text read_dir/;
13              
14             sub read_binary {
15 1     1 1 3 my $filename = shift;
16              
17             # This logic is a bit ugly, but gives a significant speed boost
18             # because slurpy readline is not optimized for non-buffered usage
19 1 50       32 open my $fh, '<:unix', $filename or croak "Couldn't open $filename: $!";
20 1 50       12 if (my $size = -s $fh) {
21 1         2 my $buf;
22 1         3 my ($pos, $read) = 0;
23 1   33     9 do {
24 1 50       6 defined($read = read $fh, ${$buf}, $size - $pos, $pos) or croak "Couldn't read $filename: $!";
  1         13  
25 1         7 $pos += $read;
26             } while ($read && $pos < $size);
27 1         2 return ${$buf};
  1         13  
28             }
29             else {
30 0         0 return do { local $/; <$fh> };
  0         0  
  0         0  
31             }
32             }
33              
34             use constant {
35             CRLF_DEFAULT => $^O eq 'MSWin32',
36 2         7 HAS_UTF8_STRICT => scalar do { local $@; eval { require PerlIO::utf8_strict } },
  2         5  
  2         4  
  2         924  
37 2     2   18 };
  2         6  
38              
39             sub _text_layers {
40 6     6   13 my ($encoding, $crlf) = @_;
41 6 50 33     16 $crlf = CRLF_DEFAULT if $crlf && $crlf eq 'auto';
42              
43 6 100       35 if (HAS_UTF8_STRICT && $encoding =~ /^utf-?8\b/i) {
44 5 50       15 return $crlf ? ':unix:utf8_strict:crlf' : ':unix:utf8_strict';
45             }
46             else {
47             # non-ascii compatible encodings such as UTF-16 need encoding before crlf
48 1 50       7 return $crlf ? ":raw:encoding($encoding):crlf" : ":raw:encoding($encoding)";
49             }
50             }
51              
52             sub read_text {
53 3     3 1 527 my ($filename, $encoding, $crlf) = @_;
54 3   100     16 $encoding ||= 'utf-8';
55 3         11 my $layer = _text_layers($encoding, $crlf);
56              
57 3         16 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
58 3 50       119 open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
59 3         58 return do { local $/; <$fh> };
  3         17  
  3         169  
60             }
61              
62             sub write_text {
63 1     1 1 563 my ($filename, undef, $encoding, $crlf) = @_;
64 1   50     6 $encoding ||= 'utf-8';
65 1         3 my $layer = _text_layers($encoding, $crlf);
66              
67 1         3 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
68 1 50       55 open my $fh, ">$layer", $filename or croak "Couldn't open $filename: $!";
69 1 50       9 print $fh $_[1] or croak "Couldn't write to $filename: $!";
70 1 50       117 close $fh or croak "Couldn't write to $filename: $!";
71 1         7 return;
72             }
73              
74             sub write_binary {
75 0     0 1 0 my $filename = $_[0];
76 0 0       0 open my $fh, ">:raw", $filename or croak "Couldn't open $filename: $!";
77 0 0       0 print $fh $_[1] or croak "Couldn't write to $filename: $!";
78 0 0       0 close $fh or croak "Couldn't write to $filename: $!";
79 0         0 return;
80             }
81              
82             sub read_lines {
83 2     2 1 8 my ($filename, $encoding, $crlf, $skip_chomp) = @_;
84 2   100     10 $encoding ||= 'utf-8';
85 2         5 my $layer = _text_layers($encoding, $crlf);
86              
87 2         5 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
88 2 50       73 open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
89 2 100       57 return <$fh> if $skip_chomp;
90 1         35 my @buf = <$fh>;
91 1         9 close $fh;
92 1         5 chomp @buf;
93 1         13 return @buf;
94             }
95              
96             sub read_dir {
97 1     1 1 3 my ($dirname) = @_;
98 1 50       38 opendir my ($dir), $dirname or croak "Could not open $dirname: $!";
99 1         31 return grep { not m/ \A \.\.? \z /x } readdir $dir;
  3         29  
100             }
101              
102             1;
103              
104             # ABSTRACT: A simple, sane and efficient module to slurp a file
105              
106             __END__