line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Slurp::Tiny; |
2
|
|
|
|
|
|
|
$File::Slurp::Tiny::VERSION = '0.003'; |
3
|
1
|
|
|
1
|
|
2443
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use Carp 'croak'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
55
|
|
7
|
1
|
|
|
1
|
|
4
|
use Exporter 5.57 'import'; |
|
1
|
|
|
|
|
37
|
|
|
1
|
|
|
|
|
23
|
|
8
|
1
|
|
|
1
|
|
4
|
use File::Spec::Functions 'catfile'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
9
|
1
|
|
|
1
|
|
829
|
use FileHandle; |
|
1
|
|
|
|
|
13097
|
|
|
1
|
|
|
|
|
7
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw/read_file read_lines write_file read_dir/; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $default_layer = $^O eq 'MSWin32' ? ':crlf' : ':unix'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub read_file { |
15
|
6
|
|
|
6
|
1
|
320
|
my ($filename, %options) = @_; |
16
|
6
|
|
66
|
|
|
25
|
my $layer = $options{binmode} || $default_layer; |
17
|
6
|
100
|
|
|
|
15
|
my $buf_ref = defined $options{buf_ref} ? $options{buf_ref} : \my $buf; |
18
|
|
|
|
|
|
|
|
19
|
6
|
50
|
|
|
|
190
|
open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; |
20
|
6
|
50
|
|
|
|
48
|
if (my $size = -s $fh) { |
21
|
6
|
|
|
|
|
7
|
my ($pos, $read) = 0; |
22
|
6
|
|
33
|
|
|
8
|
do { |
23
|
6
|
50
|
|
|
|
8
|
defined($read = read $fh, ${$buf_ref}, $size - $pos, $pos) or croak "Couldn't read $filename: $!"; |
|
6
|
|
|
|
|
65
|
|
24
|
6
|
|
|
|
|
32
|
$pos += $read; |
25
|
|
|
|
|
|
|
} while ($read && $pos < $size); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
else { |
28
|
0
|
|
|
|
|
0
|
${$buf_ref} = do { local $/; <$fh> }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
29
|
|
|
|
|
|
|
} |
30
|
6
|
|
|
|
|
52
|
close $fh; |
31
|
6
|
100
|
66
|
|
|
28
|
return if not defined wantarray or $options{buf_ref}; |
32
|
5
|
100
|
|
|
|
43
|
return $options{scalar_ref} ? $buf_ref : $buf; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub read_lines { |
36
|
2
|
|
|
2
|
1
|
465
|
my ($filename, %options) = @_; |
37
|
2
|
|
50
|
|
|
11
|
my $layer = delete $options{binmode} || ':'; |
38
|
|
|
|
|
|
|
|
39
|
2
|
50
|
|
|
|
65
|
open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!"; |
40
|
2
|
100
|
|
|
|
61
|
return <$fh> if not %options; |
41
|
1
|
|
|
|
|
62
|
my @buf = <$fh>; |
42
|
1
|
|
|
|
|
12
|
close $fh; |
43
|
1
|
50
|
|
|
|
6
|
chomp @buf if $options{chomp}; |
44
|
1
|
50
|
|
|
|
21
|
return $options{array_ref} ? \@buf : @buf; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub write_file { |
48
|
2
|
|
|
2
|
1
|
625
|
my ($filename, undef, %options) = @_; |
49
|
2
|
|
33
|
|
|
27
|
my $layer = $options{binmode} || $default_layer; |
50
|
2
|
100
|
|
|
|
6
|
my $mode = $options{append} ? '>>' : '>'; |
51
|
2
|
50
|
|
|
|
7
|
my $buf_ref = defined $options{buf_ref} ? $options{buf_ref} : \$_[1]; |
52
|
|
|
|
|
|
|
|
53
|
2
|
50
|
|
|
|
114
|
open my $fh, $mode.$layer, $filename or croak "Couldn't open $filename: $!"; |
54
|
2
|
|
|
|
|
14
|
$fh->autoflush(1); |
55
|
2
|
50
|
|
|
|
80
|
print $fh ${$buf_ref} or croak "Couldn't write to $filename: $!"; |
|
2
|
|
|
|
|
58
|
|
56
|
2
|
50
|
|
|
|
23
|
close $fh or croak "Couldn't close $filename: $!"; |
57
|
2
|
|
|
|
|
10
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub read_dir { |
61
|
2
|
|
|
2
|
1
|
6
|
my ($dirname, %options) = @_; |
62
|
2
|
50
|
|
|
|
58
|
opendir my ($dir), $dirname or croak "Could not open $dirname: $!"; |
63
|
2
|
|
|
|
|
27
|
my @ret = grep { not m/ ^ \.\.? $ /x } readdir $dir; |
|
6
|
|
|
|
|
21
|
|
64
|
2
|
100
|
|
|
|
9
|
@ret = map { catfile($dirname, $_) } @ret if $options{prefix}; |
|
1
|
|
|
|
|
9
|
|
65
|
2
|
|
|
|
|
24
|
closedir $dir; |
66
|
2
|
|
|
|
|
20
|
return @ret; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# ABSTRACT: A simple, sane and efficient file slurper |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__END__ |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=pod |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=encoding UTF-8 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 NAME |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
File::Slurp::Tiny - A simple, sane and efficient file slurper |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 VERSION |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
version 0.003 |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SYNOPSIS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use File::Slurp::Tiny 'read_file'; |
90
|
|
|
|
|
|
|
my $content = read_file($filename); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 DESCRIPTION |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This module provides functions for fast and correct slurping and spewing. All functions are optionally exported. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 FUNCTIONS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 read_file($filename, %options) |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Reads file C<$filename> into a scalar. By default it returns this scalar. Can optionally take these named arguments: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=over 4 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item * binmode |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Set the layers to read the file with. The default will be something sensible on your platform. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item * buf_ref |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Pass a reference to a scalar to read the file into, instead of returning it by value. This has performance benefits. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item * scalar_ref |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
If set to true, C<read_file> will return a reference to a scalar containing the file content. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=back |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 read_lines($filename, %options) |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Reads file C<$filename> into a list/array. By default it returns this list. Can optionally take these named arguments: |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=over 4 |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * binmode |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Set the layers to read the file with. The default will be something sensible on your platform. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * array_ref |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Pass a reference to an array to read the lines into, instead of returning them by value. This has performance benefits. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item * chomp |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
C<chomp> the lines. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=back |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 write_file($filename, $content, %options) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Open C<$filename>, and write C<$content> to it. Can optionally take this named argument: |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=over 4 |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item * binmode |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Set the layers to write the file with. The default will be something sensible on your platform. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=back |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 read_dir($dirname, %options) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Open C<dirname> and return all entries except C<.> and C<..>. Can optionally take this named argument: |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=over 4 |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * prefix |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
This will prepend C<$dir> to the entries |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=back |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 SEE ALSO |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=over 4 |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item * L<Path::Tiny> |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
A minimalistic abstraction not only around |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item * L<File::Slurp> |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Another file slurping tool. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=back |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 AUTHOR |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Leon Timmermans <leont@cpan.org> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Leon Timmermans. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
185
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |