blib/lib/File/Slurper.pm

filter: Statementstmt 90.2% 74 / 82 Branchbran 47.2% 17 / 36 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 92.8% 13 / 14 Podpod 100% 6 / 6 totaltotal 75.8% 119 / 157 CC 24 SCAR 18.3
CC
24
Cov
75%
write_binary
20.0
read_binary
7.7
_text_layers
7.1
CRAP 6.3
↑ Summary
1 package File::Slurper;
2 2 $File::Slurper::VERSION = '0.014';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Encode 2.11 qw/FB_CROAK STOP_AT_PARTIAL/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use PerlIO::encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @EXPORT_OK = qw/read_binary read_text read_lines write_binary write_text read_dir/;
13  
14 1 ▶sub read_binary {
Subroutine read_binary: called CC=6 SCAR=20.4
Pod documented
15 1 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 ▶ open my $fh, '<:unix', $filename or croak "Couldn't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:unix", $filename or croak("Couldn't open ${filename}: $!") 1 0 1
20 1 ▶ if (my $size = -s $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $size = -s $fh) { } 1 0 1
21 1 my $buf;
22 1 my ($pos, $read) = 0;
23 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$read and $pos < $size and do { do { defined($read = read($fh, ${$buf;}, $size - $pos, $pos)) or croak("Couldn't read ${filename}: $!"); $pos += $read } } 0 1 1
Condition33%1/3
$read and $pos < $size
!ll&&!rl&&r
010
MC/DC0%0/2
$read and $pos < $size
$read $pos < $size
Truth table33%1/3
A and B
A= $readB= $pos < $size
ABresult
0X0
100
111
24 1 ▶ defined($read = read $fh, ${$buf}, $size - $pos, $pos) or croak "Couldn't read $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined($read = read($fh, ${$buf;}, $size - $pos, $pos)) or croak("Couldn't read ${filename}: $!") 1 0 1
25 1 $pos += $read;
26 } while ($read && $pos < $size);
27 1 return ${$buf};
28 }
29 else {
30 0 return do { local $/; <$fh> };
31 }
32 }
33  
34 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 CRLF_DEFAULT => $^O eq 'MSWin32',
36 2 HAS_UTF8_STRICT => scalar do { local $@; eval { require PerlIO::utf8_strict } },
37 2 };
38  
39 6 ▶sub _text_layers {
Subroutine _text_layers: called CC=6 SCAR=19.6
40 6 my ($encoding, $crlf) = @_;
41 6 ▶ $crlf = CRLF_DEFAULT if $crlf && $crlf eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $crlf and $crlf eq "auto" 0 6 6
Condition33%1/3
$crlf and $crlf eq "auto"
!ll&&!rl&&r
600
MC/DC0%0/2
$crlf and $crlf eq "auto"
$crlf $crlf eq "auto"
Truth table33%1/3
A and B
A= $crlfB= $crlf eq "auto"
ABresult
0X0
100
111
42  
43 6 ▶ if (HAS_UTF8_STRICT && $encoding =~ /^utf-?8\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /^utf-?8\b/i) { } 5 1 6
44 5 ▶ return $crlf ? ':unix:utf8_strict:crlf' : ':unix:utf8_strict';
Branch: 1 branch
BranchTrueFalseTotal
$crlf ? : 0 5 5
45 }
46 else {
47 # non-ascii compatible encodings such as UTF-16 need encoding before crlf
48 1 ▶ return $crlf ? ":raw:encoding($encoding):crlf" : ":raw:encoding($encoding)";
Branch: 1 branch
BranchTrueFalseTotal
$crlf ? : 0 1 1
49 }
50 }
51  
52 3 ▶sub read_text {
Subroutine read_text: called CC=3 SCAR=11.0
Pod documented
53 3 my ($filename, $encoding, $crlf) = @_;
54 3 ▶ $encoding ||= 'utf-8';
Condition100%2/2
$encoding ||= "utf-8"
l!l
12
MC/DC100%1/1
$encoding ||= "utf-8"
$encoding
Truth table100%2/2
A
A= $encoding
Aresult
11
00
55 3 my $layer = _text_layers($encoding, $crlf);
56  
57 3 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
58 3 ▶ open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$layer", $filename or croak("Couldn't open ${filename}: $!") 3 0 3
59 3 return scalar do { local $/; <$fh> };
60 }
61  
62 1 ▶sub write_text {
Subroutine write_text: called CC=5 SCAR=16.8
Pod documented
63 1 my ($filename, undef, $encoding, $crlf) = @_;
64 1 ▶ $encoding ||= 'utf-8';
Condition50%1/2
$encoding ||= "utf-8"
l!l
01
MC/DC0%0/1
$encoding ||= "utf-8"
$encoding
Truth table50%1/2
A
A= $encoding
Aresult
11
00
65 1 my $layer = _text_layers($encoding, $crlf);
66  
67 1 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
68 1 ▶ open my $fh, ">$layer", $filename or croak "Couldn't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$layer", $filename or croak("Couldn't open ${filename}: $!") 1 0 1
69 1 ▶ print $fh $_[1] or croak "Couldn't write to $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $_[1] or croak("Couldn't write to ${filename}: $!") 1 0 1
70 1 ▶ close $fh or croak "Couldn't write to $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Couldn't write to ${filename}: $!") 1 0 1
71 1 return;
72 }
73  
74 0 ▶sub write_binary {
Subroutine write_binary: not called CC=4 SCAR=30.0
Pod documented
75 0 my $filename = $_[0];
76 0 ▶ open my $fh, ">:raw", $filename or croak "Couldn't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $filename or croak("Couldn't open ${filename}: $!") 0 0 0
77 0 ▶ print $fh $_[1] or croak "Couldn't write to $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $_[1] or croak("Couldn't write to ${filename}: $!") 0 0 0
78 0 ▶ close $fh or croak "Couldn't write to $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Couldn't write to ${filename}: $!") 0 0 0
79 0 return;
80 }
81  
82 2 ▶sub read_lines {
Subroutine read_lines: called CC=4 SCAR=13.9
Pod documented
83 2 my ($filename, $encoding, $crlf, $skip_chomp) = @_;
84 2 ▶ $encoding ||= 'utf-8';
Condition100%2/2
$encoding ||= "utf-8"
l!l
11
MC/DC100%1/1
$encoding ||= "utf-8"
$encoding
Truth table100%2/2
A
A= $encoding
Aresult
11
00
85 2 my $layer = _text_layers($encoding, $crlf);
86  
87 2 local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK;
88 2 ▶ open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$layer", $filename or croak("Couldn't open ${filename}: $!") 2 0 2
89 2 local $/ = "\n";
90 2 ▶ return <$fh> if $skip_chomp;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_chomp 1 1 2
91 1 my @buf = <$fh>;
92 1 close $fh;
93 1 chomp @buf;
94 1 return @buf;
95 }
96  
97 1 ▶sub read_dir {
Subroutine read_dir: called CC=2 SCAR=7.0
Pod documented
98 1 my ($dirname) = @_;
99 1 ▶ opendir my ($dir), $dirname or croak "Could not open $dirname: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $dirname or croak("Could not open ${dirname}: $!") 1 0 1
100 1 return grep { not m/ \A \.\.? \z /x } readdir $dir;
101 }
102  
103 2 1;
104  
105 # ABSTRACT: A simple, sane and efficient module to slurp a file
106  
107 __END__
↑ Summary