Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / Tie / File.html
blob9092b2deb85a3e69e2ea4143bc8eb71795dbb969
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Tie::File - Access the lines of a disk file via a Perl array</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;Tie::File - Access the lines of a disk file via a Perl array</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <ul>
27 <li><a href="#recsep"><code>recsep</code></a></li>
28 <li><a href="#autochomp"><code>autochomp</code></a></li>
29 <li><a href="#mode"><code>mode</code></a></li>
30 <li><a href="#memory"><code>memory</code></a></li>
31 <li><a href="#dw_size"><code>dw_size</code></a></li>
32 <li><a href="#option_format">Option Format</a></li>
33 </ul>
35 <li><a href="#public_methods">Public Methods</a></li>
36 <ul>
38 <li><a href="#flock"><code>flock</code></a></li>
39 <li><a href="#autochomp"><code>autochomp</code></a></li>
40 <li><a href="#defer__flush__discard__and_autodefer"><code>defer</code>, <code>flush</code>, <code>discard</code>, and <code>autodefer</code></a></li>
41 <li><a href="#offset"><code>offset</code></a></li>
42 </ul>
44 <li><a href="#tying_to_an_alreadyopened_filehandle">Tying to an already-opened filehandle</a></li>
45 <li><a href="#deferred_writing">Deferred Writing</a></li>
46 <ul>
48 <li><a href="#autodeferring">Autodeferring</a></li>
49 </ul>
51 <li><a href="#concurrent_access_to_files">CONCURRENT ACCESS TO FILES</a></li>
52 <li><a href="#caveats">CAVEATS</a></li>
53 <li><a href="#subclassing">SUBCLASSING</a></li>
54 <li><a href="#what_about_db_file">WHAT ABOUT <code>DB_File</code>?</a></li>
55 <li><a href="#author">AUTHOR</a></li>
56 <li><a href="#license">LICENSE</a></li>
57 <li><a href="#warranty">WARRANTY</a></li>
58 <li><a href="#thanks">THANKS</a></li>
59 <li><a href="#todo">TODO</a></li>
60 </ul>
61 <!-- INDEX END -->
63 <hr />
64 <p>
65 </p>
66 <h1><a name="name">NAME</a></h1>
67 <p>Tie::File - Access the lines of a disk file via a Perl array</p>
68 <p>
69 </p>
70 <hr />
71 <h1><a name="synopsis">SYNOPSIS</a></h1>
72 <pre>
73 # This file documents Tie::File version 0.97
74 use Tie::File;</pre>
75 <pre>
76 tie @array, 'Tie::File', filename or die ...;</pre>
77 <pre>
78 $array[13] = 'blah'; # line 13 of the file is now 'blah'
79 print $array[42]; # display line 42 of the file</pre>
80 <pre>
81 $n_recs = @array; # how many records are in the file?
82 $#array -= 2; # chop two records off the end</pre>
83 <pre>
84 for (@array) {
85 s/PERL/Perl/g; # Replace PERL with Perl everywhere in the file
86 }</pre>
87 <pre>
88 # These are just like regular push, pop, unshift, shift, and splice
89 # Except that they modify the file in the way you would expect</pre>
90 <pre>
91 push @array, new recs...;
92 my $r1 = pop @array;
93 unshift @array, new recs...;
94 my $r2 = shift @array;
95 @old_recs = splice @array, 3, 7, new recs...;</pre>
96 <pre>
97 untie @array; # all finished</pre>
98 <p>
99 </p>
100 <hr />
101 <h1><a name="description">DESCRIPTION</a></h1>
102 <p><code>Tie::File</code> represents a regular text file as a Perl array. Each
103 element in the array corresponds to a record in the file. The first
104 line of the file is element 0 of the array; the second line is element
105 1, and so on.</p>
106 <p>The file is <em>not</em> loaded into memory, so this will work even for
107 gigantic files.</p>
108 <p>Changes to the array are reflected in the file immediately.</p>
109 <p>Lazy people and beginners may now stop reading the manual.</p>
111 </p>
112 <h2><a name="recsep"><code>recsep</code></a></h2>
113 <p>What is a 'record'? By default, the meaning is the same as for the
114 <code>&lt;...&gt;</code> operator: It's a string terminated by <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$/</code></a>, which is
115 probably <code>&quot;\n&quot;</code>. (Minor exception: on DOS and Win32 systems, a
116 'record' is a string terminated by <code>&quot;\r\n&quot;</code>.) You may change the
117 definition of ``record'' by supplying the <code>recsep</code> option in the <code>tie</code>
118 call:</p>
119 <pre>
120 tie @array, 'Tie::File', $file, recsep =&gt; 'es';</pre>
121 <p>This says that records are delimited by the string <code>es</code>. If the file
122 contained the following data:</p>
123 <pre>
124 Curse these pesky flies!\n</pre>
125 <p>then the <code>@array</code> would appear to have four elements:</p>
126 <pre>
127 &quot;Curse th&quot;
128 &quot;e p&quot;
129 &quot;ky fli&quot;
130 &quot;!\n&quot;</pre>
131 <p>An undefined value is not permitted as a record separator. Perl's
132 special ``paragraph mode'' semantics (&agrave; la <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$/ = &quot;&quot;</code></a>) are not
133 emulated.</p>
134 <p>Records read from the tied array do not have the record separator
135 string on the end; this is to allow</p>
136 <pre>
137 $array[17] .= &quot;extra&quot;;</pre>
138 <p>to work as expected.</p>
139 <p>(See <a href="#autochomp">autochomp</a>, below.) Records stored into the array will have
140 the record separator string appended before they are written to the
141 file, if they don't have one already. For example, if the record
142 separator string is <code>&quot;\n&quot;</code>, then the following two lines do exactly
143 the same thing:</p>
144 <pre>
145 $array[17] = &quot;Cherry pie&quot;;
146 $array[17] = &quot;Cherry pie\n&quot;;</pre>
147 <p>The result is that the contents of line 17 of the file will be
148 replaced with ``Cherry pie''; a newline character will separate line 17
149 from line 18. This means that this code will do nothing:</p>
150 <pre>
151 chomp $array[17];</pre>
152 <p>Because the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_chomp"><code>chomp</code></a>ed value will have the separator reattached when
153 it is written back to the file. There is no way to create a file
154 whose trailing record separator string is missing.</p>
155 <p>Inserting records that <em>contain</em> the record separator string is not
156 supported by this module. It will probably produce a reasonable
157 result, but what this result will be may change in a future version.
158 Use 'splice' to insert records or to replace one record with several.</p>
160 </p>
161 <h2><a name="autochomp"><code>autochomp</code></a></h2>
162 <p>Normally, array elements have the record separator removed, so that if
163 the file contains the text</p>
164 <pre>
165 Gold
166 Frankincense
167 Myrrh</pre>
168 <p>the tied array will appear to contain <code>(&quot;Gold&quot;, &quot;Frankincense&quot;,
169 &quot;Myrrh&quot;)</code>. If you set <code>autochomp</code> to a false value, the record
170 separator will not be removed. If the file above was tied with</p>
171 <pre>
172 tie @gifts, &quot;Tie::File&quot;, $gifts, autochomp =&gt; 0;</pre>
173 <p>then the array <code>@gifts</code> would appear to contain <code>(&quot;Gold\n&quot;,
174 &quot;Frankincense\n&quot;, &quot;Myrrh\n&quot;)</code>, or (on Win32 systems) <code>(&quot;Gold\r\n&quot;,
175 &quot;Frankincense\r\n&quot;, &quot;Myrrh\r\n&quot;)</code>.</p>
177 </p>
178 <h2><a name="mode"><code>mode</code></a></h2>
179 <p>Normally, the specified file will be opened for read and write access,
180 and will be created if it does not exist. (That is, the flags
181 <code>O_RDWR | O_CREAT</code> are supplied in the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a> call.) If you want to
182 change this, you may supply alternative flags in the <code>mode</code> option.
183 See <a href="file://C|\msysgit\mingw\html/lib/Fcntl.html">the Fcntl manpage</a> for a listing of available flags.
184 For example:</p>
185 <pre>
186 # open the file if it exists, but fail if it does not exist
187 use Fcntl 'O_RDWR';
188 tie @array, 'Tie::File', $file, mode =&gt; O_RDWR;</pre>
189 <pre>
190 # create the file if it does not exist
191 use Fcntl 'O_RDWR', 'O_CREAT';
192 tie @array, 'Tie::File', $file, mode =&gt; O_RDWR | O_CREAT;</pre>
193 <pre>
194 # open an existing file in read-only mode
195 use Fcntl 'O_RDONLY';
196 tie @array, 'Tie::File', $file, mode =&gt; O_RDONLY;</pre>
197 <p>Opening the data file in write-only or append mode is not supported.</p>
199 </p>
200 <h2><a name="memory"><code>memory</code></a></h2>
201 <p>This is an upper limit on the amount of memory that <code>Tie::File</code> will
202 consume at any time while managing the file. This is used for two
203 things: managing the <em>read cache</em> and managing the <em>deferred write
204 buffer</em>.</p>
205 <p>Records read in from the file are cached, to avoid having to re-read
206 them repeatedly. If you read the same record twice, the first time it
207 will be stored in memory, and the second time it will be fetched from
208 the <em>read cache</em>. The amount of data in the read cache will not
209 exceed the value you specified for <code>memory</code>. If <code>Tie::File</code> wants
210 to cache a new record, but the read cache is full, it will make room
211 by expiring the least-recently visited records from the read cache.</p>
212 <p>The default memory limit is 2Mib. You can adjust the maximum read
213 cache size by supplying the <code>memory</code> option. The argument is the
214 desired cache size, in bytes.</p>
215 <pre>
216 # I have a lot of memory, so use a large cache to speed up access
217 tie @array, 'Tie::File', $file, memory =&gt; 20_000_000;</pre>
218 <p>Setting the memory limit to 0 will inhibit caching; records will be
219 fetched from disk every time you examine them.</p>
220 <p>The <code>memory</code> value is not an absolute or exact limit on the memory
221 used. <code>Tie::File</code> objects contains some structures besides the read
222 cache and the deferred write buffer, whose sizes are not charged
223 against <code>memory</code>.</p>
224 <p>The cache itself consumes about 310 bytes per cached record, so if
225 your file has many short records, you may want to decrease the cache
226 memory limit, or else the cache overhead may exceed the size of the
227 cached data.</p>
229 </p>
230 <h2><a name="dw_size"><code>dw_size</code></a></h2>
231 <p>(This is an advanced feature. Skip this section on first reading.)</p>
232 <p>If you use deferred writing (See <a href="#deferred_writing">Deferred Writing</a>, below) then
233 data you write into the array will not be written directly to the
234 file; instead, it will be saved in the <em>deferred write buffer</em> to be
235 written out later. Data in the deferred write buffer is also charged
236 against the memory limit you set with the <code>memory</code> option.</p>
237 <p>You may set the <code>dw_size</code> option to limit the amount of data that can
238 be saved in the deferred write buffer. This limit may not exceed the
239 total memory limit. For example, if you set <code>dw_size</code> to 1000 and
240 <code>memory</code> to 2500, that means that no more than 1000 bytes of deferred
241 writes will be saved up. The space available for the read cache will
242 vary, but it will always be at least 1500 bytes (if the deferred write
243 buffer is full) and it could grow as large as 2500 bytes (if the
244 deferred write buffer is empty.)</p>
245 <p>If you don't specify a <code>dw_size</code>, it defaults to the entire memory
246 limit.</p>
248 </p>
249 <h2><a name="option_format">Option Format</a></h2>
250 <p><code>-mode</code> is a synonym for <code>mode</code>. <code>-recsep</code> is a synonym for
251 <code>recsep</code>. <code>-memory</code> is a synonym for <code>memory</code>. You get the
252 idea.</p>
254 </p>
255 <hr />
256 <h1><a name="public_methods">Public Methods</a></h1>
257 <p>The <code>tie</code> call returns an object, say <code>$o</code>. You may call</p>
258 <pre>
259 $rec = $o-&gt;FETCH($n);
260 $o-&gt;STORE($n, $rec);</pre>
261 <p>to fetch or store the record at line <code>$n</code>, respectively; similarly
262 the other tied array methods. (See <a href="file://C|\msysgit\mingw\html/pod/perltie.html">the perltie manpage</a> for details.) You may
263 also call the following methods on this object:</p>
265 </p>
266 <h2><a name="flock"><code>flock</code></a></h2>
267 <pre>
268 $o-&gt;flock(MODE)</pre>
269 <p>will lock the tied file. <code>MODE</code> has the same meaning as the second
270 argument to the Perl built-in <code>flock</code> function; for example
271 <code>LOCK_SH</code> or <code>LOCK_EX | LOCK_NB</code>. (These constants are provided by
272 the <code>use Fcntl ':flock'</code> declaration.)</p>
273 <p><code>MODE</code> is optional; the default is <code>LOCK_EX</code>.</p>
274 <p><code>Tie::File</code> maintains an internal table of the byte offset of each
275 record it has seen in the file.</p>
276 <p>When you use <code>flock</code> to lock the file, <code>Tie::File</code> assumes that the
277 read cache is no longer trustworthy, because another process might
278 have modified the file since the last time it was read. Therefore, a
279 successful call to <code>flock</code> discards the contents of the read cache
280 and the internal record offset table.</p>
281 <p><code>Tie::File</code> promises that the following sequence of operations will
282 be safe:</p>
283 <pre>
284 my $o = tie @array, &quot;Tie::File&quot;, $filename;
285 $o-&gt;flock;</pre>
286 <p>In particular, <code>Tie::File</code> will <em>not</em> read or write the file during
287 the <code>tie</code> call. (Exception: Using <code>mode =&gt; O_TRUNC</code> will, of
288 course, erase the file during the <code>tie</code> call. If you want to do this
289 safely, then open the file without <code>O_TRUNC</code>, lock the file, and use
290 <code>@array = ()</code>.)</p>
291 <p>The best way to unlock a file is to discard the object and untie the
292 array. It is probably unsafe to unlock the file without also untying
293 it, because if you do, changes may remain unwritten inside the object.
294 That is why there is no shortcut for unlocking. If you really want to
295 unlock the file prematurely, you know what to do; if you don't know
296 what to do, then don't do it.</p>
297 <p>All the usual warnings about file locking apply here. In particular,
298 note that file locking in Perl is <strong>advisory</strong>, which means that
299 holding a lock will not prevent anyone else from reading, writing, or
300 erasing the file; it only prevents them from getting another lock at
301 the same time. Locks are analogous to green traffic lights: If you
302 have a green light, that does not prevent the idiot coming the other
303 way from plowing into you sideways; it merely guarantees to you that
304 the idiot does not also have a green light at the same time.</p>
306 </p>
307 <h2><a name="autochomp"><code>autochomp</code></a></h2>
308 <pre>
309 my $old_value = $o-&gt;autochomp(0); # disable autochomp option
310 my $old_value = $o-&gt;autochomp(1); # enable autochomp option</pre>
311 <pre>
312 my $ac = $o-&gt;autochomp(); # recover current value</pre>
313 <p>See <a href="#autochomp">autochomp</a>, above.</p>
315 </p>
316 <h2><a name="defer__flush__discard__and_autodefer"><code>defer</code>, <code>flush</code>, <code>discard</code>, and <code>autodefer</code></a></h2>
317 <p>See <a href="#deferred_writing">Deferred Writing</a>, below.</p>
319 </p>
320 <h2><a name="offset"><code>offset</code></a></h2>
321 <pre>
322 $off = $o-&gt;offset($n);</pre>
323 <p>This method returns the byte offset of the start of the <code>$n</code>th record
324 in the file. If there is no such record, it returns an undefined
325 value.</p>
327 </p>
328 <hr />
329 <h1><a name="tying_to_an_alreadyopened_filehandle">Tying to an already-opened filehandle</a></h1>
330 <p>If <code>$fh</code> is a filehandle, such as is returned by <code>IO::File</code> or one
331 of the other <code>IO</code> modules, you may use:</p>
332 <pre>
333 tie @array, 'Tie::File', $fh, ...;</pre>
334 <p>Similarly if you opened that handle <code>FH</code> with regular <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a> or
335 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sysopen"><code>sysopen</code></a>, you may use:</p>
336 <pre>
337 tie @array, 'Tie::File', \*FH, ...;</pre>
338 <p>Handles that were opened write-only won't work. Handles that were
339 opened read-only will work as long as you don't try to modify the
340 array. Handles must be attached to seekable sources of data---that
341 means no pipes or sockets. If <code>Tie::File</code> can detect that you
342 supplied a non-seekable handle, the <code>tie</code> call will throw an
343 exception. (On Unix systems, it can detect this.)</p>
344 <p>Note that Tie::File will only close any filehandles that it opened
345 internally. If you passed it a filehandle as above, you ``own'' the
346 filehandle, and are responsible for closing it after you have untied
347 the @array.</p>
349 </p>
350 <hr />
351 <h1><a name="deferred_writing">Deferred Writing</a></h1>
352 <p>(This is an advanced feature. Skip this section on first reading.)</p>
353 <p>Normally, modifying a <code>Tie::File</code> array writes to the underlying file
354 immediately. Every assignment like <code>$a[3] = ...</code> rewrites as much of
355 the file as is necessary; typically, everything from line 3 through
356 the end will need to be rewritten. This is the simplest and most
357 transparent behavior. Performance even for large files is reasonably
358 good.</p>
359 <p>However, under some circumstances, this behavior may be excessively
360 slow. For example, suppose you have a million-record file, and you
361 want to do:</p>
362 <pre>
363 for (@FILE) {
364 $_ = &quot;&gt; $_&quot;;
365 }</pre>
366 <p>The first time through the loop, you will rewrite the entire file,
367 from line 0 through the end. The second time through the loop, you
368 will rewrite the entire file from line 1 through the end. The third
369 time through the loop, you will rewrite the entire file from line 2 to
370 the end. And so on.</p>
371 <p>If the performance in such cases is unacceptable, you may defer the
372 actual writing, and then have it done all at once. The following loop
373 will perform much better for large files:</p>
374 <pre>
375 (tied @a)-&gt;defer;
376 for (@a) {
377 $_ = &quot;&gt; $_&quot;;
379 (tied @a)-&gt;flush;</pre>
380 <p>If <code>Tie::File</code>'s memory limit is large enough, all the writing will
381 done in memory. Then, when you call <code>-&gt;flush</code>, the entire file
382 will be rewritten in a single pass.</p>
383 <p>(Actually, the preceding discussion is something of a fib. You don't
384 need to enable deferred writing to get good performance for this
385 common case, because <code>Tie::File</code> will do it for you automatically
386 unless you specifically tell it not to. See <a href="#autodeferring">autodeferring</a>,
387 below.)</p>
388 <p>Calling <code>-&gt;flush</code> returns the array to immediate-write mode. If
389 you wish to discard the deferred writes, you may call <code>-&gt;discard</code>
390 instead of <code>-&gt;flush</code>. Note that in some cases, some of the data
391 will have been written already, and it will be too late for
392 <code>-&gt;discard</code> to discard all the changes. Support for
393 <code>-&gt;discard</code> may be withdrawn in a future version of <code>Tie::File</code>.</p>
394 <p>Deferred writes are cached in memory up to the limit specified by the
395 <code>dw_size</code> option (see above). If the deferred-write buffer is full
396 and you try to write still more deferred data, the buffer will be
397 flushed. All buffered data will be written immediately, the buffer
398 will be emptied, and the now-empty space will be used for future
399 deferred writes.</p>
400 <p>If the deferred-write buffer isn't yet full, but the total size of the
401 buffer and the read cache would exceed the <code>memory</code> limit, the oldest
402 records will be expired from the read cache until the total size is
403 under the limit.</p>
404 <p><code>push</code>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_pop"><code>pop</code></a>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_shift"><code>shift</code></a>, <code>unshift</code>, and <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_splice"><code>splice</code></a> cannot be
405 deferred. When you perform one of these operations, any deferred data
406 is written to the file and the operation is performed immediately.
407 This may change in a future version.</p>
408 <p>If you resize the array with deferred writing enabled, the file will
409 be resized immediately, but deferred records will not be written.
410 This has a surprising consequence: <code>@a = (...)</code> erases the file
411 immediately, but the writing of the actual data is deferred. This
412 might be a bug. If it is a bug, it will be fixed in a future version.</p>
414 </p>
415 <h2><a name="autodeferring">Autodeferring</a></h2>
416 <p><code>Tie::File</code> tries to guess when deferred writing might be helpful,
417 and to turn it on and off automatically.</p>
418 <pre>
419 for (@a) {
420 $_ = &quot;&gt; $_&quot;;
421 }</pre>
422 <p>In this example, only the first two assignments will be done
423 immediately; after this, all the changes to the file will be deferred
424 up to the user-specified memory limit.</p>
425 <p>You should usually be able to ignore this and just use the module
426 without thinking about deferring. However, special applications may
427 require fine control over which writes are deferred, or may require
428 that all writes be immediate. To disable the autodeferment feature,
429 use</p>
430 <pre>
431 (tied @o)-&gt;autodefer(0);</pre>
432 <p>or</p>
433 <pre>
434 tie @array, 'Tie::File', $file, autodefer =&gt; 0;</pre>
435 <p>Similarly, <code>-&gt;autodefer(1)</code> re-enables autodeferment, and
436 <code>-&gt;autodefer()</code> recovers the current value of the autodefer setting.</p>
438 </p>
439 <hr />
440 <h1><a name="concurrent_access_to_files">CONCURRENT ACCESS TO FILES</a></h1>
441 <p>Caching and deferred writing are inappropriate if you want the same
442 file to be accessed simultaneously from more than one process. Other
443 optimizations performed internally by this module are also
444 incompatible with concurrent access. A future version of this module will
445 support a <code>concurrent =&gt; 1</code> option that enables safe concurrent access.</p>
446 <p>Previous versions of this documentation suggested using <code>memory
447 =E&lt;gt&gt; 0</code> for safe concurrent access. This was mistaken. Tie::File
448 will not support safe concurrent access before version 0.98.</p>
450 </p>
451 <hr />
452 <h1><a name="caveats">CAVEATS</a></h1>
453 <p>(That's Latin for 'warnings'.)</p>
454 <ul>
455 <li>
456 <p>Reasonable effort was made to make this module efficient. Nevertheless,
457 changing the size of a record in the middle of a large file will
458 always be fairly slow, because everything after the new record must be
459 moved.</p>
460 </li>
461 <li>
462 <p>The behavior of tied arrays is not precisely the same as for regular
463 arrays. For example:</p>
464 <pre>
465 # This DOES print &quot;How unusual!&quot;
466 undef $a[10]; print &quot;How unusual!\n&quot; if defined $a[10];</pre>
467 <p><a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a>-ing a <code>Tie::File</code> array element just blanks out the
468 corresponding record in the file. When you read it back again, you'll
469 get the empty string, so the supposedly-<a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a>'ed value will be
470 defined. Similarly, if you have <code>autochomp</code> disabled, then</p>
471 <pre>
472 # This DOES print &quot;How unusual!&quot; if 'autochomp' is disabled
473 undef $a[10];
474 print &quot;How unusual!\n&quot; if $a[10];</pre>
475 <p>Because when <code>autochomp</code> is disabled, <code>$a[10]</code> will read back as
476 <code>&quot;\n&quot;</code> (or whatever the record separator string is.)</p>
477 <p>There are other minor differences, particularly regarding <code>exists</code>
478 and <code>delete</code>, but in general, the correspondence is extremely close.</p>
479 </li>
480 <li>
481 <p>I have supposed that since this module is concerned with file I/O,
482 almost all normal use of it will be heavily I/O bound. This means
483 that the time to maintain complicated data structures inside the
484 module will be dominated by the time to actually perform the I/O.
485 When there was an opportunity to spend CPU time to avoid doing I/O, I
486 usually tried to take it.</p>
487 </li>
488 <li>
489 <p>You might be tempted to think that deferred writing is like
490 transactions, with <code>flush</code> as <code>commit</code> and <code>discard</code> as
491 <code>rollback</code>, but it isn't, so don't.</p>
492 </li>
493 <li>
494 <p>There is a large memory overhead for each record offset and for each
495 cache entry: about 310 bytes per cached data record, and about 21 bytes per offset table entry.</p>
496 <p>The per-record overhead will limit the maximum number of records you
497 can access per file. Note that <em>accessing</em> the length of the array
498 via <code>$x = scalar @tied_file</code> accesses <strong>all</strong> records and stores their
499 offsets. The same for <code>foreach (@tied_file)</code>, even if you exit the
500 loop early.</p>
501 </li>
502 </ul>
504 </p>
505 <hr />
506 <h1><a name="subclassing">SUBCLASSING</a></h1>
507 <p>This version promises absolutely nothing about the internals, which
508 may change without notice. A future version of the module will have a
509 well-defined and stable subclassing API.</p>
511 </p>
512 <hr />
513 <h1><a name="what_about_db_file">WHAT ABOUT <code>DB_File</code>?</a></h1>
514 <p>People sometimes point out that <a href="file://C|\msysgit\mingw\html/ext/DB_File/DB_File.html">the DB_File manpage</a> will do something similar,
515 and ask why <code>Tie::File</code> module is necessary.</p>
516 <p>There are a number of reasons that you might prefer <code>Tie::File</code>.
517 A list is available at <code>http://perl.plover.com/TieFile/why-not-DB_File</code>.</p>
519 </p>
520 <hr />
521 <h1><a name="author">AUTHOR</a></h1>
522 <p>Mark Jason Dominus</p>
523 <p>To contact the author, send email to: <code>mjd-perl-tiefile+@plover.com</code></p>
524 <p>To receive an announcement whenever a new version of this module is
525 released, send a blank email message to
526 <code>mjd-perl-tiefile-subscribe@plover.com</code>.</p>
527 <p>The most recent version of this module, including documentation and
528 any news of importance, will be available at</p>
529 <pre>
530 <a href="http://perl.plover.com/TieFile/">http://perl.plover.com/TieFile/</a></pre>
532 </p>
533 <hr />
534 <h1><a name="license">LICENSE</a></h1>
535 <p><code>Tie::File</code> version 0.97 is copyright (C) 2003 Mark Jason Dominus.</p>
536 <p>This library is free software; you may redistribute it and/or modify
537 it under the same terms as Perl itself.</p>
538 <p>These terms are your choice of any of (1) the Perl Artistic Licence,
539 or (2) version 2 of the GNU General Public License as published by the
540 Free Software Foundation, or (3) any later version of the GNU General
541 Public License.</p>
542 <p>This library is distributed in the hope that it will be useful,
543 but WITHOUT ANY WARRANTY; without even the implied warranty of
544 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
545 GNU General Public License for more details.</p>
546 <p>You should have received a copy of the GNU General Public License
547 along with this library program; it should be in the file <code>COPYING</code>.
548 If not, write to the Free Software Foundation, Inc., 59 Temple Place,
549 Suite 330, Boston, MA 02111 USA</p>
550 <p>For licensing inquiries, contact the author at:</p>
551 <pre>
552 Mark Jason Dominus
553 255 S. Warnock St.
554 Philadelphia, PA 19107</pre>
556 </p>
557 <hr />
558 <h1><a name="warranty">WARRANTY</a></h1>
559 <p><code>Tie::File</code> version 0.97 comes with ABSOLUTELY NO WARRANTY.
560 For details, see the license.</p>
562 </p>
563 <hr />
564 <h1><a name="thanks">THANKS</a></h1>
565 <p>Gigantic thanks to Jarkko Hietaniemi, for agreeing to put this in the
566 core when I hadn't written it yet, and for generally being helpful,
567 supportive, and competent. (Usually the rule is ``choose any one.'')
568 Also big thanks to Abhijit Menon-Sen for all of the same things.</p>
569 <p>Special thanks to Craig Berry and Peter Prymmer (for VMS portability
570 help), Randy Kobes (for Win32 portability help), Clinton Pierce and
571 Autrijus Tang (for heroic eleventh-hour Win32 testing above and beyond
572 the call of duty), Michael G Schwern (for testing advice), and the
573 rest of the CPAN testers (for testing generally).</p>
574 <p>Special thanks to Tels for suggesting several speed and memory
575 optimizations.</p>
576 <p>Additional thanks to:
577 Edward Avis /
578 Mattia Barbon /
579 Tom Christiansen /
580 Gerrit Haase /
581 Gurusamy Sarathy /
582 Jarkko Hietaniemi (again) /
583 Nikola Knezevic /
584 John Kominetz /
585 Nick Ing-Simmons /
586 Tassilo von Parseval /
587 H. Dieter Pearcey /
588 Slaven Rezic /
589 Eric Roode /
590 Peter Scott /
591 Peter Somu /
592 Autrijus Tang (again) /
593 Tels (again) /
594 Juerd Waalboer</p>
596 </p>
597 <hr />
598 <h1><a name="todo">TODO</a></h1>
599 <p>More tests. (Stuff I didn't think of yet.)</p>
600 <p>Paragraph mode?</p>
601 <p>Fixed-length mode. Leave-blanks mode.</p>
602 <p>Maybe an autolocking mode?</p>
603 <p>For many common uses of the module, the read cache is a liability.
604 For example, a program that inserts a single record, or that scans the
605 file once, will have a cache hit rate of zero. This suggests a major
606 optimization: The cache should be initially disabled. Here's a hybrid
607 approach: Initially, the cache is disabled, but the cache code
608 maintains statistics about how high the hit rate would be *if* it were
609 enabled. When it sees the hit rate get high enough, it enables
610 itself. The STAT comments in this code are the beginning of an
611 implementation of this.</p>
612 <p>Record locking with fcntl()? Then the module might support an undo
613 log and get real transactions. What a tour de force that would be.</p>
614 <p>Keeping track of the highest cached record. This would allow reads-in-a-row
615 to skip the cache lookup faster (if reading from 1..N with empty cache at
616 start, the last cached value will be always N-1).
618 </p>
619 <p>More tests.
621 </p>
622 <table border="0" width="100%" cellspacing="0" cellpadding="3">
623 <tr><td class="block" style="background-color: #cccccc" valign="middle">
624 <big><strong><span class="block">&nbsp;Tie::File - Access the lines of a disk file via a Perl array</span></strong></big>
625 </td></tr>
626 </table>
628 </body>
630 </html>