| 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
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | if (my $size = -s $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | my $buf; | |||||||||||||||||||||||||||
| 22 | 1 | my ($pos, $read) = 0; | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$read and $pos < $size
MC/DC0%0/2
$read and $pos < $size
$read
$pos < $size
Truth table33%1/3
A and B
A= $readB= $pos < $size
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | defined($read = read $fh, ${$buf}, $size - $pos, $pos) or croak "Couldn't read $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$crlf and $crlf eq "auto"
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"
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 6 | ▶ | if (HAS_UTF8_STRICT && $encoding =~ /^utf-?8\b/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | return $crlf ? ':unix:utf8_strict:crlf' : ':unix:utf8_strict'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$encoding ||= "utf-8"
$encoding
Truth table100%2/2
A
A= $encoding
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$encoding ||= "utf-8"
$encoding
Truth table50%1/2
A
A= $encoding
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | print $fh $_[1] or croak "Couldn't write to $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | close $fh or croak "Couldn't write to $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | print $fh $_[1] or croak "Couldn't write to $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | close $fh or croak "Couldn't write to $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$encoding ||= "utf-8"
$encoding
Truth table100%2/2
A
A= $encoding
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | 2 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 90 | 2 | ▶ | return <$fh> if $skip_chomp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||