line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Slurp::Sane; |
2
|
|
|
|
|
|
|
$File::Slurp::Sane::VERSION = '0.001'; |
3
|
1
|
|
|
1
|
|
2577
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use Carp 'croak'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
62
|
|
7
|
1
|
|
|
1
|
|
968
|
use Encode 'resolve_alias'; |
|
1
|
|
|
|
|
14579
|
|
|
1
|
|
|
|
|
772
|
|
8
|
1
|
|
|
1
|
|
10
|
use Exporter 5.57 'import'; |
|
1
|
|
|
|
|
26
|
|
|
1
|
|
|
|
|
30
|
|
9
|
1
|
|
|
1
|
|
5
|
use File::Spec::Functions 'catfile'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
10
|
1
|
|
|
1
|
|
2743
|
use FileHandle; |
|
1
|
|
|
|
|
13802
|
|
|
1
|
|
|
|
|
8
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw/read_binary read_text read_lines read_dir/; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub read_binary { |
14
|
1
|
|
|
1
|
1
|
3
|
my $filename = shift; |
15
|
1
|
|
|
|
|
1
|
my $buf; |
16
|
|
|
|
|
|
|
|
17
|
1
|
50
|
|
|
|
34
|
open my $fh, '<:unix', $filename or croak "Couldn't open $filename: $!"; |
18
|
1
|
50
|
|
|
|
11
|
if (my $size = -s $fh) { |
19
|
1
|
|
|
|
|
4
|
my ($pos, $read) = 0; |
20
|
1
|
|
33
|
|
|
2
|
do { |
21
|
1
|
50
|
|
|
|
15
|
defined($read = read $fh, $buf, $size - $pos, $pos) or croak "Couldn't read $filename: $!"; |
22
|
1
|
|
|
|
|
7
|
$pos += $read; |
23
|
|
|
|
|
|
|
} while ($read && $pos < $size); |
24
|
1
|
|
|
|
|
13
|
return $buf; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
else { |
27
|
0
|
|
|
|
|
0
|
return do { local $/; <$fh> }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $crlf_default = $^O eq 'MSWin32' ? 1 : 0; |
32
|
|
|
|
|
|
|
my $has_utf8_strict = eval { require PerlIO::utf8_strict }; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _text_layers { |
35
|
3
|
|
|
3
|
|
5
|
my ($encoding, $options) = @_; |
36
|
3
|
50
|
|
|
|
11
|
my $crlf = exists $options->{crlf} ? !! delete $options->{crlf} : $crlf_default; |
37
|
3
|
50
|
33
|
|
|
28
|
if ($encoding =~ /^(latin|iso-8859-)1$/i) { |
|
|
50
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
0
|
return $crlf ? ':unix:crlf:perlio' : ':raw' |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
elsif ($has_utf8_strict && $encoding =~ /^utf-?8\b/) { |
41
|
3
|
50
|
|
|
|
12
|
return $crlf ? ':unix:utf8_strict:crlf:perlio' : ':unix:utf8_strict:perlio'; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
0
|
0
|
|
|
|
0
|
return $crlf ? ":unix:perlio:encoding($encoding):crlf:perlio" : ":raw:encoding($encoding)"; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub read_text { |
49
|
1
|
|
|
1
|
1
|
111
|
my ($filename, $encoding, %options) = @_; |
50
|
1
|
|
50
|
|
|
7
|
$encoding ||= 'utf-8'; |
51
|
1
|
|
|
|
|
5
|
my $layer = _text_layers($encoding, \%options); |
52
|
1
|
50
|
|
|
|
7
|
return read_binary($filename) if $layer eq ':raw'; |
53
|
|
|
|
|
|
|
|
54
|
1
|
50
|
|
|
|
48
|
open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; |
55
|
1
|
|
|
|
|
1
|
return do { local $/; <$fh> }; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
51
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub read_lines { |
59
|
2
|
|
|
2
|
1
|
7
|
my ($filename, $encoding, %options) = @_; |
60
|
2
|
|
100
|
|
|
9
|
$encoding ||= 'utf-8'; |
61
|
2
|
|
|
|
|
4
|
my $layer = _text_layers($encoding, \%options); |
62
|
|
|
|
|
|
|
|
63
|
2
|
50
|
|
|
|
71
|
open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; |
64
|
2
|
100
|
|
|
|
72
|
return <$fh> if not %options; |
65
|
1
|
|
|
|
|
54
|
my @buf = <$fh>; |
66
|
1
|
|
|
|
|
10
|
close $fh; |
67
|
1
|
50
|
|
|
|
5
|
chomp @buf if $options{chomp}; |
68
|
1
|
|
|
|
|
12
|
return @buf; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub read_dir { |
72
|
2
|
|
|
2
|
1
|
4
|
my ($dirname, %options) = @_; |
73
|
2
|
50
|
|
|
|
61
|
opendir my ($dir), $dirname or croak "Could not open $dirname: $!"; |
74
|
2
|
|
|
|
|
26
|
my @ret = grep { not m/ \A \.\.? \z /x } readdir $dir; |
|
6
|
|
|
|
|
19
|
|
75
|
2
|
100
|
|
|
|
7
|
@ret = map { catfile($dirname, $_) } @ret if $options{prefix}; |
|
1
|
|
|
|
|
11
|
|
76
|
2
|
|
|
|
|
20
|
closedir $dir; |
77
|
2
|
|
|
|
|
18
|
return @ret; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# ABSTRACT: A simple, sane and efficient file slurper |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
__END__ |