| 1 | package File::Slurper::Dash; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $DATE = '2019-10-06'; # DATE | |||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.001'; # VERSION | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter 5.57 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Encode qw(:fallbacks); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use PerlIO::encoding; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT_OK = qw/read_binary read_text read_lines write_binary write_text read_dir/; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub read_binary { | ||||||||||||||||||||||||||
|
Subroutine
read_binary: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 0 | my $filename = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my $fh; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | if ($filename eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | $fh = *STDIN; | |||||||||||||||||||||||||||
| 24 | } else { | ||||||||||||||||||||||||||||
| 25 | # This logic is a bit ugly, but gives a significant speed boost | ||||||||||||||||||||||||||||
| 26 | # because slurpy readline is not optimized for non-buffered usage | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | open my $fh, '<:unix', $filename or croak "Couldn't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (my $size = -s $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | my $buf; | |||||||||||||||||||||||||||
| 32 | 0 | my ($pos, $read) = 0; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$read and $pos < $size
MC/DC0%0/2
$read and $pos < $size
$read
$pos < $size
Truth table0%0/3
A and B
A= $readB= $pos < $size
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | defined($read = read $fh, ${$buf}, $size - $pos, $pos) or croak "Couldn't read $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $pos += $read; | |||||||||||||||||||||||||||
| 36 | } while ($read && $pos < $size); | ||||||||||||||||||||||||||||
| 37 | 0 | return ${$buf}; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | 0 | return do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | CRLF_DEFAULT => $^O eq 'MSWin32', | ||||||||||||||||||||||||||||
| 46 | 1 | HAS_UTF8_STRICT => scalar do { local $@; eval { require PerlIO::utf8_strict } }, | |||||||||||||||||||||||||||
| 47 | 1 | }; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _text_layers { | ||||||||||||||||||||||||||
|
Subroutine
_text_layers: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($encoding, $crlf) = @_; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | $crlf = CRLF_DEFAULT if $crlf && $crlf eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$crlf and $crlf eq "auto"
MC/DC0%0/2
$crlf and $crlf eq "auto"
$crlf
$crlf eq "auto"
Truth table0%0/3
A and B
A= $crlfB= $crlf eq "auto"
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($encoding =~ /^(latin|iso-8859-)1$/i) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return $crlf ? ':unix:crlf' : ':raw'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | elsif (HAS_UTF8_STRICT && $encoding =~ /^utf-?8\b/i) { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return $crlf ? ':unix:utf8_strict:crlf' : ':unix:utf8_strict'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | # non-ascii compatible encodings such as UTF-16 need encoding before crlf | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return $crlf ? ":raw:encoding($encoding):crlf" : ":raw:encoding($encoding)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub read_text { | ||||||||||||||||||||||||||
|
Subroutine
read_text: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ($filename, $encoding, $crlf) = @_; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | $encoding ||= 'utf-8'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$encoding ||= "utf-8"
MC/DC0%0/1
$encoding ||= "utf-8"
$encoding
Truth table0%0/2
A
A= $encoding
| |||||||||||||||||||||||||||||
| 68 | 0 | my $layer = _text_layers($encoding, $crlf); | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | return read_binary($filename) if $layer eq ':raw'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | local $PerlIO::encoding::fallback = FB_CROAK; | |||||||||||||||||||||||||||
| 72 | 0 | my $fh; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($filename eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $fh = *STDIN; | |||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | open $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 0 | return do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub write_text { | ||||||||||||||||||||||||||
|
Subroutine
write_text: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 0 | my ($filename, undef, $encoding, $crlf) = @_; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | $encoding ||= 'utf-8'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$encoding ||= "utf-8"
MC/DC0%0/1
$encoding ||= "utf-8"
$encoding
Truth table0%0/2
A
A= $encoding
| |||||||||||||||||||||||||||||
| 84 | 0 | my $layer = _text_layers($encoding, $crlf); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | local $PerlIO::encoding::fallback = FB_CROAK; | |||||||||||||||||||||||||||
| 87 | 0 | my $fh; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($filename eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $fh = *STDOUT; | |||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | open $fh, ">$layer", $filename or croak "Couldn't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | print $fh $_[1] or croak "Couldn't write to $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | close $fh or croak "Couldn't write to $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub write_binary { | ||||||||||||||||||||||||||
|
Subroutine
write_binary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | return write_text(@_[0,1], 'latin-1'); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub read_lines { | ||||||||||||||||||||||||||
|
Subroutine
read_lines: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($filename, $encoding, $crlf, $skip_chomp) = @_; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | $encoding ||= 'utf-8'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$encoding ||= "utf-8"
MC/DC0%0/1
$encoding ||= "utf-8"
$encoding
Truth table0%0/2
A
A= $encoding
| |||||||||||||||||||||||||||||
| 105 | 0 | my $layer = _text_layers($encoding, $crlf); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | local $PerlIO::encoding::fallback = FB_CROAK; | |||||||||||||||||||||||||||
| 108 | 0 | my $fh; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($filename eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $fh = *STDIN; | |||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | open $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | return <$fh> if $skip_chomp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | my @buf = <$fh>; | |||||||||||||||||||||||||||
| 116 | 0 | close $fh; | |||||||||||||||||||||||||||
| 117 | 0 | chomp @buf; | |||||||||||||||||||||||||||
| 118 | 0 | return @buf; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub read_dir { | ||||||||||||||||||||||||||
|
Subroutine
read_dir: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my ($dirname) = @_; | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | opendir my ($dir), $dirname or croak "Could not open $dirname: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | return grep { not m/ \A \.\.? \z /x } readdir $dir; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | 1; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # ABSTRACT: A fork of File::Slurper to grok "-" as stdin/stdout | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||