line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Slurper; |
2
|
|
|
|
|
|
|
$File::Slurper::VERSION = '0.013'; |
3
|
2
|
|
|
2
|
|
72933
|
use strict; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
59
|
|
4
|
2
|
|
|
2
|
|
19
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
54
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
9
|
use Carp 'croak'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
156
|
|
7
|
2
|
|
|
2
|
|
14
|
use Exporter 5.57 'import'; |
|
2
|
|
|
|
|
105
|
|
|
2
|
|
|
|
|
98
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
1221
|
use Encode 2.11 qw/FB_CROAK STOP_AT_PARTIAL/; |
|
2
|
|
|
|
|
22134
|
|
|
2
|
|
|
|
|
174
|
|
10
|
2
|
|
|
2
|
|
930
|
use PerlIO::encoding; |
|
2
|
|
|
|
|
919
|
|
|
2
|
|
|
|
|
502
|
|
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
|
|
|
|
39
|
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
|
|
|
|
14
|
defined($read = read $fh, ${$buf}, $size - $pos, $pos) or croak "Couldn't read $filename: $!"; |
|
1
|
|
|
|
|
18
|
|
25
|
1
|
|
|
|
|
8
|
$pos += $read; |
26
|
|
|
|
|
|
|
} while ($read && $pos < $size); |
27
|
1
|
|
|
|
|
2
|
return ${$buf}; |
|
1
|
|
|
|
|
19
|
|
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
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
912
|
|
37
|
2
|
|
|
2
|
|
17
|
}; |
|
2
|
|
|
|
|
5
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _text_layers { |
40
|
6
|
|
|
6
|
|
16
|
my ($encoding, $crlf) = @_; |
41
|
6
|
50
|
33
|
|
|
28
|
$crlf = CRLF_DEFAULT if $crlf && $crlf eq 'auto'; |
42
|
|
|
|
|
|
|
|
43
|
6
|
100
|
|
|
|
41
|
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
|
|
|
|
6
|
return $crlf ? ":raw:encoding($encoding):crlf" : ":raw:encoding($encoding)"; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub read_text { |
53
|
3
|
|
|
3
|
1
|
611
|
my ($filename, $encoding, $crlf) = @_; |
54
|
3
|
|
100
|
|
|
18
|
$encoding ||= 'utf-8'; |
55
|
3
|
|
|
|
|
13
|
my $layer = _text_layers($encoding, $crlf); |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
8
|
local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK; |
58
|
3
|
50
|
|
|
|
138
|
open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; |
59
|
3
|
|
|
|
|
61
|
return do { local $/; <$fh> }; |
|
3
|
|
|
|
|
19
|
|
|
3
|
|
|
|
|
202
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub write_text { |
63
|
1
|
|
|
1
|
1
|
608
|
my ($filename, undef, $encoding, $crlf) = @_; |
64
|
1
|
|
50
|
|
|
7
|
$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
|
|
|
|
62
|
open my $fh, ">$layer", $filename or croak "Couldn't open $filename: $!"; |
69
|
1
|
50
|
|
|
|
11
|
print $fh $_[1] or croak "Couldn't write to $filename: $!"; |
70
|
1
|
50
|
|
|
|
108
|
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
|
8
|
my ($filename, $encoding, $crlf, $skip_chomp) = @_; |
84
|
2
|
|
100
|
|
|
11
|
$encoding ||= 'utf-8'; |
85
|
2
|
|
|
|
|
6
|
my $layer = _text_layers($encoding, $crlf); |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
5
|
local $PerlIO::encoding::fallback = STOP_AT_PARTIAL | FB_CROAK; |
88
|
2
|
50
|
|
|
|
89
|
open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; |
89
|
2
|
|
|
|
|
13
|
local $/ = "\n"; |
90
|
2
|
100
|
|
|
|
73
|
return <$fh> if $skip_chomp; |
91
|
1
|
|
|
|
|
53
|
my @buf = <$fh>; |
92
|
1
|
|
|
|
|
25
|
close $fh; |
93
|
1
|
|
|
|
|
7
|
chomp @buf; |
94
|
1
|
|
|
|
|
19
|
return @buf; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub read_dir { |
98
|
1
|
|
|
1
|
1
|
3
|
my ($dirname) = @_; |
99
|
1
|
50
|
|
|
|
46
|
opendir my ($dir), $dirname or croak "Could not open $dirname: $!"; |
100
|
1
|
|
|
|
|
47
|
return grep { not m/ \A \.\.? \z /x } readdir $dir; |
|
3
|
|
|
|
|
36
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# ABSTRACT: A simple, sane and efficient module to slurp a file |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |