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