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