File Coverage

blib/lib/File/Slurper.pm
Criterion Covered Total %
statement 71 79 89.8
branch 16 34 47.0
condition 7 12 58.3
subroutine 13 14 92.8
pod 6 6 100.0
total 113 145 77.9


line stmt bran cond sub pod time code
1             package File::Slurper;
2             $File::Slurper::VERSION = '0.014';
3 2     2   73955 use strict;
  2         8  
  2         58  
4 2     2   10 use warnings;
  2         5  
  2         94  
5              
6 2     2   12 use Carp 'croak';
  2         5  
  2         136  
7 2     2   14 use Exporter 5.57 'import';
  2         40  
  2         91  
8              
9 2     2   1140 use Encode 2.11 qw/FB_CROAK STOP_AT_PARTIAL/;
  2         21025  
  2         136  
10 2     2   897 use PerlIO::encoding;
  2         826  
  2         511  
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       38 open my $fh, '<:unix', $filename or croak "Couldn't open $filename: $!";
20 1 50       13 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         15  
25 1         8 $pos += $read;
26             } while ($read && $pos < $size);
27 1         3 return ${$buf};
  1         18  
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         893  
37 2     2   16 };
  2         3  
38              
39             sub _text_layers {
40 6     6   15 my ($encoding, $crlf) = @_;
41 6 50 33     48 $crlf = CRLF_DEFAULT if $crlf && $crlf eq 'auto';
42              
43 6 100       43 if (HAS_UTF8_STRICT && $encoding =~ /^utf-?8\b/i) {
44 5 50       16 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 616 my ($filename, $encoding, $crlf) = @_;
54 3   100     19 $encoding ||= 'utf-8';
55 3         10 my $layer = _text_layers($encoding, $crlf);
56              
57 3         8 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
58 3 50       146 open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
59 3         68 return scalar do { local $/; <$fh> };
  3         23  
  3         212  
60             }
61              
62             sub write_text {
63 1     1 1 651 my ($filename, undef, $encoding, $crlf) = @_;
64 1   50     7 $encoding ||= 'utf-8';
65 1         4 my $layer = _text_layers($encoding, $crlf);
66              
67 1         2 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
68 1 50       62 open my $fh, ">$layer", $filename or croak "Couldn't open $filename: $!";
69 1 50       24 print $fh $_[1] or croak "Couldn't write to $filename: $!";
70 1 50       147 close $fh or croak "Couldn't write to $filename: $!";
71 1         9 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 7 my ($filename, $encoding, $crlf, $skip_chomp) = @_;
84 2   100     11 $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       113 open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
89 2         15 local $/ = "\n";
90 2 100       74 return <$fh> if $skip_chomp;
91 1         45 my @buf = <$fh>;
92 1         13 close $fh;
93 1         6 chomp @buf;
94 1         22 return @buf;
95             }
96              
97             sub read_dir {
98 1     1 1 10 my ($dirname) = @_;
99 1 50       55 opendir my ($dir), $dirname or croak "Could not open $dirname: $!";
100 1         41 return grep { not m/ \A \.\.? \z /x } readdir $dir;
  3         50  
101             }
102              
103             1;
104              
105             # ABSTRACT: A simple, sane and efficient module to slurp a file
106              
107             __END__