1File::Slurp(3) User Contributed Perl Documentation File::Slurp(3)
2
3
4
6 File::Slurp - Efficient Reading/Writing of Complete Files
7
9 use File::Slurp;
10
11 my $text = read_file( 'filename' ) ;
12 my @lines = read_file( 'filename' ) ;
13
14 write_file( 'filename', @lines ) ;
15
16 use File::Slurp qw( slurp ) ;
17
18 my $text = slurp( 'filename' ) ;
19
21 This module provides subs that allow you to read or write entire files
22 with one simple call. They are designed to be simple to use, have
23 flexible ways to pass in or get the file contents and to be very
24 efficient. There is also a sub to read in all the files in a directory
25 other than "." and ".."
26
27 These slurp/spew subs work for files, pipes and sockets, and stdio,
28 pseudo-files, and DATA.
29
30 read_file
31 This sub reads in an entire file and returns its contents to the
32 caller. In list context it will return a list of lines (using the
33 current value of $/ as the separator including support for paragraph
34 mode when it is set to ''). In scalar context it returns the entire
35 file as a single scalar.
36
37 my $text = read_file( 'filename' ) ;
38 my @lines = read_file( 'filename' ) ;
39
40 The first argument to "read_file" is the filename and the rest of the
41 arguments are key/value pairs which are optional and which modify the
42 behavior of the call. Other than binmode the options all control how
43 the slurped file is returned to the caller.
44
45 If the first argument is a file handle reference or I/O object (if ref
46 is true), then that handle is slurped in. This mode is supported so you
47 slurp handles such as "DATA", "STDIN". See the test handle.t for an
48 example that does "open( '-|' )" and child process spews data to the
49 parant which slurps it in. All of the options that control how the
50 data is returned to the caller still work in this case.
51
52 NOTE: as of version 9999.06, read_file works correctly on the "DATA"
53 handle. It used to need a sysseek workaround but that is now handled
54 when needed by the module itself.
55
56 You can optionally request that "slurp()" is exported to your code.
57 This is an alias for read_file and is meant to be forward compatible
58 with Perl 6 (which will have slurp() built-in).
59
60 The options are:
61
62 binmode
63
64 If you set the binmode option, then the file will be slurped in binary
65 mode.
66
67 my $bin_data = read_file( $bin_file, binmode => ':raw' ) ;
68
69 NOTE: this actually sets the O_BINARY mode flag for sysopen. It
70 probably should call binmode and pass its argument to support other
71 file modes.
72
73 array_ref
74
75 If this boolean option is set, the return value (only in scalar
76 context) will be an array reference which contains the lines of the
77 slurped file. The following two calls are equivalent:
78
79 my $lines_ref = read_file( $bin_file, array_ref => 1 ) ;
80 my $lines_ref = [ read_file( $bin_file ) ] ;
81
82 scalar_ref
83
84 If this boolean option is set, the return value (only in scalar
85 context) will be an scalar reference to a string which is the contents
86 of the slurped file. This will usually be faster than returning the
87 plain scalar.
88
89 my $text_ref = read_file( $bin_file, scalar_ref => 1 ) ;
90
91 buf_ref
92
93 You can use this option to pass in a scalar reference and the slurped
94 file contents will be stored in the scalar. This can be used in
95 conjunction with any of the other options.
96
97 my $text_ref = read_file( $bin_file, buf_ref => \$buffer,
98 array_ref => 1 ) ;
99 my @lines = read_file( $bin_file, buf_ref => \$buffer ) ;
100
101 blk_size
102
103 You can use this option to set the block size used when slurping from
104 an already open handle (like \*STDIN). It defaults to 1MB.
105
106 my $text_ref = read_file( $bin_file, blk_size => 10_000_000,
107 array_ref => 1 ) ;
108
109 err_mode
110
111 You can use this option to control how read_file behaves when an error
112 occurs. This option defaults to 'croak'. You can set it to 'carp' or to
113 'quiet to have no error handling. This code wants to carp and then read
114 abother file if it fails.
115
116 my $text_ref = read_file( $file, err_mode => 'carp' ) ;
117 unless ( $text_ref ) {
118
119 # read a different file but croak if not found
120 $text_ref = read_file( $another_file ) ;
121 }
122
123 # process ${$text_ref}
124
125 write_file
126 This sub writes out an entire file in one call.
127
128 write_file( 'filename', @data ) ;
129
130 The first argument to "write_file" is the filename. The next argument
131 is an optional hash reference and it contains key/values that can
132 modify the behavior of "write_file". The rest of the argument list is
133 the data to be written to the file.
134
135 write_file( 'filename', {append => 1 }, @data ) ;
136 write_file( 'filename', {binmode => ':raw' }, $buffer ) ;
137
138 As a shortcut if the first data argument is a scalar or array
139 reference, it is used as the only data to be written to the file. Any
140 following arguments in @_ are ignored. This is a faster way to pass in
141 the output to be written to the file and is equivilent to the "buf_ref"
142 option. These following pairs are equivilent but the pass by reference
143 call will be faster in most cases (especially with larger files).
144
145 write_file( 'filename', \$buffer ) ;
146 write_file( 'filename', $buffer ) ;
147
148 write_file( 'filename', \@lines ) ;
149 write_file( 'filename', @lines ) ;
150
151 If the first argument is a file handle reference or I/O object (if ref
152 is true), then that handle is slurped in. This mode is supported so you
153 spew to handles such as \*STDOUT. See the test handle.t for an example
154 that does "open( '-|' )" and child process spews data to the parant
155 which slurps it in. All of the options that control how the data is
156 passes into "write_file" still work in this case.
157
158 "write_file" returns 1 upon successfully writing the file or undef if
159 it encountered an error.
160
161 The options are:
162
163 binmode
164
165 If you set the binmode option, then the file will be written in binary
166 mode.
167
168 write_file( $bin_file, {binmode => ':raw'}, @data ) ;
169
170 NOTE: this actually sets the O_BINARY mode flag for sysopen. It
171 probably should call binmode and pass its argument to support other
172 file modes.
173
174 buf_ref
175
176 You can use this option to pass in a scalar reference which has the
177 data to be written. If this is set then any data arguments (including
178 the scalar reference shortcut) in @_ will be ignored. These are
179 equivilent:
180
181 write_file( $bin_file, { buf_ref => \$buffer } ) ;
182 write_file( $bin_file, \$buffer ) ;
183 write_file( $bin_file, $buffer ) ;
184
185 atomic
186
187 If you set this boolean option, the file will be written to in an
188 atomic fashion. A temporary file name is created by appending the pid
189 ($$) to the file name argument and that file is spewed to. After the
190 file is closed it is renamed to the original file name (and rename is
191 an atomic operation on most OS's). If the program using this were to
192 crash in the middle of this, then the file with the pid suffix could be
193 left behind.
194
195 append
196
197 If you set this boolean option, the data will be written at the end of
198 the current file.
199
200 write_file( $file, {append => 1}, @data ) ;
201
202 "write_file" croaks if it cannot open the file. It returns true if it
203 succeeded in writing out the file and undef if there was an error.
204 (Yes, I know if it croaks it can't return anything but that is for when
205 I add the options to select the error handling mode).
206
207 no_clobber
208
209 If you set this boolean option, an existing file will not be
210 overwritten.
211
212 write_file( $file, {no_clobber => 1}, @data ) ;
213
214 err_mode
215
216 You can use this option to control how "write_file" behaves when an
217 error occurs. This option defaults to 'croak'. You can set it to 'carp'
218 or to 'quiet' to have no error handling other than the return value. If
219 the first call to "write_file" fails it will carp and then write to
220 another file. If the second call to "write_file" fails, it will croak.
221
222 unless ( write_file( $file, { err_mode => 'carp', \$data ) ;
223
224 # write a different file but croak if not found
225 write_file( $other_file, \$data ) ;
226 }
227
228 overwrite_file
229 This sub is just a typeglob alias to write_file since write_file always
230 overwrites an existing file. This sub is supported for backwards
231 compatibility with the original version of this module. See write_file
232 for its API and behavior.
233
234 append_file
235 This sub will write its data to the end of the file. It is a wrapper
236 around write_file and it has the same API so see that for the full
237 documentation. These calls are equivilent:
238
239 append_file( $file, @data ) ;
240 write_file( $file, {append => 1}, @data ) ;
241
242 read_dir
243 This sub reads all the file names from directory and returns them to
244 the caller but "." and ".." are removed by default.
245
246 my @files = read_dir( '/path/to/dir' ) ;
247
248 It croaks if it cannot open the directory.
249
250 In a list context "read_dir" returns a list of the entries in the
251 directory. In a scalar context it returns an array reference which has
252 the entries.
253
254 keep_dot_dot
255
256 If this boolean option is set, "." and ".." are not removed from the
257 list of files.
258
259 my @all_files = read_dir( '/path/to/dir', keep_dot_dot => 1 ) ;
260
261 EXPORT
262 read_file write_file overwrite_file append_file read_dir
263
264 SEE ALSO
265 An article on file slurping in extras/slurp_article.pod. There is also
266 a benchmarking script in extras/slurp_bench.pl.
267
268 BUGS
269 If run under Perl 5.004, slurping from the DATA handle will fail as
270 that requires B.pm which didn't get into core until 5.005.
271
273 Uri Guttman, <uri@stemsystems.com>
274
275
276
277perl v5.10.1 2008-01-24 File::Slurp(3)