cleanup
[urisagit/Perl-Docs.git] / slurp_talk / Slurp.pm.html
CommitLineData
635c7876 1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5 <link rel="stylesheet" href="/s/style.css" type="text/css">
6 <title>search.cpan.org: File::Slurp - Efficient Reading/Writing of Complete Files</title>
7 </head>
8 <body id=cpansearch class=std>
9
10<table width="100%"><tr><td rowspan=2 width="5%">
11<a href="/"><img src="/s/img/cpan_banner.png" alt="The CPAN Search Site"></a>
12</td>
13<td>
14<div class=menubar>&nbsp;
15 <a class=m href="/">Home</a> &nbsp;&nbsp;
16 <a class=m href="/author/">Authors</a> &nbsp;&nbsp;
17 <a class=m href="/recent">Recent</a> &nbsp;&nbsp;
18 <a class=m href="/site.html">About</a> &nbsp;&nbsp;
19 <a class=m href="/mirror">Mirrors</a> &nbsp;&nbsp;
20 <a class=m href="/faq.html">FAQ</a> &nbsp;&nbsp;
21 <a class=m href="/feedback">Feedback</a>
22&nbsp;</div>
23
24</td></tr><tr><td>
25
26<form method=get action="/search" name=f><table><tr><td>
27<input type="text" name="query" value="" size=35 ></td></tr><tr><td>in <select name="mode">
28 <option value="all">All</option>
29 <option value="module" >Modules</option>
30 <option value="dist" >Distributions</option>
31 <option value="author" >Authors</option>
32</select>&nbsp;<input type="submit" value="CPAN Search">
33</td></tr></table>
34</form>
35
36</td></tr></table>
37
38
39
40 <a name="_top"></a>
41
42 <div class=path>
43 <a href="/~uri/">Uri Guttman</a> >
44 <a href="/~uri/File-Slurp-9999.01/">File-Slurp-9999.01</a> >
45 File::Slurp
46 </div>
47
48 Module Version: 9999.01 &nbsp;
49 <a href="/src/URI/File-Slurp-9999.01/lib/File/Slurp.pm">Source</a> &nbsp;
50 <p /><div class=pod>
51<a name="_top"></a>
52<div class=pod>
53<div class=toc><ul><li><a href="#NAME">NAME</a>
54<li><a href="#SYNOPSIS">SYNOPSIS</a>
55<li><a href="#DESCRIPTION">DESCRIPTION</a>
56<ul><li><a href="#read_file">read_file</a>
57<ul><li><a href="#binmode">binmode</a>
58<li><a href="#array_ref">array_ref</a>
59<li><a href="#scalar_ref">scalar_ref</a>
60<li><a href="#buf_ref">buf_ref</a>
61<li><a href="#blk_size">blk_size</a>
62<li><a href="#err_mode">err_mode</a>
63</ul>
64<li><a href="#write_file">write_file</a>
65<ul><li><a href="#binmode">binmode</a>
66<li><a href="#buf_ref">buf_ref</a>
67<li><a href="#append">append</a>
68<li><a href="#err_mode">err_mode</a>
69</ul>
70<li><a href="#overwrite_file">overwrite_file</a>
71<li><a href="#append_file">append_file</a>
72<li><a href="#read_dir">read_dir</a>
73<li><a href="#EXPORT">EXPORT</a>
74</ul>
75<li><a href="#AUTHOR">AUTHOR</a>
76</ul>
77</div>
78<!-- generated by TUCS::Pod2HTML v, using Pod::Simple::PullParser v2.02, under Perl v5.006001 at Tue Dec 9 05:21:57 2003 GMT -->
79<!-- start doc -->
80
81<h1><a name="NAME"></a
82>NAME <a href='#_top'><img alt='^' src='/s/img/up.gif'></a></h1>
83
84<p>File::Slurp &#45; Efficient Reading/Writing of Complete Files</p>
85
86<h1><a name="SYNOPSIS"></a
87>SYNOPSIS <a href='#_top'><img alt='^' src='/s/img/up.gif'></a></h1>
88
89<pre> use File::Slurp;
90
91 my $text = read_file( &#39;filename&#39; ) ;
92 my @lines = read_file( &#39;filename&#39; ) ;
93
94 write_file( &#39;filename&#39;, @lines ) ;</pre>
95
96<h1><a name="DESCRIPTION"></a
97>DESCRIPTION <a href='#_top'><img alt='^' src='/s/img/up.gif'></a></h1>
98
99<p>This module provides subs that allow you to read or write entire files with one simple call. They are designed to be simple to use, have flexible ways to pass in or get the file contents and to be very efficient. There is also a sub to read in all the files in a directory other than <code>.</code> and <code>..</code></p>
100
101<p>Note that these slurp/spew subs work only for files and not for pipes or stdio. If you want to slurp the latter, use the standard techniques such as setting $/ to undef, reading &#60;&#62; in a list context, or printing all you want to STDOUT.</p>
102
103<h2><a name="read_file"></a
104><b>read_file</b></h2>
105
106<p>This sub reads in an entire file and returns its contents to the caller. In list context it will return a list of lines (using the current value of $/ as the separator. In scalar context it returns the entire file as a single scalar.</p>
107
108<pre> my $text = read_file( &#39;filename&#39; ) ;
109 my @lines = read_file( &#39;filename&#39; ) ;</pre>
110
111<p>The first argument to <code>read_file</code> is the filename and the rest of the arguments are key/value pairs which are optional and which modify the behavior of the call. Other than binmode the options all control how the slurped file is returned to the caller.</p>
112
113<p>If the first argument is a file handle reference or I/O object (if fileno returns a defined value), then that handle is slurped in. This mode is supported so you slurp handles such as &#60;DATA&#62;, \*STDIN. See the test handle.t for an example that does <code>open( &#39;&#45;|&#39; )</code> and child process spews data to the parant which slurps it in. All of the options that control how the data is returned to the caller still work in this case.</p>
114
115<p>The options are:</p>
116
117<h3><a name="binmode"></a
118>binmode</h3>
119
120<p>If you set the binmode option, then the file will be slurped in binary mode.</p>
121
122<pre> my $bin_data = read_file( $bin_file, binmode =&#62; &#39;:raw&#39; ) ;</pre>
123
124<p>NOTE: this actually sets the O_BINARY mode flag for sysopen. It probably should call binmode and pass its argument to support other file modes.</p>
125
126<h3><a name="array_ref"></a
127>array_ref</h3>
128
129<p>If this boolean option is set, the return value (only in scalar context) will be an array reference which contains the lines of the slurped file. The following two calls are equivilent:</p>
130
131<pre> my $lines_ref = read_file( $bin_file, array_ref =&#62; 1 ) ;
132 my $lines_ref = [ read_file( $bin_file ) ] ;</pre>
133
134<h3><a name="scalar_ref"></a
135>scalar_ref</h3>
136
137<p>If this boolean option is set, the return value (only in scalar context) will be an scalar reference to a string which is the contents of the slurped file. This will usually be faster than returning the plain scalar.</p>
138
139<pre> my $text_ref = read_file( $bin_file, scalar_ref =&#62; 1 ) ;</pre>
140
141<h3><a name="buf_ref"></a
142>buf_ref</h3>
143
144<p>You can use this option to pass in a scalar reference and the slurped file contents will be stored in the scalar. This can be used in conjunction with any of the other options.</p>
145
146<pre> my $text_ref = read_file( $bin_file, buf_ref =&#62; \$buffer,
147 array_ref =&#62; 1 ) ;
148 my @lines = read_file( $bin_file, buf_ref =&#62; \$buffer ) ;</pre>
149
150<h3><a name="blk_size"></a
151>blk_size</h3>
152
153<p>You can use this option to set the block size used when slurping from an already open handle (like \*STDIN). It defaults to 1MB.</p>
154
155<pre> my $text_ref = read_file( $bin_file, blk_size =&#62; 10_000_000,
156 array_ref =&#62; 1 ) ;</pre>
157
158<h3><a name="err_mode"></a
159>err_mode</h3>
160
161<p>You can use this option to control how read_file behaves when an error occurs. This option defaults to &#39;croak&#39;. You can set it to &#39;carp&#39; or to &#39;quiet to have no error handling. This code wants to carp and then read abother file if it fails.</p>
162
163<pre> my $text_ref = read_file( $file, err_mode =&#62; &#39;carp&#39; ) ;
164 unless ( $text_ref ) {
165
166 # read a different file but croak if not found
167 $text_ref = read_file( $another_file ) ;
168 }
169
170 # process ${$text_ref}</pre>
171
172<h2><a name="write_file"></a
173><b>write_file</b></h2>
174
175<p>This sub writes out an entire file in one call.</p>
176
177<pre> write_file( &#39;filename&#39;, @data ) ;</pre>
178
179<p>The first argument to <code>write_file</code> is the filename. The next argument is an optional hash reference and it contains key/values that can modify the behavior of <code>write_file</code>. The rest of the argument list is the data to be written to the file.</p>
180
181<pre> write_file( &#39;filename&#39;, {append =&#62; 1 }, @data ) ;
182 write_file( &#39;filename&#39;, {binmode =&#62; &#39;:raw&#39; }, $buffer ) ;</pre>
183
184<p>As a shortcut if the first data argument is a scalar or array reference, it is used as the only data to be written to the file. Any following arguments in @_ are ignored. This is a faster way to pass in the output to be written to the file and is equivilent to the <code>buf_ref</code> option. These following pairs are equivilent but the pass by reference call will be faster in most cases (especially with larger files).</p>
185
186<pre> write_file( &#39;filename&#39;, \$buffer ) ;
187 write_file( &#39;filename&#39;, $buffer ) ;
188
189 write_file( &#39;filename&#39;, \@lines ) ;
190 write_file( &#39;filename&#39;, @lines ) ;</pre>
191
192<p>If the first argument is a file handle reference or I/O object (if fileno returns a defined value), then that handle is slurped in. This mode is supported so you spew to handles such as \*STDOUT. See the test handle.t for an example that does <code>open( &#39;&#45;|&#39; )</code> and child process spews data to the parant which slurps it in. All of the options that control how the data is passes into <code>write_file</code> still work in this case.</p>
193
194<p>The options are:</p>
195
196<h3><a name="binmode"></a
197>binmode</h3>
198
199<p>If you set the binmode option, then the file will be written in binary mode.</p>
200
201<pre> write_file( $bin_file, {binmode =&#62; &#39;:raw&#39;}, @data ) ;</pre>
202
203<p>NOTE: this actually sets the O_BINARY mode flag for sysopen. It probably should call binmode and pass its argument to support other file modes.</p>
204
205<h3><a name="buf_ref"></a
206>buf_ref</h3>
207
208<p>You can use this option to pass in a scalar reference which has the data to be written. If this is set then any data arguments (including the scalar reference shortcut) in @_ will be ignored. These are equivilent:</p>
209
210<pre> write_file( $bin_file, { buf_ref =&#62; \$buffer } ) ;
211 write_file( $bin_file, \$buffer ) ;
212 write_file( $bin_file, $buffer ) ;</pre>
213
214<h3><a name="append"></a
215>append</h3>
216
217<p>If you set this boolean option, the data will be written at the end of the current file.</p>
218
219<pre> write_file( $file, {append =&#62; 1}, @data ) ;</pre>
220
221<p><code>write_file</code> croaks if it cannot open the file. It returns true if it succeeded in writing out the file and undef if there was an error. (Yes, I know if it croaks it can&#39;t return anything but that is for when I add the options to select the error handling mode).</p>
222
223<h3><a name="err_mode"></a
224>err_mode</h3>
225
226<p>You can use this option to control how <code>write_file</code> behaves when an error occurs. This option defaults to &#39;croak&#39;. You can set it to &#39;carp&#39; or to &#39;quiet to have no error handling. If the first call to <code>write_file</code> fails it will carp and then write to another file. If the second call to <code>write_file</code> fails, it will croak.</p>
227
228<pre> unless ( write_file( $file, { err_mode =&#62; &#39;carp&#39;, \$data ) ;
229
230 # write a different file but croak if not found
231 write_file( $other_file, \$data ) ;
232 }</pre>
233
234<h2><a name="overwrite_file"></a
235>overwrite_file</h2>
236
237<p>This sub is just a typeglob alias to write_file since write_file always overwrites an existing file. This sub is supported for backwards compatibility with the original version of this module. See write_file for its API and behavior.</p>
238
239<h2><a name="append_file"></a
240>append_file</h2>
241
242<p>This sub will write its data to the end of the file. It is a wrapper around write_file and it has the same API so see that for the full documentation. These calls are equivilent:</p>
243
244<pre> append_file( $file, @data ) ;
245 write_file( $file, {append =&#62; 1}, @data ) ;</pre>
246
247<h2><a name="read_dir"></a
248>read_dir</h2>
249
250<p>This sub reads all the file names from directory and returns them to the caller but <code>.</code> and <code>..</code> are removed.</p>
251
252<pre> my @files = read_dir( &#39;/path/to/dir&#39; ) ;</pre>
253
254<p>It croaks if it cannot open the directory.</p>
255
256<h2><a name="EXPORT"></a
257>EXPORT</h2>
258
259<pre> read_file write_file overwrite_file append_file read_dir</pre>
260
261<h1><a name="AUTHOR"></a
262>AUTHOR <a href='#_top'><img alt='^' src='/s/img/up.gif'></a></h1>
263
264<p>Uri Guttman, &#60;uri@stemsystems.com&#62;</p>
265<!-- end doc -->
266
267</div>
268
269
270</div>
271
272<!-- Tue Dec 9 05:21:57 2003 GMT (0.239951014518738) -->
273 </body>
274</html>
275
276
277