92d16ce9a1ab39d3b1a931be05ba961260fe13ba
[p5sagit/p5-mst-13.2.git] / lib / Tie / File.pm
1
2 package Tie::File;
3 use Carp;
4 use POSIX 'SEEK_SET';
5 use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX';
6 require 5.005;
7
8 $VERSION = "0.50";
9 my $DEFAULT_MEMORY_SIZE = 1<<21;    # 2 megabytes
10
11 my %good_opt = map {$_ => 1, "-$_" => 1} 
12                qw(memory dw_size mode recsep discipline autochomp);
13
14 sub TIEARRAY {
15   if (@_ % 2 != 0) {
16     croak "usage: tie \@array, $_[0], filename, [option => value]...";
17   }
18   my ($pack, $file, %opts) = @_;
19
20   # transform '-foo' keys into 'foo' keys
21   for my $key (keys %opts) {
22     unless ($good_opt{$key}) {
23       croak("$pack: Unrecognized option '$key'\n");
24     }
25     my $okey = $key;
26     if ($key =~ s/^-+//) {
27       $opts{$key} = delete $opts{$okey};
28     }
29   }
30
31   unless (defined $opts{memory}) {
32     # default is the larger of the default cache size and the 
33     # deferred-write buffer size (if specified)
34     $opts{memory} = $DEFAULT_MEMORY_SIZE;
35     $opts{memory} = $opts{dw_size} 
36       if defined $opts{dw_size} && $opts{dw_size} > $DEFAULT_MEMORY_SIZE;
37     # Dora Winifred Read
38   }
39   $opts{dw_size} = $opts{memory} unless defined $opts{dw_size};
40   if ($opts{dw_size} > $opts{memory}) {
41       croak("$pack: dw_size may not be larger than total memory allocation\n");
42   }
43   # are we in deferred-write mode?
44   $opts{defer} = 0 unless defined $opts{defer};
45   $opts{deferred} = {};         # no records are presently deferred
46   $opts{deferred_s} = 0;        # count of total bytes in ->{deferred}
47
48   # the cache is a hash instead of an array because it is likely to be
49   # sparsely populated
50   $opts{cache} = {}; 
51   $opts{cached} = 0;   # total size of cached data
52   $opts{lru} = [];     # replace with heap in later version
53
54   $opts{offsets} = [0];
55   $opts{filename} = $file;
56   unless (defined $opts{recsep}) { 
57     $opts{recsep} = _default_recsep();
58   }
59   $opts{recseplen} = length($opts{recsep});
60   if ($opts{recseplen} == 0) {
61     croak "Empty record separator not supported by $pack";
62   }
63
64   $opts{autochomp} = 1 unless defined $opts{autochomp};
65
66   my $mode = defined($opts{mode}) ? $opts{mode} : O_CREAT|O_RDWR;
67   my $fh;
68
69   if (UNIVERSAL::isa($file, 'GLOB')) {
70     # We use 1 here on the theory that some systems 
71     # may not indicate failure if we use 0.
72     # MSWin32 does not indicate failure with 0, but I don't know if
73     # it will indicate failure with 1 or not.
74     unless (seek $file, 1, SEEK_SET) {
75       croak "$pack: your filehandle does not appear to be seekable";
76     }
77     seek $file, 0, SEEK_SET     # put it back
78     $fh = $file;                # setting binmode is the user's problem
79   } elsif (ref $file) {
80     croak "usage: tie \@array, $pack, filename, [option => value]...";
81   } else {
82     $fh = \do { local *FH };   # only works in 5.005 and later
83     sysopen $fh, $file, $mode, 0666 or return;
84     binmode $fh;
85   }
86   { my $ofh = select $fh; $| = 1; select $ofh } # autoflush on write
87   if (defined $opts{discipline} && $] >= 5.006) {
88     # This avoids a compile-time warning under 5.005
89     eval 'binmode($fh, $opts{discipline})';
90     croak $@ if $@ =~ /unknown discipline/i;
91     die if $@;
92   }
93   $opts{fh} = $fh;
94
95   bless \%opts => $pack;
96 }
97
98 sub FETCH {
99   my ($self, $n) = @_;
100   my $rec = exists $self->{deferred}{$n}
101                  ? $self->{deferred}{$n} : $self->_fetch($n);
102   $self->_chomp1($rec);
103 }
104
105 # Chomp many records in-place; return nothing useful
106 sub _chomp {
107   my $self = shift;
108   return unless $self->{autochomp};
109   if ($self->{autochomp}) {
110     for (@_) {
111       next unless defined;
112       substr($_, - $self->{recseplen}) = "";
113     }
114   }
115 }
116
117 # Chomp one record in-place; return modified record
118 sub _chomp1 {
119   my ($self, $rec) = @_;
120   return $rec unless $self->{autochomp};
121   return unless defined $rec;
122   substr($rec, - $self->{recseplen}) = "";
123   $rec;
124 }
125
126 sub _fetch {
127   my ($self, $n) = @_;
128
129   # check the record cache
130   { my $cached = $self->_check_cache($n);
131     return $cached if defined $cached;
132   }
133
134   unless ($#{$self->{offsets}} >= $n) {
135     my $o = $self->_fill_offsets_to($n);
136     # If it's still undefined, there is no such record, so return 'undef'
137     return unless defined $o;
138   }
139
140   my $fh = $self->{FH};
141   $self->_seek($n);             # we can do this now that offsets is populated
142   my $rec = $self->_read_record;
143
144 # If we happen to have just read the first record, check to see if
145 # the length of the record matches what 'tell' says.  If not, Tie::File
146 # won't work, and should drop dead.
147 #
148 #  if ($n == 0 && defined($rec) && tell($self->{fh}) != length($rec)) {
149 #    if (defined $self->{discipline}) {
150 #      croak "I/O discipline $self->{discipline} not supported";
151 #    } else {
152 #      croak "File encoding not supported";
153 #    }
154 #  }
155
156   $self->_cache_insert($n, $rec) if defined $rec;
157   $rec;
158 }
159
160 sub STORE {
161   my ($self, $n, $rec) = @_;
162
163   $self->_fixrecs($rec);
164
165   return $self->_store_deferred($n, $rec) if $self->{defer};
166
167   # We need this to decide whether the new record will fit
168   # It incidentally populates the offsets table 
169   # Note we have to do this before we alter the cache
170   my $oldrec = $self->_fetch($n);
171
172   if (my $cached = $self->_check_cache($n)) {
173     my $len_diff = length($rec) - length($cached);
174     $self->{cache}{$n} = $rec;
175     $self->{cached} += $len_diff;
176     $self->_cache_flush if $len_diff > 0 && $self->_cache_too_full;
177   }
178
179   if (not defined $oldrec) {
180     # We're storing a record beyond the end of the file
181     $self->_extend_file_to($n+1);
182     $oldrec = $self->{recsep};
183   }
184   my $len_diff = length($rec) - length($oldrec);
185
186   # length($oldrec) here is not consistent with text mode  TODO XXX BUG
187   $self->_twrite($rec, $self->{offsets}[$n], length($oldrec));
188
189   # now update the offsets
190   # array slice goes from element $n+1 (the first one to move)
191   # to the end
192   for (@{$self->{offsets}}[$n+1 .. $#{$self->{offsets}}]) {
193     $_ += $len_diff;
194   }
195 }
196
197 sub _store_deferred {
198   my ($self, $n, $rec) = @_;
199   $self->_uncache($n);
200   my $old_deferred = $self->{deferred}{$n};
201   $self->{deferred}{$n} = $rec;
202   $self->{deferred_s} += length($rec);
203   $self->{deferred_s} -= length($old_deferred) if defined $old_deferred;
204   if ($self->{deferred_s} > $self->{dw_size}) {
205     $self->_flush;
206   } elsif ($self->_cache_too_full) {
207     $self->_cache_flush;
208   }
209 }
210
211 # Remove a single record from the deferred-write buffer without writing it
212 # The record need not be present
213 sub _delete_deferred {
214   my ($self, $n) = @_;
215   my $rec = delete $self->{deferred}{$n};
216   return unless defined $rec;
217   $self->{deferred_s} -= length $rec;
218 }
219
220 sub FETCHSIZE {
221   my $self = shift;
222   my $n = $#{$self->{offsets}};
223   # 20020317 Change this to binary search
224   while (defined ($self->_fill_offsets_to($n+1))) {
225     ++$n;
226   }
227   for my $k (keys %{$self->{deferred}}) {
228     $n = $k+1 if $n < $k+1;
229   }
230   $n;
231 }
232
233 sub STORESIZE {
234   my ($self, $len) = @_;
235   my $olen = $self->FETCHSIZE;
236   return if $len == $olen;      # Woo-hoo!
237
238   # file gets longer
239   if ($len > $olen) {
240     if ($self->{defer}) {
241       for ($olen .. $len-1) {
242         $self->_store_deferred($_, $self->{recsep});
243       }
244     } else {
245       $self->_extend_file_to($len);
246     }
247     return;
248   }
249
250   # file gets shorter
251   if ($self->{defer}) {
252     for (grep $_ >= $len, keys %{$self->{deferred}}) {
253       $self->_delete_deferred($_);
254     }
255   }
256
257   $self->_seek($len);
258   $self->_chop_file;
259   $#{$self->{offsets}} = $len;
260 #  $self->{offsets}[0] = 0;      # in case we just chopped this
261   my @cached = grep $_ >= $len, keys %{$self->{cache}};
262   $self->_uncache(@cached);
263 }
264
265 sub PUSH {
266   my $self = shift;
267   $self->SPLICE($self->FETCHSIZE, scalar(@_), @_);
268 #  $self->FETCHSIZE;  # av.c takes care of this for me
269 }
270
271 sub POP {
272   my $self = shift;
273   my $size = $self->FETCHSIZE;
274   return if $size == 0;
275 #  print STDERR "# POPPITY POP POP POP\n";
276   scalar $self->SPLICE($size-1, 1);
277 }
278
279 sub SHIFT {
280   my $self = shift;
281   scalar $self->SPLICE(0, 1);
282 }
283
284 sub UNSHIFT {
285   my $self = shift;
286   $self->SPLICE(0, 0, @_);
287   # $self->FETCHSIZE; # av.c takes care of this for me
288 }
289
290 sub CLEAR {
291   # And enable auto-defer mode, since it's likely that they just
292   # did @a = (...); 
293   #
294   # 20020316
295   # Maybe that's too much dwimmery.  But stuffing a fake '-1' into the
296   # autodefer history might not be too much.  If you did that, you
297   # could also special-case [ -1, 0 ], which might not be too much.
298   my $self = shift;
299   $self->_seekb(0);
300   $self->_chop_file;
301   %{$self->{cache}}   = ();
302     $self->{cached}   = 0;
303   @{$self->{lru}}     = ();
304   @{$self->{offsets}} = (0);
305   %{$self->{deferred}}= ();
306     $self->{deferred_s} = 0;
307 }
308
309 sub EXTEND {
310   my ($self, $n) = @_;
311
312   # No need to pre-extend anything in this case
313   return if $self->{defer};
314
315   $self->_fill_offsets_to($n);
316   $self->_extend_file_to($n);
317 }
318
319 sub DELETE {
320   my ($self, $n) = @_;
321   my $lastrec = $self->FETCHSIZE-1;
322   my $rec = $self->FETCH($n);
323   $self->_delete_deferred($n) if $self->{defer};
324   if ($n == $lastrec) {
325     $self->_seek($n);
326     $self->_chop_file;
327     $#{$self->{offsets}}--;
328     $self->_uncache($n);
329     # perhaps in this case I should also remove trailing null records?
330     # 20020316
331     # Note that delete @a[-3..-1] deletes the records in the wrong order,
332     # so we only chop the very last one out of the file.  We could repair this
333     # by tracking deleted records inside the object.
334   } elsif ($n < $lastrec) {
335     $self->STORE($n, "");
336   }
337   $rec;
338 }
339
340 sub EXISTS {
341   my ($self, $n) = @_;
342   return 1 if exists $self->{deferred}{$n};
343   $self->_fill_offsets_to($n);  # I think this is unnecessary
344   $n < $self->FETCHSIZE;
345 }
346
347 sub SPLICE {
348   my $self = shift;
349   $self->_flush if $self->{defer};
350   if (wantarray) {
351     $self->_chomp(my @a = $self->_splice(@_));
352     @a;
353   } else {
354     $self->_chomp1(scalar $self->_splice(@_));
355   }
356 }
357
358 sub DESTROY {
359   my $self = shift;
360   $self->flush if $self->{defer};
361 }
362
363 sub _splice {
364   my ($self, $pos, $nrecs, @data) = @_;
365   my @result;
366
367   $pos = 0 unless defined $pos;
368
369   # Deal with negative and other out-of-range positions
370   # Also set default for $nrecs 
371   {
372     my $oldsize = $self->FETCHSIZE;
373     $nrecs = $oldsize unless defined $nrecs;
374     my $oldpos = $pos;
375
376     if ($pos < 0) {
377       $pos += $oldsize;
378       if ($pos < 0) {
379         croak "Modification of non-creatable array value attempted, subscript $oldpos";
380       }
381     }
382
383     if ($pos > $oldsize) {
384       return unless @data;
385       $pos = $oldsize;          # This is what perl does for normal arrays
386     }
387   }
388
389   $self->_fixrecs(@data);
390   my $data = join '', @data;
391   my $datalen = length $data;
392   my $oldlen = 0;
393
394   # compute length of data being removed
395   # Incidentally fills offsets table
396   for ($pos .. $pos+$nrecs-1) {
397     my $rec = $self->_fetch($_);
398     last unless defined $rec;
399     push @result, $rec;
400     $oldlen += length($rec);
401   }
402
403   # Modify the file
404   $self->_twrite($data, $self->{offsets}[$pos], $oldlen);
405
406   # update the offsets table part 1
407   # compute the offsets of the new records:
408   my @new_offsets;
409   if (@data) {
410     push @new_offsets, $self->{offsets}[$pos];
411     for (0 .. $#data-1) {
412       push @new_offsets, $new_offsets[-1] + length($data[$_]);
413     }
414   }
415   splice(@{$self->{offsets}}, $pos, $nrecs, @new_offsets);
416
417   # update the offsets table part 2
418   # adjust the offsets of the following old records
419   for ($pos+@data .. $#{$self->{offsets}}) {
420     $self->{offsets}[$_] += $datalen - $oldlen;
421   }
422   # If we scrubbed out all known offsets, regenerate the trivial table
423   # that knows that the file does indeed start at 0.
424   $self->{offsets}[0] = 0 unless @{$self->{offsets}};
425
426   # Perhaps the following cache foolery could be factored out
427   # into a bunch of mor opaque cache functions.  For example,
428   # it's odd to delete a record from the cache and then remove
429   # it from the LRU queue later on; there should be a function to
430   # do both at once.
431
432   # update the read cache, part 1
433   # modified records
434   # Consider this carefully for correctness
435   for ($pos .. $pos+$nrecs-1) {
436     my $cached = $self->{cache}{$_};
437     next unless defined $cached;
438     my $new = $data[$_-$pos];
439     if (defined $new) {
440       $self->{cached} += length($new) - length($cached);
441       $self->{cache}{$_} = $new;
442     } else {
443       $self->_uncache($_);
444     }
445   }
446   # update the read cache, part 2
447   # moved records - records past the site of the change
448   # need to be renumbered
449   # Maybe merge this with the previous block?
450   {
451     my %adjusted;
452     for (keys %{$self->{cache}}) {
453       next unless $_ >= $pos + $nrecs;
454       $adjusted{$_-$nrecs+@data} = delete $self->{cache}{$_};
455     }
456     @{$self->{cache}}{keys %adjusted} = values %adjusted;
457 #    for (keys %{$self->{cache}}) {
458 #      next unless $_ >= $pos + $nrecs;
459 #      $self->{cache}{$_-$nrecs+@data} = delete $self->{cache}{$_};
460 #    }
461   }
462     
463   # fix the LRU queue
464   my(@new, @changed);
465   for (@{$self->{lru}}) {
466     if ($_ >= $pos + $nrecs) {
467       push @new, $_ + @data - $nrecs;
468     } elsif ($_ >= $pos) {
469       push @changed, $_ if $_ < $pos + @data;
470     } else {
471       push @new, $_;
472     }
473   }
474   @{$self->{lru}} = (@new, @changed);
475
476   # Now there might be too much data in the cache, if we spliced out
477   # some short records and spliced in some long ones.  If so, flush
478   # the cache.
479   $self->_cache_flush;
480
481   # Yes, the return value of 'splice' *is* actually this complicated
482   wantarray ? @result : @result ? $result[-1] : undef;
483 }
484
485 # write data into the file
486 # $data is the data to be written. 
487 # it should be written at position $pos, and should overwrite
488 # exactly $len of the following bytes.  
489 # Note that if length($data) > $len, the subsequent bytes will have to 
490 # be moved up, and if length($data) < $len, they will have to
491 # be moved down
492 sub _twrite {
493   my ($self, $data, $pos, $len) = @_;
494
495   unless (defined $pos) {
496     die "\$pos was undefined in _twrite";
497   }
498
499   my $len_diff = length($data) - $len;
500
501   if ($len_diff == 0) {          # Woo-hoo!
502     my $fh = $self->{fh};
503     $self->_seekb($pos);
504     $self->_write_record($data);
505     return;                     # well, that was easy.
506   }
507
508   # the two records are of different lengths
509   # our strategy here: rewrite the tail of the file,
510   # reading ahead one buffer at a time
511   # $bufsize is required to be at least as large as the data we're overwriting
512   my $bufsize = _bufsize($len_diff);
513   my ($writepos, $readpos) = ($pos, $pos+$len);
514   my $next_block;
515
516   # Seems like there ought to be a way to avoid the repeated code
517   # and the special case here.  The read(1) is also a little weird.
518   # Think about this.
519   do {
520     $self->_seekb($readpos);
521     my $br = read $self->{fh}, $next_block, $bufsize;
522     my $more_data = read $self->{fh}, my($dummy), 1;
523     $self->_seekb($writepos);
524     $self->_write_record($data);
525     $readpos += $br;
526     $writepos += length $data;
527     $data = $next_block;
528   } while $more_data;
529   $self->_seekb($writepos);
530   $self->_write_record($next_block);
531
532   # There might be leftover data at the end of the file
533   $self->_chop_file if $len_diff < 0;
534 }
535
536 # If a record does not already end with the appropriate terminator
537 # string, append one.
538 sub _fixrecs {
539   my $self = shift;
540   for (@_) {
541     $_ .= $self->{recsep}
542       unless substr($_, - $self->{recseplen}) eq $self->{recsep};
543   }
544 }
545
546
547 ################################################################
548 #
549 # Basic read, write, and seek
550 #
551
552 # seek to the beginning of record #$n
553 # Assumes that the offsets table is already correctly populated
554 #
555 # Note that $n=-1 has a special meaning here: It means the start of
556 # the last known record; this may or may not be the very last record
557 # in the file, depending on whether the offsets table is fully populated.
558 #
559 sub _seek {
560   my ($self, $n) = @_;
561   my $o = $self->{offsets}[$n];
562   defined($o)
563     or confess("logic error: undefined offset for record $n");
564   seek $self->{fh}, $o, SEEK_SET
565     or die "Couldn't seek filehandle: $!";  # "Should never happen."
566 }
567
568 sub _seekb {
569   my ($self, $b) = @_;
570   seek $self->{fh}, $b, SEEK_SET
571     or die "Couldn't seek filehandle: $!";  # "Should never happen."
572 }
573
574 # populate the offsets table up to the beginning of record $n
575 # return the offset of record $n
576 sub _fill_offsets_to {
577   my ($self, $n) = @_;
578   my $fh = $self->{fh};
579   local *OFF = $self->{offsets};
580   my $rec;
581
582   until ($#OFF >= $n) {
583     my $o = $OFF[-1];
584     $self->_seek(-1);           # tricky -- see comment at _seek
585     $rec = $self->_read_record;
586     if (defined $rec) {
587       push @OFF, tell $fh;
588     } else {
589       return;                   # It turns out there is no such record
590     }
591   }
592
593   # we have now read all the records up to record n-1,
594   # so we can return the offset of record n
595   return $OFF[$n];
596 }
597
598 # assumes that $rec is already suitably terminated
599 sub _write_record {
600   my ($self, $rec) = @_;
601   my $fh = $self->{fh};
602   print $fh $rec
603     or die "Couldn't write record: $!";  # "Should never happen."
604
605 }
606
607 sub _read_record {
608   my $self = shift;
609   my $rec;
610   { local $/ = $self->{recsep};
611     my $fh = $self->{fh};
612     $rec = <$fh>;
613   }
614   $rec;
615 }
616
617 ################################################################
618 #
619 # Read cache management
620
621 # Insert a record into the cache at position $n
622 # Only appropriate when no data is cached for $n already
623 sub _cache_insert {
624   my ($self, $n, $rec) = @_;
625
626   # Do not cache records that are too big to fit in the cache.
627   return unless length $rec <= $self->{memory};
628
629   $self->{cache}{$n} = $rec;
630   $self->{cached} += length $rec;
631   push @{$self->{lru}}, $n;     # most-recently-used is at the END
632
633   $self->_cache_flush if $self->_cache_too_full;
634 }
635
636 # Remove cached data for record $n, if there is any
637 # (It is OK if $n is not in the cache at all)
638 sub _uncache {
639   my $self = shift;
640   for my $n (@_) {
641     my $cached = delete $self->{cache}{$n};
642     next unless defined $cached;
643     @{$self->{lru}} = grep $_ != $n, @{$self->{lru}};
644     $self->{cached} -= length($cached);
645   }
646 }
647
648 # _check_cache promotes record $n to MRU.  Is this correct behavior?
649 sub _check_cache {
650   my ($self, $n) = @_;
651   my $rec;
652   return unless defined($rec = $self->{cache}{$n});
653
654   # cache hit; update LRU queue and return $rec
655   # replace this with a heap in a later version
656   # 20020317 This should be a separate method
657   @{$self->{lru}} = ((grep $_ ne $n, @{$self->{lru}}), $n);
658   $rec;
659 }
660
661 sub _cache_too_full {
662   my $self = shift;
663   $self->{cached} + $self->{deferred_s} > $self->{memory};
664 }
665
666 sub _cache_flush {
667   my ($self) = @_;
668   while ($self->_cache_too_full) {
669     my $lru = shift @{$self->{lru}};
670     my $rec = delete $self->{cache}{$lru};
671     $self->{cached} -= length $rec;
672   }
673 }
674
675 ################################################################
676 #
677 # File custodial services
678 #
679
680
681 # We have read to the end of the file and have the offsets table
682 # entirely populated.  Now we need to write a new record beyond
683 # the end of the file.  We prepare for this by writing
684 # empty records into the file up to the position we want
685 #
686 # assumes that the offsets table already contains the offset of record $n,
687 # if it exists, and extends to the end of the file if not.
688 sub _extend_file_to {
689   my ($self, $n) = @_;
690   $self->_seek(-1);             # position after the end of the last record
691   my $pos = $self->{offsets}[-1];
692
693   # the offsets table has one entry more than the total number of records
694   $extras = $n - $#{$self->{offsets}};
695
696   # Todo : just use $self->{recsep} x $extras here?
697   while ($extras-- > 0) {
698     $self->_write_record($self->{recsep});
699     push @{$self->{offsets}}, tell $self->{fh};
700   }
701 }
702
703 # Truncate the file at the current position
704 sub _chop_file {
705   my $self = shift;
706   truncate $self->{fh}, tell($self->{fh});
707 }
708
709
710 # compute the size of a buffer suitable for moving
711 # all the data in a file forward $n bytes
712 # ($n may be negative)
713 # The result should be at least $n.
714 sub _bufsize {
715   my $n = shift;
716   return 8192 if $n < 0;
717   my $b = $n & ~8191;
718   $b += 8192 if $n & 8191;
719   $b;
720 }
721
722 ################################################################
723 #
724 # Miscellaneous public methods
725 #
726
727 # Lock the file
728 sub flock {
729   my ($self, $op) = @_;
730   unless (@_ <= 3) {
731     my $pack = ref $self;
732     croak "Usage: $pack\->flock([OPERATION])";
733   }
734   my $fh = $self->{fh};
735   $op = LOCK_EX unless defined $op;
736   flock $fh, $op;
737 }
738
739 # Get/set autochomp option
740 sub autochomp {
741   my $self = shift;
742   if (@_) {
743     my $old = $self->{autochomp};
744     $self->{autochomp} = shift;
745     $old;
746   } else {
747     $self->{autochomp};
748   }
749 }
750
751 ################################################################
752 #
753 # Matters related to deferred writing
754 #
755
756 # Defer writes
757 sub defer {
758   my $self = shift;
759   $self->{defer} = 1;
760 }
761
762 # Flush deferred writes
763 #
764 # This could be better optimized to write the file in one pass, instead
765 # of one pass per block of records.  But that will require modifications
766 # to _twrite, so I should have a good _twite test suite first.
767 sub flush {
768   my $self = shift;
769
770   $self->_flush;
771   $self->{defer} = 0;
772 }
773
774 sub _flush {
775   my $self = shift;
776   my @writable = sort {$a<=>$b} (keys %{$self->{deferred}});
777   
778   while (@writable) {
779     # gather all consecutive records from the front of @writable
780     my $first_rec = shift @writable;
781     my $last_rec = $first_rec+1;
782     ++$last_rec, shift @writable while @writable && $last_rec == $writable[0];
783     --$last_rec;
784     $self->_fill_offsets_to($last_rec);
785     $self->_extend_file_to($last_rec);
786     $self->_splice($first_rec, $last_rec-$first_rec+1, 
787                    @{$self->{deferred}}{$first_rec .. $last_rec});
788   }
789
790   $self->_discard;               # clear out defered-write-cache
791 }
792
793 # Discard deferred writes and disable future deferred writes
794 sub discard {
795   my $self = shift;
796   $self->_discard;
797   $self->{defer} = 0;
798 }
799
800 # Discard deferred writes, but retain old deferred writing mode
801 sub _discard {
802   my $self = shift;
803   $self->{deferred} = {};
804   $self->{deferred_s} = 0;
805 }
806
807 # Not yet implemented
808 sub autodefer { }
809
810 # This is NOT a method.  It is here for two reasons:
811 #  1. To factor a fairly complicated block out of the constructor
812 #  2. To provide access for the test suite, which need to be sure
813 #     files are being written properly.
814 sub _default_recsep {
815   my $recsep = $/;
816   if ($^O eq 'MSWin32') {       # Dos too?
817     # Windows users expect files to be terminated with \r\n
818     # But $/ is set to \n instead
819     # Note that this also transforms \n\n into \r\n\r\n.
820     # That is a feature.
821     $recsep =~ s/\n/\r\n/g;
822   }
823   $recsep;
824 }
825
826 # Utility function for _check_integrity
827 sub _ci_warn {
828   my $msg = shift;
829   $msg =~ s/\n/\\n/g;
830   $msg =~ s/\r/\\r/g;
831   print "# $msg\n";
832 }
833
834 # Given a file, make sure the cache is consistent with the
835 # file contents and the internal data structures are consistent with
836 # each other.  Returns true if everything checks out, false if not
837 #
838 # The $file argument is no longer used.  It is retained for compatibility
839 # with the existing test suite.
840 sub _check_integrity {
841   my ($self, $file, $warn) = @_;
842   my $good = 1; 
843
844   if (not defined $self->{offsets}[0]) {
845     _ci_warn("offset 0 is missing!");
846     $good = 0;
847   } elsif ($self->{offsets}[0] != 0) {
848     _ci_warn("rec 0: offset <$self->{offsets}[0]> s/b 0!");
849     $good = 0;
850   }
851
852   local *F = $self->{fh};
853   seek F, 0, SEEK_SET;
854   local $/ = $self->{recsep};
855   my $rsl = $self->{recseplen};
856   $. = 0;
857
858   while (<F>) {
859     my $n = $. - 1;
860     my $cached = $self->{cache}{$n};
861     my $offset = $self->{offsets}[$.];
862     my $ao = tell F;
863     if (defined $offset && $offset != $ao) {
864       _ci_warn("rec $n: offset <$offset> actual <$ao>");
865       $good = 0;
866     }
867     if (defined $cached && $_ ne $cached) {
868       $good = 0;
869       chomp $cached;
870       chomp;
871       _ci_warn("rec $n: cached <$cached> actual <$_>");
872     }
873     if (defined $cached && substr($cached, -$rsl) ne $/) {
874       _ci_warn("rec $n in the cache is missing the record separator");
875     }
876   }
877
878   my $cached = 0;
879   while (my ($n, $r) = each %{$self->{cache}}) {
880     $cached += length($r);
881     next if $n+1 <= $.;         # checked this already
882     _ci_warn("spurious caching of record $n");
883     $good = 0;
884   }
885   if ($cached != $self->{cached}) {
886     _ci_warn("cache size is $self->{cached}, should be $cached");
887     $good = 0;
888   }
889
890   my (%seen, @duplicate);
891   for (@{$self->{lru}}) {
892     $seen{$_}++;
893     if (not exists $self->{cache}{$_}) {
894       _ci_warn("$_ is mentioned in the LRU queue, but not in the cache");
895       $good = 0;
896     }
897   }
898   @duplicate = grep $seen{$_}>1, keys %seen;
899   if (@duplicate) {
900     my $records = @duplicate == 1 ? 'Record' : 'Records';
901     my $appear  = @duplicate == 1 ? 'appears' : 'appear';
902     _ci_warn("$records @duplicate $appear multiple times in LRU queue: @{$self->{lru}}");
903     $good = 0;
904   }
905   for (keys %{$self->{cache}}) {
906     unless (exists $seen{$_}) {
907       _ci_warn("record $_ is in the cache but not the LRU queue");
908       $good = 0;
909     }
910   }
911
912   # Now let's check the deferbuffer
913   # Unless deferred writing is enabled, it should be empty
914   if (! $self->{defer} && %{$self->{deferred}}) {
915     _ci_warn("deferred writing disabled, but deferbuffer nonempty");
916     $good = 0;
917   }
918
919   # Any record in the deferbuffer should *not* be present in the readcache
920   my $deferred_s = 0;
921   while (my ($n, $r) = each %{$self->{deferred}}) {
922     $deferred_s += length($r);
923     if (exists $self->{cache}{$n}) {
924       _ci_warn("record $n is in the deferbuffer *and* the readcache");
925       $good = 0;
926     }
927     if (substr($r, -$rsl) ne $/) {
928       _ci_warn("rec $n in the deferbuffer is missing the record separator");
929       $good = 0;
930     }
931   }
932
933   # Total size of deferbuffer should match internal total
934   if ($deferred_s != $self->{deferred_s}) {
935     _ci_warn("buffer size is $self->{deferred_s}, should be $deferred_s");
936     $good = 0;
937   }
938
939   # Total size of deferbuffer should not exceed the specified limit
940   if ($deferred_s > $self->{dw_size}) {
941     _ci_warn("buffer size is $self->{deferred_s} which exceeds the limit of $self->{dw_size}");
942     $good = 0;
943   }
944
945   # Total size of cached data should not exceed the specified limit
946   if ($deferred_s + $cached > $self->{memory}) {
947     my $total = $deferred_s + $cached;
948     _ci_warn("total stored data size is $total which exceeds the limit of $self->{memory}");
949     $good = 0;
950   }
951
952   $good;
953 }
954
955 "Cogito, ergo sum.";  # don't forget to return a true value from the file
956
957 =head1 NAME
958
959 Tie::File - Access the lines of a disk file via a Perl array
960
961 =head1 SYNOPSIS
962
963         # This file documents Tie::File version 0.50
964
965         tie @array, 'Tie::File', filename or die ...;
966
967         $array[13] = 'blah';     # line 13 of the file is now 'blah'
968         print $array[42];        # display line 42 of the file
969
970         $n_recs = @array;        # how many records are in the file?
971         $#array -= 2;            # chop two records off the end
972
973
974         for (@array) {
975           s/PERL/Perl/g;         # Replace PERL with Perl everywhere in the file
976         }
977
978         # These are just like regular push, pop, unshift, shift, and splice
979         # Except that they modify the file in the way you would expect
980
981         push @array, new recs...;
982         my $r1 = pop @array;
983         unshift @array, new recs...;
984         my $r1 = shift @array;
985         @old_recs = splice @array, 3, 7, new recs...;
986
987         untie @array;            # all finished
988
989
990 =head1 DESCRIPTION
991
992 C<Tie::File> represents a regular text file as a Perl array.  Each
993 element in the array corresponds to a record in the file.  The first
994 line of the file is element 0 of the array; the second line is element
995 1, and so on.
996
997 The file is I<not> loaded into memory, so this will work even for
998 gigantic files.
999
1000 Changes to the array are reflected in the file immediately.
1001
1002 Lazy people and beginners may now stop reading the manual.
1003
1004 =head2 C<recsep>
1005
1006 What is a 'record'?  By default, the meaning is the same as for the
1007 C<E<lt>...E<gt>> operator: It's a string terminated by C<$/>, which is
1008 probably C<"\n">.  (Minor exception: on dos and Win32 systems, a
1009 'record' is a string terminated by C<"\r\n">.)  You may change the
1010 definition of "record" by supplying the C<recsep> option in the C<tie>
1011 call:
1012
1013         tie @array, 'Tie::File', $file, recsep => 'es';
1014
1015 This says that records are delimited by the string C<es>.  If the file
1016 contained the following data:
1017
1018         Curse these pesky flies!\n
1019
1020 then the C<@array> would appear to have four elements: 
1021
1022         "Curse th"
1023         "e p"
1024         "ky fli"
1025         "!\n"
1026
1027 An undefined value is not permitted as a record separator.  Perl's
1028 special "paragraph mode" semantics (E<agrave> la C<$/ = "">) are not
1029 emulated.
1030
1031 Records read from the tied array do not have the record separator
1032 string on the end; this is to allow 
1033
1034         $array[17] .= "extra";
1035
1036 to work as expected.
1037
1038 (See L<"autochomp">, below.)  Records stored into the array will have
1039 the record separator string appended before they are written to the
1040 file, if they don't have one already.  For example, if the record
1041 separator string is C<"\n">, then the following two lines do exactly
1042 the same thing:
1043
1044         $array[17] = "Cherry pie";
1045         $array[17] = "Cherry pie\n";
1046
1047 The result is that the contents of line 17 of the file will be
1048 replaced with "Cherry pie"; a newline character will separate line 17
1049 from line 18.  This means that in particular, this will do nothing:
1050
1051         chomp $array[17];
1052
1053 Because the C<chomp>ed value will have the separator reattached when
1054 it is written back to the file.  There is no way to create a file
1055 whose trailing record separator string is missing.
1056
1057 Inserting records that I<contain> the record separator string will
1058 produce a reasonable result, but if you can't foresee what this result
1059 will be, you'd better avoid doing this.
1060
1061 =head2 C<autochomp>
1062
1063 Normally, array elements have the record separator removed, so that if
1064 the file contains the text
1065
1066         Gold
1067         Frankincense
1068         Myrrh
1069
1070 the tied array will appear to contain C<("Gold", "Frankincense",
1071 "Myrrh")>.  If you set C<autochomp> to a false value, the record
1072 separator will not be removed.  If the file above was tied with
1073
1074         tie @gifts, "Tie::File", $gifts, autochomp => 0;
1075
1076 then the array C<@gifts> would appear to contain C<("Gold\n",
1077 "Frankincense\n", "Myrrh\n")>, or (on Win32 systems) C<("Gold\r\n",
1078 "Frankincense\r\n", "Myrrh\r\n")>.
1079
1080 =head2 C<mode>
1081
1082 Normally, the specified file will be opened for read and write access,
1083 and will be created if it does not exist.  (That is, the flags
1084 C<O_RDWR | O_CREAT> are supplied in the C<open> call.)  If you want to
1085 change this, you may supply alternative flags in the C<mode> option.
1086 See L<Fcntl> for a listing of available flags.
1087 For example:
1088
1089         # open the file if it exists, but fail if it does not exist
1090         use Fcntl 'O_RDWR';
1091         tie @array, 'Tie::File', $file, mode => O_RDWR;
1092
1093         # create the file if it does not exist
1094         use Fcntl 'O_RDWR', 'O_CREAT';
1095         tie @array, 'Tie::File', $file, mode => O_RDWR | O_CREAT;
1096
1097         # open an existing file in read-only mode
1098         use Fcntl 'O_RDONLY';
1099         tie @array, 'Tie::File', $file, mode => O_RDONLY;
1100
1101 Opening the data file in write-only or append mode is not supported.
1102
1103 =head2 C<memory>
1104
1105 This is an upper limit on the amount of memory that C<Tie::File> will
1106 consume at any time while managing the file.  This is used for two
1107 things: managing the I<read cache> and managing the I<deferred write
1108 buffer>.
1109
1110 Records read in from the file are cached, to avoid having to re-read
1111 them repeatedly.  If you read the same record twice, the first time it
1112 will be stored in memory, and the second time it will be fetched from
1113 the I<read cache>.  The amount of data in the read cache will not
1114 exceed the value you specified for C<memory>.  If C<Tie::File> wants
1115 to cache a new record, but the read cache is full, it will make room
1116 by expiring the least-recently visited records from the read cache.
1117
1118 The default memory limit is 2Mib.  You can adjust the maximum read
1119 cache size by supplying the C<memory> option.  The argument is the
1120 desired cache size, in bytes.
1121
1122         # I have a lot of memory, so use a large cache to speed up access
1123         tie @array, 'Tie::File', $file, memory => 20_000_000;
1124
1125 Setting the memory limit to 0 will inhibit caching; records will be
1126 fetched from disk every time you examine them.
1127
1128 =head2 C<dw_size>
1129
1130 (This is an advanced feature.  Skip this section on first reading.)
1131  
1132 If you use deferred writing (See L<"Deferred Writing">, below) then
1133 data you write into the array will not be written directly to the
1134 file; instead, it will be saved in the I<deferred write buffer> to be
1135 written out later.  Data in the deferred write buffer is also charged
1136 against the memory limit you set with the C<memory> option.
1137
1138 You may set the C<dw_size> option to limit the amount of data that can
1139 be saved in the deferred write buffer.  This limit may not exceed the
1140 total memory limit.  For example, if you set C<dw_size> to 1000 and
1141 C<memory> to 2500, that means that no more than 1000 bytes of deferred
1142 writes will be saved up.  The space available for the read cache will
1143 vary, but it will always be at least 1500 bytes (if the deferred write
1144 buffer is full) and it could grow as large as 2500 bytes (if the
1145 deferred write buffer is empty.)
1146
1147 If you don't specify a C<dw_size>, it defaults to the entire memory
1148 limit.
1149
1150 =head2 Option Format
1151
1152 C<-mode> is a synonym for C<mode>.  C<-recsep> is a synonym for
1153 C<recsep>.  C<-memory> is a synonym for C<memory>.  You get the
1154 idea.
1155
1156 =head1 Public Methods
1157
1158 The C<tie> call returns an object, say C<$o>.  You may call 
1159
1160         $rec = $o->FETCH($n);
1161         $o->STORE($n, $rec);
1162
1163 to fetch or store the record at line C<$n>, respectively; similarly
1164 the other tied array methods.  (See L<perltie> for details.)  You may
1165 also call the following methods on this object:
1166
1167 =head2 C<flock>
1168
1169         $o->flock(MODE)
1170
1171 will lock the tied file.  C<MODE> has the same meaning as the second
1172 argument to the Perl built-in C<flock> function; for example
1173 C<LOCK_SH> or C<LOCK_EX | LOCK_NB>.  (These constants are provided by
1174 the C<use Fcntl ':flock'> declaration.)
1175
1176 C<MODE> is optional; the default is C<LOCK_EX>.
1177
1178 C<Tie::File> promises that the following sequence of operations will
1179 be safe:
1180
1181         my $o = tie @array, "Tie::File", $filename;
1182         $o->flock;
1183
1184 In particular, C<Tie::File> will I<not> read or write the file during
1185 the C<tie> call.  (Exception: Using C<mode =E<gt> O_TRUNC> will, of
1186 course, erase the file during the C<tie> call.  If you want to do this
1187 safely, then open the file without C<O_TRUNC>, lock the file, and use
1188 C<@array = ()>.)
1189
1190 The best way to unlock a file is to discard the object and untie the
1191 array.  It is probably unsafe to unlock the file without also untying
1192 it, because if you do, changes may remain unwritten inside the object.
1193 That is why there is no shortcut for unlocking.  If you really want to
1194 unlock the file prematurely, you know what to do; if you don't know
1195 what to do, then don't do it.
1196
1197 All the usual warnings about file locking apply here.  In particular,
1198 note that file locking in Perl is B<advisory>, which means that
1199 holding a lock will not prevent anyone else from reading, writing, or
1200 erasing the file; it only prevents them from getting another lock at
1201 the same time.  Locks are analogous to green traffic lights: If you
1202 have a green light, that does not prevent the idiot coming the other
1203 way from plowing into you sideways; it merely guarantees to you that
1204 the idiot does not also have a green light at the same time.
1205
1206 =head2 C<autochomp>
1207
1208         my $old_value = $o->autochomp(0);    # disable autochomp option
1209         my $old_value = $o->autochomp(1);    #  enable autochomp option
1210
1211         my $ac = $o->autochomp();   # recover current value
1212
1213 See L<"autochomp">, above.
1214
1215 =head2 C<defer>, C<flush>, and C<discard>
1216
1217 See L<"Deferred Writing">, below.
1218
1219 =head1 Tying to an already-opened filehandle
1220
1221 If C<$fh> is a filehandle, such as is returned by C<IO::File> or one
1222 of the other C<IO> modules, you may use:
1223
1224         tie @array, 'Tie::File', $fh, ...;
1225
1226 Similarly if you opened that handle C<FH> with regular C<open> or
1227 C<sysopen>, you may use:
1228
1229         tie @array, 'Tie::File', \*FH, ...;
1230
1231 Handles that were opened write-only won't work.  Handles that were
1232 opened read-only will work as long as you don't try to modify the
1233 array.  Handles must be attached to seekable sources of data---that
1234 means no pipes or sockets.  If you supply a non-seekable handle, the
1235 C<tie> call will try to throw an exception.  (On Unix systems, it
1236 B<will> throw an exception.)
1237
1238 =head1 Deferred Writing
1239
1240 (This is an advanced feature.  Skip this section on first reading.)
1241
1242 Normally, modifying a C<Tie::File> array writes to the underlying file
1243 immediately.  Every assignment like C<$a[3] = ...> rewrites as much of
1244 the file as is necessary; typically, everything from line 3 through
1245 the end will need to be rewritten.  This is the simplest and most
1246 transparent behavior.  Performance even for large files is reasonably
1247 good.
1248
1249 However, under some circumstances, this behavior may be excessively
1250 slow.  For example, suppose you have a million-record file, and you
1251 want to do:
1252
1253         for (@FILE) {
1254           $_ = "> $_";
1255         }
1256
1257 The first time through the loop, you will rewrite the entire file,
1258 from line 0 through the end.  The second time through the loop, you
1259 will rewrite the entire file from line 1 through the end.  The third
1260 time through the loop, you will rewrite the entire file from line 2 to
1261 the end.  And so on.
1262
1263 If the performance in such cases is unacceptable, you may defer the
1264 actual writing, and then have it done all at once.  The following loop
1265 will perform much better for large files:
1266
1267         (tied @a)->defer;
1268         for (@a) {
1269           $_ = "> $_";
1270         }
1271         (tied @a)->flush;
1272
1273 If C<Tie::File>'s memory limit is large enough, all the writing will
1274 done in memory.  Then, when you call C<-E<gt>flush>, the entire file
1275 will be rewritten in a single pass.
1276
1277 Calling C<-E<gt>flush> returns the array to immediate-write mode.  If
1278 you wish to discard the deferred writes, you may call C<-E<gt>discard>
1279 instead of C<-E<gt>flush>.  Note that in some cases, some of the data
1280 will have been written already, and it will be too late for
1281 C<-E<gt>discard> to discard all the changes.
1282
1283 Deferred writes are cached in memory up to the limit specified by the
1284 C<dw_size> option (see above).  If the deferred-write buffer is full
1285 and you try to write still more deferred data, the buffer will be
1286 flushed.  All buffered data will be written immediately, the buffer
1287 will be emptied, and the now-empty space will be used for future
1288 deferred writes.
1289
1290 If the deferred-write buffer isn't yet full, but the total size of the
1291 buffer and the read cache would exceed the C<memory> limit, the oldest
1292 records will be flushed out of the read cache until total usage is
1293 under the limit.
1294
1295 C<push>, C<pop>, C<shift>, C<unshift>, and C<splice> cannot be
1296 deferred.  When you perform one of these operations, any deferred data
1297 is written to the file and the operation is performed immediately.
1298 This may change in a future version.
1299
1300 A soon-to-be-released version of this module may enabled deferred
1301 write mode automagically if it guesses that you are about to write
1302 many consecutive records.  To disable this feature, use
1303
1304         (tied @o)->autodefer(0);
1305
1306 (At present, this call does nothing.)
1307
1308 =head1 CAVEATS
1309
1310 (That's Latin for 'warnings'.)
1311
1312 =over 4
1313
1314 =item *
1315
1316 This is BETA RELEASE SOFTWARE.  It may have bugs.  See the discussion
1317 below about the (lack of any) warranty.
1318
1319 =item * 
1320
1321 Every effort was made to make this module efficient.  Nevertheless,
1322 changing the size of a record in the middle of a large file will
1323 always be fairly slow, because everything after the new record must be
1324 moved.
1325
1326 =item *
1327
1328 The behavior of tied arrays is not precisely the same as for regular
1329 arrays.  For example:
1330
1331         # This DOES print "How unusual!"
1332         undef $a[10];  print "How unusual!\n" if defined $a[10];
1333
1334 C<undef>-ing a C<Tie::File> array element just blanks out the
1335 corresponding record in the file.  When you read it back again, you'll
1336 get the empty string, so the supposedly-C<undef>'ed value will be
1337 defined.  Similarly, if you have C<autochomp> disabled, then
1338
1339         # This DOES print "How unusual!" if 'autochomp' is disabled
1340         undef $a[10];  
1341         print "How unusual!\n" if $a[10];
1342
1343 Because when C<autochomp> is disabled, C<$a[10]> will read back as
1344 C<"\n"> (or whatever the record separator string is.)  
1345
1346 There are other minor differences, but in general, the correspondence
1347 is extremely close.
1348
1349 =item *
1350
1351 Not quite every effort was made to make this module as efficient as
1352 possible.  C<FETCHSIZE> should use binary search instead of linear
1353 search.  The cache's LRU queue should be a heap instead of a list.
1354
1355 The performance of the C<flush> method could be improved.  At present,
1356 it still rewrites the tail of the file once for each block of
1357 contiguous lines to be changed.  In the typical case, this will result
1358 in only one rewrite, but in peculiar cases it might be bad.  It should
1359 be possible to perform I<all> deferred writing with a single rewrite.
1360
1361 These defects are probably minor; in any event, they will be fixed in
1362 a future version of the module.
1363
1364 =item *
1365
1366 The author has supposed that since this module is concerned with file
1367 I/O, almost all normal use of it will be heavily I/O bound, and that
1368 the time to maintain complicated data structures inside the module
1369 will be dominated by the time to actually perform the I/O.  This
1370 suggests, for example, that an LRU read-cache is a good tradeoff,
1371 even if it requires substantial adjustment following a C<splice>
1372 operation.
1373
1374 =item *
1375 You might be tempted to think that deferred writing is like
1376 transactions, with C<flush> as C<commit> and C<discard> as
1377 C<rollback>, but it isn't, so don't.  
1378
1379 =back
1380
1381 =head1 SUBCLASSING
1382
1383 This version promises absolutely nothing about the internals, which
1384 may change without notice.  A future version of the module will have a
1385 well-defined and stable subclassing API.
1386
1387 =head1 WHAT ABOUT C<DB_File>?
1388
1389 C<DB_File>'s C<DB_RECNO> feature does something similar to
1390 C<Tie::File>, but there are a number of reasons that you might prefer
1391 C<Tie::File>.  C<DB_File> is a great piece of software, but the
1392 C<DB_RECNO> part is less great than the rest of it.
1393
1394 =over 4
1395
1396 =item *
1397
1398 C<DB_File> reads your entire file into memory, modifies it in memory,
1399 and the writes out the entire file again when you untie the file.
1400 This is completely impractical for large files.
1401
1402 C<Tie::File> does not do any of those things.  It doesn't try to read
1403 the entire file into memory; instead it uses a lazy approach and
1404 caches recently-used records.  The cache size is strictly bounded by
1405 the C<memory> option.  DB_File's C<-E<gt>{cachesize}> doesn't prevent
1406 your process from blowing up when reading a big file.
1407
1408 =item *
1409
1410 C<DB_File> has an extremely poor writing strategy.  If you have a
1411 ten-megabyte file and tie it with C<DB_File>, and then use
1412
1413         $a[0] =~ s/PERL/Perl/;
1414
1415 C<DB_file> will then read the entire ten-megabyte file into memory, do
1416 the change, and write the entire file back to disk, reading ten
1417 megabytes and writing ten megabytes.  C<Tie::File> will read and write
1418 only the first record.
1419
1420 If you have a million-record file and tie it with C<DB_File>, and then
1421 use
1422
1423         $a[999998] =~ s/Larry/Larry Wall/;
1424
1425 C<DB_File> will read the entire million-record file into memory, do
1426 the change, and write the entire file back to disk.  C<Tie::File> will
1427 only rewrite records 999998 and 999999.  During the writing process,
1428 it will never have more than a few kilobytes of data in memory at any
1429 time, even if the two records are very large.
1430
1431 =item *
1432
1433 Since changes to C<DB_File> files only appear when you do C<untie>, it
1434 can be inconvenient to arrange for concurrent access to the same file
1435 by two or more processes.  Each process needs to call C<$db-E<gt>sync>
1436 after every write.  When you change a C<Tie::File> array, the changes
1437 are reflected in the file immediately; no explicit C<-E<gt>sync> call
1438 is required.  (Or you can enable deferred writing mode to require that
1439 changes be explicitly sync'ed.)
1440
1441 =item *
1442
1443 C<DB_File> is only installed by default if you already have the C<db>
1444 library on your system; C<Tie::File> is pure Perl and is installed by
1445 default no matter what.  Starting with Perl 5.7.3 you can be
1446 absolutely sure it will be everywhere.  You will never have that
1447 surety with C<DB_File>.  If you don't have C<DB_File> yet, it requires
1448 a C compiler.  You can install C<Tie::File> from CPAN in five minutes
1449 with no compiler.
1450
1451 =item *
1452
1453 C<DB_File> is written in C, so if you aren't allowed to install
1454 modules on your system, it is useless.  C<Tie::File> is written in Perl,
1455 so even if you aren't allowed to install modules, you can look into
1456 the source code, see how it works, and copy the subroutines or the
1457 ideas from the subroutines directly into your own Perl program.
1458
1459 =item *
1460
1461 Except in very old, unsupported versions, C<DB_File>'s free license
1462 requires that you distribute the source code for your entire
1463 application.  If you are not able to distribute the source code for
1464 your application, you must negotiate an alternative license from
1465 Sleepycat, possibly for a fee.  Tie::File is under the Perl Artistic
1466 license and can be distributed free under the same terms as Perl
1467 itself.
1468
1469 =back
1470
1471 =head1 AUTHOR
1472
1473 Mark Jason Dominus
1474
1475 To contact the author, send email to: C<mjd-perl-tiefile+@plover.com>
1476
1477 To receive an announcement whenever a new version of this module is
1478 released, send a blank email message to
1479 C<mjd-perl-tiefile-subscribe@plover.com>.
1480
1481 The most recent version of this module, including documentation and
1482 any news of importance, will be available at
1483
1484         http://perl.plover.com/TieFile/
1485
1486
1487 =head1 LICENSE
1488
1489 C<Tie::File> version 0.50 is copyright (C) 2002 Mark Jason Dominus.
1490
1491 This library is free software; you may redistribute it and/or modify
1492 it under the same terms as Perl itself.
1493
1494 These terms are your choice of any of (1) the Perl Artistic Licence,
1495 or (2) version 2 of the GNU General Public License as published by the
1496 Free Software Foundation, or (3) any later version of the GNU General
1497 Public License.
1498
1499 This library is distributed in the hope that it will be useful,
1500 but WITHOUT ANY WARRANTY; without even the implied warranty of
1501 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1502 GNU General Public License for more details.
1503
1504 You should have received a copy of the GNU General Public License
1505 along with this library program; it should be in the file C<COPYING>.
1506 If not, write to the Free Software Foundation, Inc., 59 Temple Place,
1507 Suite 330, Boston, MA 02111 USA
1508
1509 For licensing inquiries, contact the author at:
1510
1511         Mark Jason Dominus
1512         255 S. Warnock St.
1513         Philadelphia, PA 19107
1514
1515 =head1 WARRANTY
1516
1517 C<Tie::File> version 0.50 comes with ABSOLUTELY NO WARRANTY.
1518 For details, see the license.
1519
1520 =head1 THANKS
1521
1522 Gigantic thanks to Jarkko Hietaniemi, for agreeing to put this in the
1523 core when I hadn't written it yet, and for generally being helpful,
1524 supportive, and competent.  (Usually the rule is "choose any one.")
1525 Also big thanks to Abhijit Menon-Sen for all of the same things.
1526
1527 Special thanks to Craig Berry and Peter Prymmer (for VMS portability
1528 help), Randy Kobes (for Win32 portability help), Clinton Pierce and
1529 Autrijus Tang (for heroic eleventh-hour Win32 testing above and beyond
1530 the call of duty), and the rest of the CPAN testers (for testing
1531 generally).
1532
1533 Additional thanks to:
1534 Edward Avis /
1535 Gerrit Haase /
1536 Nikola Knezevic /
1537 Nick Ing-Simmons /
1538 Tassilo von Parseval /
1539 H. Dieter Pearcey /
1540 Slaven Rezic /
1541 Peter Somu /
1542 Autrijus Tang (again) /
1543 Tels
1544
1545 =head1 TODO
1546
1547 Test DELETE machinery more carefully.
1548
1549 More tests.  (C<mode> option.  _twrite should be tested separately,
1550 because there are a lot of weird special cases lurking in there.)
1551
1552 More tests.  (Stuff I didn't think of yet.)
1553
1554 Paragraph mode?
1555
1556 More tests.
1557
1558 Fixed-length mode.
1559
1560 Maybe an autolocking mode?
1561
1562 Autodeferment.
1563
1564 Record locking with fcntl()?  Then you might support an undo log and
1565 get real transactions.  What a coup that would be.  All would bow
1566 before my might.
1567
1568 Leave-blanks mode
1569
1570 =cut
1571