Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Tie::File.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Tie::File 3pm"
61 .TH Tie::File 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Tie::File \- Access the lines of a disk file via a Perl array
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use Tie::File;
72 \&
73 \& tie @array, \*(AqTie::File\*(Aq, filename or die ...;
74 \&
75 \& $array[0] = \*(Aqblah\*(Aq; # first line of the file is now \*(Aqblah\*(Aq
76 \& # (line numbering starts at 0)
77 \& print $array[42]; # display line 43 of the file
78 \&
79 \& $n_recs = @array; # how many records are in the file?
80 \& $#array \-= 2; # chop two records off the end
81 \&
82 \&
83 \& for (@array) {
84 \& s/PERL/Perl/g; # Replace PERL with Perl everywhere in the file
85 \& }
86 \&
87 \& # These are just like regular push, pop, unshift, shift, and splice
88 \& # Except that they modify the file in the way you would expect
89 \&
90 \& push @array, new recs...;
91 \& my $r1 = pop @array;
92 \& unshift @array, new recs...;
93 \& my $r2 = shift @array;
94 \& @old_recs = splice @array, 3, 7, new recs...;
95 \&
96 \& untie @array; # all finished
97 .Ve
98 .SH DESCRIPTION
99 .IX Header "DESCRIPTION"
100 \&\f(CW\*(C`Tie::File\*(C'\fR represents a regular text file as a Perl array. Each
101 element in the array corresponds to a record in the file. The first
102 line of the file is element 0 of the array; the second line is element
103 1, and so on.
104 .PP
105 The file is \fInot\fR loaded into memory, so this will work even for
106 gigantic files.
107 .PP
108 Changes to the array are reflected in the file immediately.
109 .PP
110 Lazy people and beginners may now stop reading the manual.
111 .ie n .SS """unicode"""
112 .el .SS \f(CWunicode\fP
113 .IX Subsection "unicode"
114 You can read a unicode (UTF\-8) file by providing a file handle opened with
115 the desired encoding. It is not safe to write to one because
116 the length in bytes and in characters is often different, Tie::File
117 will miscalculate the length of writes, overwriting parts of other records.
118 .ie n .SS """recsep"""
119 .el .SS \f(CWrecsep\fP
120 .IX Subsection "recsep"
121 What is a \*(Aqrecord\*(Aq? By default, the meaning is the same as for the
122 \&\f(CW\*(C`<...>\*(C'\fR operator: It\*(Aqs a string terminated by \f(CW$/\fR, which is
123 probably \f(CW"\en"\fR. (Minor exception: on DOS and Win32 systems, a
124 \&\*(Aqrecord\*(Aq is a string terminated by \f(CW"\er\en"\fR.) You may change the
125 definition of "record" by supplying the \f(CW\*(C`recsep\*(C'\fR option in the \f(CW\*(C`tie\*(C'\fR
126 call:
127 .PP
128 .Vb 1
129 \& tie @array, \*(AqTie::File\*(Aq, $file, recsep => \*(Aqes\*(Aq;
130 .Ve
131 .PP
132 This says that records are delimited by the string \f(CW\*(C`es\*(C'\fR. If the file
133 contained the following data:
134 .PP
135 .Vb 1
136 \& Curse these pesky flies!\en
137 .Ve
138 .PP
139 then the \f(CW@array\fR would appear to have four elements:
140 .PP
141 .Vb 4
142 \& "Curse th"
143 \& "e p"
144 \& "ky fli"
145 \& "!\en"
146 .Ve
147 .PP
148 An undefined value is not permitted as a record separator. Perl\*(Aqs
149 special "paragraph mode" semantics (à la \f(CW\*(C`$/ = ""\*(C'\fR) are not
150 emulated.
151 .PP
152 Records read from the tied array do not have the record separator
153 string on the end; this is to allow
154 .PP
155 .Vb 1
156 \& $array[17] .= "extra";
157 .Ve
158 .PP
159 to work as expected.
160 .PP
161 (See "autochomp", below.) Records stored into the array will have
162 the record separator string appended before they are written to the
163 file, if they don\*(Aqt have one already. For example, if the record
164 separator string is \f(CW"\en"\fR, then the following two lines do exactly
165 the same thing:
166 .PP
167 .Vb 2
168 \& $array[17] = "Cherry pie";
169 \& $array[17] = "Cherry pie\en";
170 .Ve
171 .PP
172 The result is that the contents of line 17 of the file will be
173 replaced with "Cherry pie"; a newline character will separate line 17
174 from line 18. This means that this code will do nothing:
175 .PP
176 .Vb 1
177 \& chomp $array[17];
178 .Ve
179 .PP
180 Because the \f(CW\*(C`chomp\*(C'\fRed value will have the separator reattached when
181 it is written back to the file. There is no way to create a file
182 whose trailing record separator string is missing.
183 .PP
184 Inserting records that \fIcontain\fR the record separator string is not
185 supported by this module. It will probably produce a reasonable
186 result, but what this result will be may change in a future version.
187 Use \*(Aqsplice\*(Aq to insert records or to replace one record with several.
188 .ie n .SS """autochomp"""
189 .el .SS \f(CWautochomp\fP
190 .IX Subsection "autochomp"
191 Normally, array elements have the record separator removed, so that if
192 the file contains the text
193 .PP
194 .Vb 3
195 \& Gold
196 \& Frankincense
197 \& Myrrh
198 .Ve
199 .PP
200 the tied array will appear to contain \f(CW\*(C`("Gold", "Frankincense",
201 "Myrrh")\*(C'\fR. If you set \f(CW\*(C`autochomp\*(C'\fR to a false value, the record
202 separator will not be removed. If the file above was tied with
203 .PP
204 .Vb 1
205 \& tie @gifts, "Tie::File", $gifts, autochomp => 0;
206 .Ve
207 .PP
208 then the array \f(CW@gifts\fR would appear to contain \f(CW\*(C`("Gold\en",
209 "Frankincense\en", "Myrrh\en")\*(C'\fR, or (on Win32 systems) \f(CW\*(C`("Gold\er\en",
210 "Frankincense\er\en", "Myrrh\er\en")\*(C'\fR.
211 .ie n .SS """mode"""
212 .el .SS \f(CWmode\fP
213 .IX Subsection "mode"
214 Normally, the specified file will be opened for read and write access,
215 and will be created if it does not exist. (That is, the flags
216 \&\f(CW\*(C`O_RDWR | O_CREAT\*(C'\fR are supplied in the \f(CW\*(C`open\*(C'\fR call.) If you want to
217 change this, you may supply alternative flags in the \f(CW\*(C`mode\*(C'\fR option.
218 See Fcntl for a listing of available flags.
219 For example:
220 .PP
221 .Vb 3
222 \& # open the file if it exists, but fail if it does not exist
223 \& use Fcntl \*(AqO_RDWR\*(Aq;
224 \& tie @array, \*(AqTie::File\*(Aq, $file, mode => O_RDWR;
225 \&
226 \& # create the file if it does not exist
227 \& use Fcntl \*(AqO_RDWR\*(Aq, \*(AqO_CREAT\*(Aq;
228 \& tie @array, \*(AqTie::File\*(Aq, $file, mode => O_RDWR | O_CREAT;
229 \&
230 \& # open an existing file in read\-only mode
231 \& use Fcntl \*(AqO_RDONLY\*(Aq;
232 \& tie @array, \*(AqTie::File\*(Aq, $file, mode => O_RDONLY;
233 .Ve
234 .PP
235 Opening the data file in write\-only or append mode is not supported.
236 .ie n .SS """memory"""
237 .el .SS \f(CWmemory\fP
238 .IX Subsection "memory"
239 This is an upper limit on the amount of memory that \f(CW\*(C`Tie::File\*(C'\fR will
240 consume at any time while managing the file. This is used for two
241 things: managing the \fIread cache\fR and managing the \fIdeferred write
242 buffer\fR.
243 .PP
244 Records read in from the file are cached, to avoid having to re\-read
245 them repeatedly. If you read the same record twice, the first time it
246 will be stored in memory, and the second time it will be fetched from
247 the \fIread cache\fR. The amount of data in the read cache will not
248 exceed the value you specified for \f(CW\*(C`memory\*(C'\fR. If \f(CW\*(C`Tie::File\*(C'\fR wants
249 to cache a new record, but the read cache is full, it will make room
250 by expiring the least\-recently visited records from the read cache.
251 .PP
252 The default memory limit is 2Mib. You can adjust the maximum read
253 cache size by supplying the \f(CW\*(C`memory\*(C'\fR option. The argument is the
254 desired cache size, in bytes.
255 .PP
256 .Vb 2
257 \& # I have a lot of memory, so use a large cache to speed up access
258 \& tie @array, \*(AqTie::File\*(Aq, $file, memory => 20_000_000;
259 .Ve
260 .PP
261 Setting the memory limit to 0 will inhibit caching; records will be
262 fetched from disk every time you examine them.
263 .PP
264 The \f(CW\*(C`memory\*(C'\fR value is not an absolute or exact limit on the memory
265 used. \f(CW\*(C`Tie::File\*(C'\fR objects contains some structures besides the read
266 cache and the deferred write buffer, whose sizes are not charged
267 against \f(CW\*(C`memory\*(C'\fR.
268 .PP
269 The cache itself consumes about 310 bytes per cached record, so if
270 your file has many short records, you may want to decrease the cache
271 memory limit, or else the cache overhead may exceed the size of the
272 cached data.
273 .ie n .SS """dw_size"""
274 .el .SS \f(CWdw_size\fP
275 .IX Subsection "dw_size"
276 (This is an advanced feature. Skip this section on first reading.)
277 .PP
278 If you use deferred writing (See "Deferred Writing", below) then
279 data you write into the array will not be written directly to the
280 file; instead, it will be saved in the \fIdeferred write buffer\fR to be
281 written out later. Data in the deferred write buffer is also charged
282 against the memory limit you set with the \f(CW\*(C`memory\*(C'\fR option.
283 .PP
284 You may set the \f(CW\*(C`dw_size\*(C'\fR option to limit the amount of data that can
285 be saved in the deferred write buffer. This limit may not exceed the
286 total memory limit. For example, if you set \f(CW\*(C`dw_size\*(C'\fR to 1000 and
287 \&\f(CW\*(C`memory\*(C'\fR to 2500, that means that no more than 1000 bytes of deferred
288 writes will be saved up. The space available for the read cache will
289 vary, but it will always be at least 1500 bytes (if the deferred write
290 buffer is full) and it could grow as large as 2500 bytes (if the
291 deferred write buffer is empty.)
292 .PP
293 If you don\*(Aqt specify a \f(CW\*(C`dw_size\*(C'\fR, it defaults to the entire memory
294 limit.
295 .SS "Option Format"
296 .IX Subsection "Option Format"
297 \&\f(CW\*(C`\-mode\*(C'\fR is a synonym for \f(CW\*(C`mode\*(C'\fR. \f(CW\*(C`\-recsep\*(C'\fR is a synonym for
298 \&\f(CW\*(C`recsep\*(C'\fR. \f(CW\*(C`\-memory\*(C'\fR is a synonym for \f(CW\*(C`memory\*(C'\fR. You get the
299 idea.
300 .SH "Public Methods"
301 .IX Header "Public Methods"
302 The \f(CW\*(C`tie\*(C'\fR call returns an object, say \f(CW$o\fR. You may call
303 .PP
304 .Vb 2
305 \& $rec = $o\->FETCH($n);
306 \& $o\->STORE($n, $rec);
307 .Ve
308 .PP
309 to fetch or store the record at line \f(CW$n\fR, respectively; similarly
310 the other tied array methods. (See perltie for details.) You may
311 also call the following methods on this object:
312 .ie n .SS """flock"""
313 .el .SS \f(CWflock\fP
314 .IX Subsection "flock"
315 .Vb 1
316 \& $o\->flock(MODE)
317 .Ve
318 .PP
319 will lock the tied file. \f(CW\*(C`MODE\*(C'\fR has the same meaning as the second
320 argument to the Perl built\-in \f(CW\*(C`flock\*(C'\fR function; for example
321 \&\f(CW\*(C`LOCK_SH\*(C'\fR or \f(CW\*(C`LOCK_EX | LOCK_NB\*(C'\fR. (These constants are provided by
322 the \f(CW\*(C`use Fcntl \*(Aq:flock\*(Aq\*(C'\fR declaration.)
323 .PP
324 \&\f(CW\*(C`MODE\*(C'\fR is optional; the default is \f(CW\*(C`LOCK_EX\*(C'\fR.
325 .PP
326 \&\f(CW\*(C`Tie::File\*(C'\fR maintains an internal table of the byte offset of each
327 record it has seen in the file.
328 .PP
329 When you use \f(CW\*(C`flock\*(C'\fR to lock the file, \f(CW\*(C`Tie::File\*(C'\fR assumes that the
330 read cache is no longer trustworthy, because another process might
331 have modified the file since the last time it was read. Therefore, a
332 successful call to \f(CW\*(C`flock\*(C'\fR discards the contents of the read cache
333 and the internal record offset table.
334 .PP
335 \&\f(CW\*(C`Tie::File\*(C'\fR promises that the following sequence of operations will
336 be safe:
337 .PP
338 .Vb 2
339 \& my $o = tie @array, "Tie::File", $filename;
340 \& $o\->flock;
341 .Ve
342 .PP
343 In particular, \f(CW\*(C`Tie::File\*(C'\fR will \fInot\fR read or write the file during
344 the \f(CW\*(C`tie\*(C'\fR call. (Exception: Using \f(CW\*(C`mode => O_TRUNC\*(C'\fR will, of
345 course, erase the file during the \f(CW\*(C`tie\*(C'\fR call. If you want to do this
346 safely, then open the file without \f(CW\*(C`O_TRUNC\*(C'\fR, lock the file, and use
347 \&\f(CW\*(C`@array = ()\*(C'\fR.)
348 .PP
349 The best way to unlock a file is to discard the object and untie the
350 array. It is probably unsafe to unlock the file without also untying
351 it, because if you do, changes may remain unwritten inside the object.
352 That is why there is no shortcut for unlocking. If you really want to
353 unlock the file prematurely, you know what to do; if you don\*(Aqt know
354 what to do, then don\*(Aqt do it.
355 .PP
356 All the usual warnings about file locking apply here. In particular,
357 note that file locking in Perl is \fBadvisory\fR, which means that
358 holding a lock will not prevent anyone else from reading, writing, or
359 erasing the file; it only prevents them from getting another lock at
360 the same time. Locks are analogous to green traffic lights: If you
361 have a green light, that does not prevent the idiot coming the other
362 way from plowing into you sideways; it merely guarantees to you that
363 the idiot does not also have a green light at the same time.
364 .ie n .SS """autochomp"""
365 .el .SS \f(CWautochomp\fP
366 .IX Subsection "autochomp"
367 .Vb 2
368 \& my $old_value = $o\->autochomp(0); # disable autochomp option
369 \& my $old_value = $o\->autochomp(1); # enable autochomp option
370 \&
371 \& my $ac = $o\->autochomp(); # recover current value
372 .Ve
373 .PP
374 See "autochomp", above.
375 .ie n .SS """defer"", ""flush"", ""discard"", and ""autodefer"""
376 .el .SS "\f(CWdefer\fP, \f(CWflush\fP, \f(CWdiscard\fP, and \f(CWautodefer\fP"
377 .IX Subsection "defer, flush, discard, and autodefer"
378 See "Deferred Writing", below.
379 .ie n .SS """offset"""
380 .el .SS \f(CWoffset\fP
381 .IX Subsection "offset"
382 .Vb 1
383 \& $off = $o\->offset($n);
384 .Ve
385 .PP
386 This method returns the byte offset of the start of the \f(CW$n\fRth record
387 in the file. If there is no such record, it returns an undefined
388 value.
389 .SH "Tying to an already\-opened filehandle"
390 .IX Header "Tying to an already-opened filehandle"
391 If \f(CW$fh\fR is a filehandle, such as is returned by \f(CW\*(C`IO::File\*(C'\fR or one
392 of the other \f(CW\*(C`IO\*(C'\fR modules, you may use:
393 .PP
394 .Vb 1
395 \& tie @array, \*(AqTie::File\*(Aq, $fh, ...;
396 .Ve
397 .PP
398 Similarly if you opened that handle \f(CW\*(C`FH\*(C'\fR with regular \f(CW\*(C`open\*(C'\fR or
399 \&\f(CW\*(C`sysopen\*(C'\fR, you may use:
400 .PP
401 .Vb 1
402 \& tie @array, \*(AqTie::File\*(Aq, \e*FH, ...;
403 .Ve
404 .PP
405 Handles that were opened write\-only won\*(Aqt work. Handles that were
406 opened read\-only will work as long as you don\*(Aqt try to modify the
407 array. Handles must be attached to seekable sources of data\-\-\-that
408 means no pipes or sockets. If \f(CW\*(C`Tie::File\*(C'\fR can detect that you
409 supplied a non\-seekable handle, the \f(CW\*(C`tie\*(C'\fR call will throw an
410 exception. (On Unix systems, it can detect this.)
411 .PP
412 Note that Tie::File will only close any filehandles that it opened
413 internally. If you passed it a filehandle as above, you "own" the
414 filehandle, and are responsible for closing it after you have untied
415 the \f(CW@array\fR.
416 .PP
417 Tie::File calls \f(CW\*(C`binmode\*(C'\fR on filehandles that it opens internally,
418 but not on filehandles passed in by the user. For consistency,
419 especially if using the tied files cross\-platform, you may wish to
420 call \f(CW\*(C`binmode\*(C'\fR on the filehandle prior to tying the file.
421 .SH "Deferred Writing"
422 .IX Header "Deferred Writing"
423 (This is an advanced feature. Skip this section on first reading.)
424 .PP
425 Normally, modifying a \f(CW\*(C`Tie::File\*(C'\fR array writes to the underlying file
426 immediately. Every assignment like \f(CW\*(C`$a[3] = ...\*(C'\fR rewrites as much of
427 the file as is necessary; typically, everything from line 3 through
428 the end will need to be rewritten. This is the simplest and most
429 transparent behavior. Performance even for large files is reasonably
430 good.
431 .PP
432 However, under some circumstances, this behavior may be excessively
433 slow. For example, suppose you have a million\-record file, and you
434 want to do:
435 .PP
436 .Vb 3
437 \& for (@FILE) {
438 \& $_ = "> $_";
439 \& }
440 .Ve
441 .PP
442 The first time through the loop, you will rewrite the entire file,
443 from line 0 through the end. The second time through the loop, you
444 will rewrite the entire file from line 1 through the end. The third
445 time through the loop, you will rewrite the entire file from line 2 to
446 the end. And so on.
447 .PP
448 If the performance in such cases is unacceptable, you may defer the
449 actual writing, and then have it done all at once. The following loop
450 will perform much better for large files:
451 .PP
452 .Vb 5
453 \& (tied @a)\->defer;
454 \& for (@a) {
455 \& $_ = "> $_";
456 \& }
457 \& (tied @a)\->flush;
458 .Ve
459 .PP
460 If \f(CW\*(C`Tie::File\*(C'\fR\*(Aqs memory limit is large enough, all the writing will
461 done in memory. Then, when you call \f(CW\*(C`\->flush\*(C'\fR, the entire file
462 will be rewritten in a single pass.
463 .PP
464 (Actually, the preceding discussion is something of a fib. You don\*(Aqt
465 need to enable deferred writing to get good performance for this
466 common case, because \f(CW\*(C`Tie::File\*(C'\fR will do it for you automatically
467 unless you specifically tell it not to. See "Autodeferring",
468 below.)
469 .PP
470 Calling \f(CW\*(C`\->flush\*(C'\fR returns the array to immediate\-write mode. If
471 you wish to discard the deferred writes, you may call \f(CW\*(C`\->discard\*(C'\fR
472 instead of \f(CW\*(C`\->flush\*(C'\fR. Note that in some cases, some of the data
473 will have been written already, and it will be too late for
474 \&\f(CW\*(C`\->discard\*(C'\fR to discard all the changes. Support for
475 \&\f(CW\*(C`\->discard\*(C'\fR may be withdrawn in a future version of \f(CW\*(C`Tie::File\*(C'\fR.
476 .PP
477 Deferred writes are cached in memory up to the limit specified by the
478 \&\f(CW\*(C`dw_size\*(C'\fR option (see above). If the deferred\-write buffer is full
479 and you try to write still more deferred data, the buffer will be
480 flushed. All buffered data will be written immediately, the buffer
481 will be emptied, and the now\-empty space will be used for future
482 deferred writes.
483 .PP
484 If the deferred\-write buffer isn\*(Aqt yet full, but the total size of the
485 buffer and the read cache would exceed the \f(CW\*(C`memory\*(C'\fR limit, the oldest
486 records will be expired from the read cache until the total size is
487 under the limit.
488 .PP
489 \&\f(CW\*(C`push\*(C'\fR, \f(CW\*(C`pop\*(C'\fR, \f(CW\*(C`shift\*(C'\fR, \f(CW\*(C`unshift\*(C'\fR, and \f(CW\*(C`splice\*(C'\fR cannot be
490 deferred. When you perform one of these operations, any deferred data
491 is written to the file and the operation is performed immediately.
492 This may change in a future version.
493 .PP
494 If you resize the array with deferred writing enabled, the file will
495 be resized immediately, but deferred records will not be written.
496 This has a surprising consequence: \f(CW\*(C`@a = (...)\*(C'\fR erases the file
497 immediately, but the writing of the actual data is deferred. This
498 might be a bug. If it is a bug, it will be fixed in a future version.
499 .SS Autodeferring
500 .IX Subsection "Autodeferring"
501 \&\f(CW\*(C`Tie::File\*(C'\fR tries to guess when deferred writing might be helpful,
502 and to turn it on and off automatically.
503 .PP
504 .Vb 3
505 \& for (@a) {
506 \& $_ = "> $_";
507 \& }
508 .Ve
509 .PP
510 In this example, only the first two assignments will be done
511 immediately; after this, all the changes to the file will be deferred
512 up to the user\-specified memory limit.
513 .PP
514 You should usually be able to ignore this and just use the module
515 without thinking about deferring. However, special applications may
516 require fine control over which writes are deferred, or may require
517 that all writes be immediate. To disable the autodeferment feature,
518 use
519 .PP
520 .Vb 1
521 \& (tied @o)\->autodefer(0);
522 .Ve
523 .PP
524 or
525 .PP
526 .Vb 1
527 \& tie @array, \*(AqTie::File\*(Aq, $file, autodefer => 0;
528 .Ve
529 .PP
530 Similarly, \f(CW\*(C`\->autodefer(1)\*(C'\fR re\-enables autodeferment, and
531 \&\f(CW\*(C`\->autodefer()\*(C'\fR recovers the current value of the autodefer setting.
532 .SH "CONCURRENT ACCESS TO FILES"
533 .IX Header "CONCURRENT ACCESS TO FILES"
534 Caching and deferred writing are inappropriate if you want the same
535 file to be accessed simultaneously from more than one process. Other
536 optimizations performed internally by this module are also
537 incompatible with concurrent access. A future version of this module will
538 support a \f(CW\*(C`concurrent => 1\*(C'\fR option that enables safe concurrent access.
539 .PP
540 Previous versions of this documentation suggested using \f(CW\*(C`memory
541 => 0\*(C'\fR for safe concurrent access. This was mistaken. Tie::File
542 will not support safe concurrent access before version 0.96.
543 .SH CAVEATS
544 .IX Header "CAVEATS"
545 (That\*(Aqs Latin for \*(Aqwarnings\*(Aq.)
546 .IP \(bu 4
547 Reasonable effort was made to make this module efficient. Nevertheless,
548 changing the size of a record in the middle of a large file will
549 always be fairly slow, because everything after the new record must be
550 moved.
551 .IP \(bu 4
552 The behavior of tied arrays is not precisely the same as for regular
553 arrays. For example:
554 .Sp
555 .Vb 2
556 \& # This DOES print "How unusual!"
557 \& undef $a[10]; print "How unusual!\en" if defined $a[10];
558 .Ve
559 .Sp
560 \&\f(CW\*(C`undef\*(C'\fR\-ing a \f(CW\*(C`Tie::File\*(C'\fR array element just blanks out the
561 corresponding record in the file. When you read it back again, you\*(Aqll
562 get the empty string, so the supposedly\-\f(CW\*(C`undef\*(C'\fR\*(Aqed value will be
563 defined. Similarly, if you have \f(CW\*(C`autochomp\*(C'\fR disabled, then
564 .Sp
565 .Vb 3
566 \& # This DOES print "How unusual!" if \*(Aqautochomp\*(Aq is disabled
567 \& undef $a[10];
568 \& print "How unusual!\en" if $a[10];
569 .Ve
570 .Sp
571 Because when \f(CW\*(C`autochomp\*(C'\fR is disabled, \f(CW$a[10]\fR will read back as
572 \&\f(CW"\en"\fR (or whatever the record separator string is.)
573 .Sp
574 There are other minor differences, particularly regarding \f(CW\*(C`exists\*(C'\fR
575 and \f(CW\*(C`delete\*(C'\fR, but in general, the correspondence is extremely close.
576 .IP \(bu 4
577 I have supposed that since this module is concerned with file I/O,
578 almost all normal use of it will be heavily I/O bound. This means
579 that the time to maintain complicated data structures inside the
580 module will be dominated by the time to actually perform the I/O.
581 When there was an opportunity to spend CPU time to avoid doing I/O, I
582 usually tried to take it.
583 .IP \(bu 4
584 You might be tempted to think that deferred writing is like
585 transactions, with \f(CW\*(C`flush\*(C'\fR as \f(CW\*(C`commit\*(C'\fR and \f(CW\*(C`discard\*(C'\fR as
586 \&\f(CW\*(C`rollback\*(C'\fR, but it isn\*(Aqt, so don\*(Aqt.
587 .IP \(bu 4
588 There is a large memory overhead for each record offset and for each
589 cache entry: about 310 bytes per cached data record, and about 21 bytes
590 per offset table entry.
591 .Sp
592 The per\-record overhead will limit the maximum number of records you
593 can access per file. Note that \fIaccessing\fR the length of the array
594 via \f(CW\*(C`$x = scalar @tied_file\*(C'\fR accesses \fBall\fR records and stores their
595 offsets. The same for \f(CW\*(C`foreach (@tied_file)\*(C'\fR, even if you exit the
596 loop early.
597 .SH SUBCLASSING
598 .IX Header "SUBCLASSING"
599 This version promises absolutely nothing about the internals, which
600 may change without notice. A future version of the module will have a
601 well\-defined and stable subclassing API.
602 .ie n .SH "WHAT ABOUT ""DB_File""?"
603 .el .SH "WHAT ABOUT \f(CWDB_File\fP?"
604 .IX Header "WHAT ABOUT DB_File?"
605 People sometimes point out that DB_File will do something similar,
606 and ask why \f(CW\*(C`Tie::File\*(C'\fR module is necessary.
607 .PP
608 There are a number of reasons that you might prefer \f(CW\*(C`Tie::File\*(C'\fR.
609 A list is available at \f(CW\*(C`<http://perl.plover.com/TieFile/why\-not\-DB_File>\*(C'\fR.
610 .SH AUTHOR
611 .IX Header "AUTHOR"
612 Mark Jason Dominus
613 .PP
614 To contact the author, send email to: \f(CW\*(C`mjd\-perl\[email protected]\*(C'\fR
615 .PP
616 To receive an announcement whenever a new version of this module is
617 released, send a blank email message to
618 \&\f(CW\*(C`mjd\-perl\-tiefile\[email protected]\*(C'\fR.
619 .PP
620 The most recent version of this module, including documentation and
621 any news of importance, will be available at
622 .PP
623 .Vb 1
624 \& http://perl.plover.com/TieFile/
625 .Ve
626 .SH LICENSE
627 .IX Header "LICENSE"
628 \&\f(CW\*(C`Tie::File\*(C'\fR version 0.96 is copyright (C) 2003 Mark Jason Dominus.
629 .PP
630 This library is free software; you may redistribute it and/or modify
631 it under the same terms as Perl itself.
632 .PP
633 These terms are your choice of any of (1) the Perl Artistic Licence,
634 or (2) version 2 of the GNU General Public License as published by the
635 Free Software Foundation, or (3) any later version of the GNU General
636 Public License.
637 .PP
638 This library is distributed in the hope that it will be useful,
639 but WITHOUT ANY WARRANTY; without even the implied warranty of
640 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
641 GNU General Public License for more details.
642 .PP
643 You should have received a copy of the GNU General Public License
644 along with this library program; it should be in the file \f(CW\*(C`COPYING\*(C'\fR.
645 If not, see <https://www.gnu.org/licenses/>.
646 .PP
647 For licensing inquiries, contact the author at:
648 .PP
649 .Vb 3
650 \& Mark Jason Dominus
651 \& 255 S. Warnock St.
652 \& Philadelphia, PA 19107
653 .Ve
654 .SH WARRANTY
655 .IX Header "WARRANTY"
656 \&\f(CW\*(C`Tie::File\*(C'\fR version 0.98 comes with ABSOLUTELY NO WARRANTY.
657 For details, see the license.
658 .SH THANKS
659 .IX Header "THANKS"
660 Gigantic thanks to Jarkko Hietaniemi, for agreeing to put this in the
661 core when I hadn\*(Aqt written it yet, and for generally being helpful,
662 supportive, and competent. (Usually the rule is "choose any one.")
663 Also big thanks to Abhijit Menon\-Sen for all of the same things.
664 .PP
665 Special thanks to Craig Berry and Peter Prymmer (for VMS portability
666 help), Randy Kobes (for Win32 portability help), Clinton Pierce and
667 Autrijus Tang (for heroic eleventh\-hour Win32 testing above and beyond
668 the call of duty), Michael G Schwern (for testing advice), and the
669 rest of the CPAN testers (for testing generally).
670 .PP
671 Special thanks to Tels for suggesting several speed and memory
672 optimizations.
673 .PP
674 Additional thanks to:
675 Edward Avis /
676 Mattia Barbon /
677 Tom Christiansen /
678 Gerrit Haase /
679 Gurusamy Sarathy /
680 Jarkko Hietaniemi (again) /
681 Nikola Knezevic /
682 John Kominetz /
683 Nick Ing\-Simmons /
684 Tassilo von Parseval /
685 H. Dieter Pearcey /
686 Slaven Rezic /
687 Eric Roode /
688 Peter Scott /
689 Peter Somu /
690 Autrijus Tang (again) /
691 Tels (again) /
692 Juerd Waalboer /
693 Todd Rinaldo
694 .SH TODO
695 .IX Header "TODO"
696 More tests. (Stuff I didn\*(Aqt think of yet.)
697 .PP
698 Paragraph mode?
699 .PP
700 Fixed\-length mode. Leave\-blanks mode.
701 .PP
702 Maybe an autolocking mode?
703 .PP
704 For many common uses of the module, the read cache is a liability.
705 For example, a program that inserts a single record, or that scans the
706 file once, will have a cache hit rate of zero. This suggests a major
707 optimization: The cache should be initially disabled. Here\*(Aqs a hybrid
708 approach: Initially, the cache is disabled, but the cache code
709 maintains statistics about how high the hit rate would be *if* it were
710 enabled. When it sees the hit rate get high enough, it enables
711 itself. The STAT comments in this code are the beginning of an
712 implementation of this.
713 .PP
714 Record locking with \fBfcntl()\fR? Then the module might support an undo
715 log and get real transactions. What a tour de force that would be.
716 .PP
717 Keeping track of the highest cached record. This would allow reads\-in\-a\-row
718 to skip the cache lookup faster (if reading from 1..N with empty cache at
719 start, the last cached value will be always N\-1).
720 .PP
721 More tests.