5 use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX';
9 my $DEFAULT_MEMORY_SIZE = 1<<21; # 2 megabytes
11 my %good_opt = map {$_ => 1, "-$_" => 1}
12 qw(memory dw_size mode recsep discipline autochomp);
16 croak "usage: tie \@array, $_[0], filename, [option => value]...";
18 my ($pack, $file, %opts) = @_;
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");
26 if ($key =~ s/^-+//) {
27 $opts{$key} = delete $opts{$okey};
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;
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");
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}
48 # the cache is a hash instead of an array because it is likely to be
51 $opts{cached} = 0; # total size of cached data
52 $opts{lru} = []; # replace with heap in later version
55 $opts{filename} = $file;
56 unless (defined $opts{recsep}) {
57 $opts{recsep} = _default_recsep();
59 $opts{recseplen} = length($opts{recsep});
60 if ($opts{recseplen} == 0) {
61 croak "Empty record separator not supported by $pack";
64 $opts{autochomp} = 1 unless defined $opts{autochomp};
66 my $mode = defined($opts{mode}) ? $opts{mode} : O_CREAT|O_RDWR;
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";
77 seek $file, 0, SEEK_SET # put it back
78 $fh = $file; # setting binmode is the user's problem
80 croak "usage: tie \@array, $pack, filename, [option => value]...";
82 $fh = \do { local *FH }; # only works in 5.005 and later
83 sysopen $fh, $file, $mode, 0666 or return;
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;
95 bless \%opts => $pack;
100 my $rec = exists $self->{deferred}{$n}
101 ? $self->{deferred}{$n} : $self->_fetch($n);
102 $self->_chomp1($rec);
105 # Chomp many records in-place; return nothing useful
108 return unless $self->{autochomp};
109 if ($self->{autochomp}) {
112 substr($_, - $self->{recseplen}) = "";
117 # Chomp one record in-place; return modified record
119 my ($self, $rec) = @_;
120 return $rec unless $self->{autochomp};
121 return unless defined $rec;
122 substr($rec, - $self->{recseplen}) = "";
129 # check the record cache
130 { my $cached = $self->_check_cache($n);
131 return $cached if defined $cached;
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;
140 my $fh = $self->{FH};
141 $self->_seek($n); # we can do this now that offsets is populated
142 my $rec = $self->_read_record;
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.
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";
152 # croak "File encoding not supported";
156 $self->_cache_insert($n, $rec) if defined $rec;
161 my ($self, $n, $rec) = @_;
163 $self->_fixrecs($rec);
165 return $self->_store_deferred($n, $rec) if $self->{defer};
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);
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;
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};
184 my $len_diff = length($rec) - length($oldrec);
186 # length($oldrec) here is not consistent with text mode TODO XXX BUG
187 $self->_twrite($rec, $self->{offsets}[$n], length($oldrec));
189 # now update the offsets
190 # array slice goes from element $n+1 (the first one to move)
192 for (@{$self->{offsets}}[$n+1 .. $#{$self->{offsets}}]) {
197 sub _store_deferred {
198 my ($self, $n, $rec) = @_;
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}) {
206 } elsif ($self->_cache_too_full) {
211 # Remove a single record from the deferred-write buffer without writing it
212 # The record need not be present
213 sub _delete_deferred {
215 my $rec = delete $self->{deferred}{$n};
216 return unless defined $rec;
217 $self->{deferred_s} -= length $rec;
222 my $n = $#{$self->{offsets}};
223 # 20020317 Change this to binary search
224 while (defined ($self->_fill_offsets_to($n+1))) {
227 for my $k (keys %{$self->{deferred}}) {
228 $n = $k+1 if $n < $k+1;
234 my ($self, $len) = @_;
235 my $olen = $self->FETCHSIZE;
236 return if $len == $olen; # Woo-hoo!
240 if ($self->{defer}) {
241 for ($olen .. $len-1) {
242 $self->_store_deferred($_, $self->{recsep});
245 $self->_extend_file_to($len);
251 if ($self->{defer}) {
252 for (grep $_ >= $len, keys %{$self->{deferred}}) {
253 $self->_delete_deferred($_);
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);
267 $self->SPLICE($self->FETCHSIZE, scalar(@_), @_);
268 # $self->FETCHSIZE; # av.c takes care of this for me
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);
281 scalar $self->SPLICE(0, 1);
286 $self->SPLICE(0, 0, @_);
287 # $self->FETCHSIZE; # av.c takes care of this for me
291 # And enable auto-defer mode, since it's likely that they just
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.
301 %{$self->{cache}} = ();
303 @{$self->{lru}} = ();
304 @{$self->{offsets}} = (0);
305 %{$self->{deferred}}= ();
306 $self->{deferred_s} = 0;
312 # No need to pre-extend anything in this case
313 return if $self->{defer};
315 $self->_fill_offsets_to($n);
316 $self->_extend_file_to($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) {
327 $#{$self->{offsets}}--;
329 # perhaps in this case I should also remove trailing null records?
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, "");
342 return 1 if exists $self->{deferred}{$n};
343 $self->_fill_offsets_to($n); # I think this is unnecessary
344 $n < $self->FETCHSIZE;
349 $self->_flush if $self->{defer};
351 $self->_chomp(my @a = $self->_splice(@_));
354 $self->_chomp1(scalar $self->_splice(@_));
360 $self->flush if $self->{defer};
364 my ($self, $pos, $nrecs, @data) = @_;
367 $pos = 0 unless defined $pos;
369 # Deal with negative and other out-of-range positions
370 # Also set default for $nrecs
372 my $oldsize = $self->FETCHSIZE;
373 $nrecs = $oldsize unless defined $nrecs;
379 croak "Modification of non-creatable array value attempted, subscript $oldpos";
383 if ($pos > $oldsize) {
385 $pos = $oldsize; # This is what perl does for normal arrays
389 $self->_fixrecs(@data);
390 my $data = join '', @data;
391 my $datalen = length $data;
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;
400 $oldlen += length($rec);
404 $self->_twrite($data, $self->{offsets}[$pos], $oldlen);
406 # update the offsets table part 1
407 # compute the offsets of the new records:
410 push @new_offsets, $self->{offsets}[$pos];
411 for (0 .. $#data-1) {
412 push @new_offsets, $new_offsets[-1] + length($data[$_]);
415 splice(@{$self->{offsets}}, $pos, $nrecs, @new_offsets);
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;
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}};
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
432 # update the read cache, part 1
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];
440 $self->{cached} += length($new) - length($cached);
441 $self->{cache}{$_} = $new;
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?
452 for (keys %{$self->{cache}}) {
453 next unless $_ >= $pos + $nrecs;
454 $adjusted{$_-$nrecs+@data} = delete $self->{cache}{$_};
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}{$_};
465 for (@{$self->{lru}}) {
466 if ($_ >= $pos + $nrecs) {
467 push @new, $_ + @data - $nrecs;
468 } elsif ($_ >= $pos) {
469 push @changed, $_ if $_ < $pos + @data;
474 @{$self->{lru}} = (@new, @changed);
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
481 # Yes, the return value of 'splice' *is* actually this complicated
482 wantarray ? @result : @result ? $result[-1] : undef;
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
493 my ($self, $data, $pos, $len) = @_;
495 unless (defined $pos) {
496 die "\$pos was undefined in _twrite";
499 my $len_diff = length($data) - $len;
501 if ($len_diff == 0) { # Woo-hoo!
502 my $fh = $self->{fh};
504 $self->_write_record($data);
505 return; # well, that was easy.
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);
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.
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);
526 $writepos += length $data;
529 $self->_seekb($writepos);
530 $self->_write_record($next_block);
532 # There might be leftover data at the end of the file
533 $self->_chop_file if $len_diff < 0;
536 # If a record does not already end with the appropriate terminator
537 # string, append one.
541 $_ .= $self->{recsep}
542 unless substr($_, - $self->{recseplen}) eq $self->{recsep};
547 ################################################################
549 # Basic read, write, and seek
552 # seek to the beginning of record #$n
553 # Assumes that the offsets table is already correctly populated
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.
561 my $o = $self->{offsets}[$n];
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."
570 seek $self->{fh}, $b, SEEK_SET
571 or die "Couldn't seek filehandle: $!"; # "Should never happen."
574 # populate the offsets table up to the beginning of record $n
575 # return the offset of record $n
576 sub _fill_offsets_to {
578 my $fh = $self->{fh};
579 local *OFF = $self->{offsets};
582 until ($#OFF >= $n) {
584 $self->_seek(-1); # tricky -- see comment at _seek
585 $rec = $self->_read_record;
589 return; # It turns out there is no such record
593 # we have now read all the records up to record n-1,
594 # so we can return the offset of record n
598 # assumes that $rec is already suitably terminated
600 my ($self, $rec) = @_;
601 my $fh = $self->{fh};
603 or die "Couldn't write record: $!"; # "Should never happen."
610 { local $/ = $self->{recsep};
611 my $fh = $self->{fh};
617 ################################################################
619 # Read cache management
621 # Insert a record into the cache at position $n
622 # Only appropriate when no data is cached for $n already
624 my ($self, $n, $rec) = @_;
626 # Do not cache records that are too big to fit in the cache.
627 return unless length $rec <= $self->{memory};
629 $self->{cache}{$n} = $rec;
630 $self->{cached} += length $rec;
631 push @{$self->{lru}}, $n; # most-recently-used is at the END
633 $self->_cache_flush if $self->_cache_too_full;
636 # Remove cached data for record $n, if there is any
637 # (It is OK if $n is not in the cache at all)
641 my $cached = delete $self->{cache}{$n};
642 next unless defined $cached;
643 @{$self->{lru}} = grep $_ != $n, @{$self->{lru}};
644 $self->{cached} -= length($cached);
648 # _check_cache promotes record $n to MRU. Is this correct behavior?
652 return unless defined($rec = $self->{cache}{$n});
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);
661 sub _cache_too_full {
663 $self->{cached} + $self->{deferred_s} > $self->{memory};
668 while ($self->_cache_too_full) {
669 my $lru = shift @{$self->{lru}};
670 my $rec = delete $self->{cache}{$lru};
671 $self->{cached} -= length $rec;
675 ################################################################
677 # File custodial services
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
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 {
690 $self->_seek(-1); # position after the end of the last record
691 my $pos = $self->{offsets}[-1];
693 # the offsets table has one entry more than the total number of records
694 $extras = $n - $#{$self->{offsets}};
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};
703 # Truncate the file at the current position
706 truncate $self->{fh}, tell($self->{fh});
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.
716 return 8192 if $n < 0;
718 $b += 8192 if $n & 8191;
722 ################################################################
724 # Miscellaneous public methods
729 my ($self, $op) = @_;
731 my $pack = ref $self;
732 croak "Usage: $pack\->flock([OPERATION])";
734 my $fh = $self->{fh};
735 $op = LOCK_EX unless defined $op;
739 # Get/set autochomp option
743 my $old = $self->{autochomp};
744 $self->{autochomp} = shift;
751 ################################################################
753 # Matters related to deferred writing
762 # Flush deferred writes
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.
776 my @writable = sort {$a<=>$b} (keys %{$self->{deferred}});
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];
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});
790 $self->_discard; # clear out defered-write-cache
793 # Discard deferred writes and disable future deferred writes
800 # Discard deferred writes, but retain old deferred writing mode
803 $self->{deferred} = {};
804 $self->{deferred_s} = 0;
807 # Not yet implemented
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 {
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.
821 $recsep =~ s/\n/\r\n/g;
826 # Utility function for _check_integrity
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
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) = @_;
844 if (not defined $self->{offsets}[0]) {
845 _ci_warn("offset 0 is missing!");
847 } elsif ($self->{offsets}[0] != 0) {
848 _ci_warn("rec 0: offset <$self->{offsets}[0]> s/b 0!");
853 local *F = $self->{fh};
855 local $/ = $self->{recsep};
856 my $rsl = $self->{recseplen};
861 my $cached = $self->{cache}{$n};
862 my $offset = $self->{offsets}[$.];
864 if (defined $offset && $offset != $ao) {
865 _ci_warn("rec $n: offset <$offset> actual <$ao>");
868 if (defined $cached && $_ ne $cached) {
872 _ci_warn("rec $n: cached <$cached> actual <$_>");
874 if (defined $cached && substr($cached, -$rsl) ne $/) {
875 _ci_warn("rec $n in the cache is missing the record separator");
880 while (my ($n, $r) = each %{$self->{cache}}) {
881 $cached += length($r);
882 next if $n+1 <= $.; # checked this already
883 _ci_warn("spurious caching of record $n");
886 if ($cached != $self->{cached}) {
887 _ci_warn("cache size is $self->{cached}, should be $cached");
891 my (%seen, @duplicate);
892 for (@{$self->{lru}}) {
894 if (not exists $self->{cache}{$_}) {
895 _ci_warn("$_ is mentioned in the LRU queue, but not in the cache");
899 @duplicate = grep $seen{$_}>1, keys %seen;
901 my $records = @duplicate == 1 ? 'Record' : 'Records';
902 my $appear = @duplicate == 1 ? 'appears' : 'appear';
903 _ci_warn("$records @duplicate $appear multiple times in LRU queue: @{$self->{lru}}");
906 for (keys %{$self->{cache}}) {
907 unless (exists $seen{$_}) {
908 _ci_warn("record $_ is in the cache but not the LRU queue");
913 # Now let's check the deferbuffer
914 # Unless deferred writing is enabled, it should be empty
915 if (! $self->{defer} && %{$self->{deferred}}) {
916 _ci_warn("deferred writing disabled, but deferbuffer nonempty");
920 # Any record in the deferbuffer should *not* be present in the readcache
922 while (my ($n, $r) = each %{$self->{deferred}}) {
923 $deferred_s += length($r);
924 if (exists $self->{cache}{$n}) {
925 _ci_warn("record $n is in the deferbuffer *and* the readcache");
928 if (substr($r, -$rsl) ne $/) {
929 _ci_warn("rec $n in the deferbuffer is missing the record separator");
934 # Total size of deferbuffer should match internal total
935 if ($deferred_s != $self->{deferred_s}) {
936 _ci_warn("buffer size is $self->{deferred_s}, should be $deferred_s");
940 # Total size of deferbuffer should not exceed the specified limit
941 if ($deferred_s > $self->{dw_size}) {
942 _ci_warn("buffer size is $self->{deferred_s} which exceeds the limit of $self->{dw_size}");
946 # Total size of cached data should not exceed the specified limit
947 if ($deferred_s + $cached > $self->{memory}) {
948 my $total = $deferred_s + $cached;
949 _ci_warn("total stored data size is $total which exceeds the limit of $self->{memory}");
956 "Cogito, ergo sum."; # don't forget to return a true value from the file
960 Tie::File - Access the lines of a disk file via a Perl array
964 # This file documents Tie::File version 0.50
966 tie @array, 'Tie::File', filename or die ...;
968 $array[13] = 'blah'; # line 13 of the file is now 'blah'
969 print $array[42]; # display line 42 of the file
971 $n_recs = @array; # how many records are in the file?
972 $#array -= 2; # chop two records off the end
976 s/PERL/Perl/g; # Replace PERL with Perl everywhere in the file
979 # These are just like regular push, pop, unshift, shift, and splice
980 # Except that they modify the file in the way you would expect
982 push @array, new recs...;
984 unshift @array, new recs...;
985 my $r1 = shift @array;
986 @old_recs = splice @array, 3, 7, new recs...;
988 untie @array; # all finished
993 C<Tie::File> represents a regular text file as a Perl array. Each
994 element in the array corresponds to a record in the file. The first
995 line of the file is element 0 of the array; the second line is element
998 The file is I<not> loaded into memory, so this will work even for
1001 Changes to the array are reflected in the file immediately.
1003 Lazy people and beginners may now stop reading the manual.
1007 What is a 'record'? By default, the meaning is the same as for the
1008 C<E<lt>...E<gt>> operator: It's a string terminated by C<$/>, which is
1009 probably C<"\n">. (Minor exception: on dos and Win32 systems, a
1010 'record' is a string terminated by C<"\r\n">.) You may change the
1011 definition of "record" by supplying the C<recsep> option in the C<tie>
1014 tie @array, 'Tie::File', $file, recsep => 'es';
1016 This says that records are delimited by the string C<es>. If the file
1017 contained the following data:
1019 Curse these pesky flies!\n
1021 then the C<@array> would appear to have four elements:
1028 An undefined value is not permitted as a record separator. Perl's
1029 special "paragraph mode" semantics (E<agrave> la C<$/ = "">) are not
1032 Records read from the tied array do not have the record separator
1033 string on the end; this is to allow
1035 $array[17] .= "extra";
1037 to work as expected.
1039 (See L<"autochomp">, below.) Records stored into the array will have
1040 the record separator string appended before they are written to the
1041 file, if they don't have one already. For example, if the record
1042 separator string is C<"\n">, then the following two lines do exactly
1045 $array[17] = "Cherry pie";
1046 $array[17] = "Cherry pie\n";
1048 The result is that the contents of line 17 of the file will be
1049 replaced with "Cherry pie"; a newline character will separate line 17
1050 from line 18. This means that in particular, this will do nothing:
1054 Because the C<chomp>ed value will have the separator reattached when
1055 it is written back to the file. There is no way to create a file
1056 whose trailing record separator string is missing.
1058 Inserting records that I<contain> the record separator string will
1059 produce a reasonable result, but if you can't foresee what this result
1060 will be, you'd better avoid doing this.
1064 Normally, array elements have the record separator removed, so that if
1065 the file contains the text
1071 the tied array will appear to contain C<("Gold", "Frankincense",
1072 "Myrrh")>. If you set C<autochomp> to a false value, the record
1073 separator will not be removed. If the file above was tied with
1075 tie @gifts, "Tie::File", $gifts, autochomp => 0;
1077 then the array C<@gifts> would appear to contain C<("Gold\n",
1078 "Frankincense\n", "Myrrh\n")>, or (on Win32 systems) C<("Gold\r\n",
1079 "Frankincense\r\n", "Myrrh\r\n")>.
1083 Normally, the specified file will be opened for read and write access,
1084 and will be created if it does not exist. (That is, the flags
1085 C<O_RDWR | O_CREAT> are supplied in the C<open> call.) If you want to
1086 change this, you may supply alternative flags in the C<mode> option.
1087 See L<Fcntl> for a listing of available flags.
1090 # open the file if it exists, but fail if it does not exist
1092 tie @array, 'Tie::File', $file, mode => O_RDWR;
1094 # create the file if it does not exist
1095 use Fcntl 'O_RDWR', 'O_CREAT';
1096 tie @array, 'Tie::File', $file, mode => O_RDWR | O_CREAT;
1098 # open an existing file in read-only mode
1099 use Fcntl 'O_RDONLY';
1100 tie @array, 'Tie::File', $file, mode => O_RDONLY;
1102 Opening the data file in write-only or append mode is not supported.
1106 This is an upper limit on the amount of memory that C<Tie::File> will
1107 consume at any time while managing the file. This is used for two
1108 things: managing the I<read cache> and managing the I<deferred write
1111 Records read in from the file are cached, to avoid having to re-read
1112 them repeatedly. If you read the same record twice, the first time it
1113 will be stored in memory, and the second time it will be fetched from
1114 the I<read cache>. The amount of data in the read cache will not
1115 exceed the value you specified for C<memory>. If C<Tie::File> wants
1116 to cache a new record, but the read cache is full, it will make room
1117 by expiring the least-recently visited records from the read cache.
1119 The default memory limit is 2Mib. You can adjust the maximum read
1120 cache size by supplying the C<memory> option. The argument is the
1121 desired cache size, in bytes.
1123 # I have a lot of memory, so use a large cache to speed up access
1124 tie @array, 'Tie::File', $file, memory => 20_000_000;
1126 Setting the memory limit to 0 will inhibit caching; records will be
1127 fetched from disk every time you examine them.
1131 (This is an advanced feature. Skip this section on first reading.)
1133 If you use deferred writing (See L<"Deferred Writing">, below) then
1134 data you write into the array will not be written directly to the
1135 file; instead, it will be saved in the I<deferred write buffer> to be
1136 written out later. Data in the deferred write buffer is also charged
1137 against the memory limit you set with the C<memory> option.
1139 You may set the C<dw_size> option to limit the amount of data that can
1140 be saved in the deferred write buffer. This limit may not exceed the
1141 total memory limit. For example, if you set C<dw_size> to 1000 and
1142 C<memory> to 2500, that means that no more than 1000 bytes of deferred
1143 writes will be saved up. The space available for the read cache will
1144 vary, but it will always be at least 1500 bytes (if the deferred write
1145 buffer is full) and it could grow as large as 2500 bytes (if the
1146 deferred write buffer is empty.)
1148 If you don't specify a C<dw_size>, it defaults to the entire memory
1151 =head2 Option Format
1153 C<-mode> is a synonym for C<mode>. C<-recsep> is a synonym for
1154 C<recsep>. C<-memory> is a synonym for C<memory>. You get the
1157 =head1 Public Methods
1159 The C<tie> call returns an object, say C<$o>. You may call
1161 $rec = $o->FETCH($n);
1162 $o->STORE($n, $rec);
1164 to fetch or store the record at line C<$n>, respectively; similarly
1165 the other tied array methods. (See L<perltie> for details.) You may
1166 also call the following methods on this object:
1172 will lock the tied file. C<MODE> has the same meaning as the second
1173 argument to the Perl built-in C<flock> function; for example
1174 C<LOCK_SH> or C<LOCK_EX | LOCK_NB>. (These constants are provided by
1175 the C<use Fcntl ':flock'> declaration.)
1177 C<MODE> is optional; the default is C<LOCK_EX>.
1179 C<Tie::File> promises that the following sequence of operations will
1182 my $o = tie @array, "Tie::File", $filename;
1185 In particular, C<Tie::File> will I<not> read or write the file during
1186 the C<tie> call. (Exception: Using C<mode =E<gt> O_TRUNC> will, of
1187 course, erase the file during the C<tie> call. If you want to do this
1188 safely, then open the file without C<O_TRUNC>, lock the file, and use
1191 The best way to unlock a file is to discard the object and untie the
1192 array. It is probably unsafe to unlock the file without also untying
1193 it, because if you do, changes may remain unwritten inside the object.
1194 That is why there is no shortcut for unlocking. If you really want to
1195 unlock the file prematurely, you know what to do; if you don't know
1196 what to do, then don't do it.
1198 All the usual warnings about file locking apply here. In particular,
1199 note that file locking in Perl is B<advisory>, which means that
1200 holding a lock will not prevent anyone else from reading, writing, or
1201 erasing the file; it only prevents them from getting another lock at
1202 the same time. Locks are analogous to green traffic lights: If you
1203 have a green light, that does not prevent the idiot coming the other
1204 way from plowing into you sideways; it merely guarantees to you that
1205 the idiot does not also have a green light at the same time.
1209 my $old_value = $o->autochomp(0); # disable autochomp option
1210 my $old_value = $o->autochomp(1); # enable autochomp option
1212 my $ac = $o->autochomp(); # recover current value
1214 See L<"autochomp">, above.
1216 =head2 C<defer>, C<flush>, and C<discard>
1218 See L<"Deferred Writing">, below.
1220 =head1 Tying to an already-opened filehandle
1222 If C<$fh> is a filehandle, such as is returned by C<IO::File> or one
1223 of the other C<IO> modules, you may use:
1225 tie @array, 'Tie::File', $fh, ...;
1227 Similarly if you opened that handle C<FH> with regular C<open> or
1228 C<sysopen>, you may use:
1230 tie @array, 'Tie::File', \*FH, ...;
1232 Handles that were opened write-only won't work. Handles that were
1233 opened read-only will work as long as you don't try to modify the
1234 array. Handles must be attached to seekable sources of data---that
1235 means no pipes or sockets. If you supply a non-seekable handle, the
1236 C<tie> call will try to throw an exception. (On Unix systems, it
1237 B<will> throw an exception.)
1239 =head1 Deferred Writing
1241 (This is an advanced feature. Skip this section on first reading.)
1243 Normally, modifying a C<Tie::File> array writes to the underlying file
1244 immediately. Every assignment like C<$a[3] = ...> rewrites as much of
1245 the file as is necessary; typically, everything from line 3 through
1246 the end will need to be rewritten. This is the simplest and most
1247 transparent behavior. Performance even for large files is reasonably
1250 However, under some circumstances, this behavior may be excessively
1251 slow. For example, suppose you have a million-record file, and you
1258 The first time through the loop, you will rewrite the entire file,
1259 from line 0 through the end. The second time through the loop, you
1260 will rewrite the entire file from line 1 through the end. The third
1261 time through the loop, you will rewrite the entire file from line 2 to
1264 If the performance in such cases is unacceptable, you may defer the
1265 actual writing, and then have it done all at once. The following loop
1266 will perform much better for large files:
1274 If C<Tie::File>'s memory limit is large enough, all the writing will
1275 done in memory. Then, when you call C<-E<gt>flush>, the entire file
1276 will be rewritten in a single pass.
1278 Calling C<-E<gt>flush> returns the array to immediate-write mode. If
1279 you wish to discard the deferred writes, you may call C<-E<gt>discard>
1280 instead of C<-E<gt>flush>. Note that in some cases, some of the data
1281 will have been written already, and it will be too late for
1282 C<-E<gt>discard> to discard all the changes.
1284 Deferred writes are cached in memory up to the limit specified by the
1285 C<dw_size> option (see above). If the deferred-write buffer is full
1286 and you try to write still more deferred data, the buffer will be
1287 flushed. All buffered data will be written immediately, the buffer
1288 will be emptied, and the now-empty space will be used for future
1291 If the deferred-write buffer isn't yet full, but the total size of the
1292 buffer and the read cache would exceed the C<memory> limit, the oldest
1293 records will be flushed out of the read cache until total usage is
1296 C<push>, C<pop>, C<shift>, C<unshift>, and C<splice> cannot be
1297 deferred. When you perform one of these operations, any deferred data
1298 is written to the file and the operation is performed immediately.
1299 This may change in a future version.
1301 A soon-to-be-released version of this module may enabled deferred
1302 write mode automagically if it guesses that you are about to write
1303 many consecutive records. To disable this feature, use
1305 (tied @o)->autodefer(0);
1307 (At present, this call does nothing.)
1311 (That's Latin for 'warnings'.)
1317 This is BETA RELEASE SOFTWARE. It may have bugs. See the discussion
1318 below about the (lack of any) warranty.
1322 Every effort was made to make this module efficient. Nevertheless,
1323 changing the size of a record in the middle of a large file will
1324 always be fairly slow, because everything after the new record must be
1329 The behavior of tied arrays is not precisely the same as for regular
1330 arrays. For example:
1332 # This DOES print "How unusual!"
1333 undef $a[10]; print "How unusual!\n" if defined $a[10];
1335 C<undef>-ing a C<Tie::File> array element just blanks out the
1336 corresponding record in the file. When you read it back again, you'll
1337 get the empty string, so the supposedly-C<undef>'ed value will be
1338 defined. Similarly, if you have C<autochomp> disabled, then
1340 # This DOES print "How unusual!" if 'autochomp' is disabled
1342 print "How unusual!\n" if $a[10];
1344 Because when C<autochomp> is disabled, C<$a[10]> will read back as
1345 C<"\n"> (or whatever the record separator string is.)
1347 There are other minor differences, but in general, the correspondence
1352 Not quite every effort was made to make this module as efficient as
1353 possible. C<FETCHSIZE> should use binary search instead of linear
1354 search. The cache's LRU queue should be a heap instead of a list.
1356 The performance of the C<flush> method could be improved. At present,
1357 it still rewrites the tail of the file once for each block of
1358 contiguous lines to be changed. In the typical case, this will result
1359 in only one rewrite, but in peculiar cases it might be bad. It should
1360 be possible to perform I<all> deferred writing with a single rewrite.
1362 These defects are probably minor; in any event, they will be fixed in
1363 a future version of the module.
1367 The author has supposed that since this module is concerned with file
1368 I/O, almost all normal use of it will be heavily I/O bound, and that
1369 the time to maintain complicated data structures inside the module
1370 will be dominated by the time to actually perform the I/O. This
1371 suggests, for example, that an LRU read-cache is a good tradeoff,
1372 even if it requires substantial adjustment following a C<splice>
1376 You might be tempted to think that deferred writing is like
1377 transactions, with C<flush> as C<commit> and C<discard> as
1378 C<rollback>, but it isn't, so don't.
1384 This version promises absolutely nothing about the internals, which
1385 may change without notice. A future version of the module will have a
1386 well-defined and stable subclassing API.
1388 =head1 WHAT ABOUT C<DB_File>?
1390 C<DB_File>'s C<DB_RECNO> feature does something similar to
1391 C<Tie::File>, but there are a number of reasons that you might prefer
1392 C<Tie::File>. C<DB_File> is a great piece of software, but the
1393 C<DB_RECNO> part is less great than the rest of it.
1399 C<DB_File> reads your entire file into memory, modifies it in memory,
1400 and the writes out the entire file again when you untie the file.
1401 This is completely impractical for large files.
1403 C<Tie::File> does not do any of those things. It doesn't try to read
1404 the entire file into memory; instead it uses a lazy approach and
1405 caches recently-used records. The cache size is strictly bounded by
1406 the C<memory> option. DB_File's C<-E<gt>{cachesize}> doesn't prevent
1407 your process from blowing up when reading a big file.
1411 C<DB_File> has an extremely poor writing strategy. If you have a
1412 ten-megabyte file and tie it with C<DB_File>, and then use
1414 $a[0] =~ s/PERL/Perl/;
1416 C<DB_file> will then read the entire ten-megabyte file into memory, do
1417 the change, and write the entire file back to disk, reading ten
1418 megabytes and writing ten megabytes. C<Tie::File> will read and write
1419 only the first record.
1421 If you have a million-record file and tie it with C<DB_File>, and then
1424 $a[999998] =~ s/Larry/Larry Wall/;
1426 C<DB_File> will read the entire million-record file into memory, do
1427 the change, and write the entire file back to disk. C<Tie::File> will
1428 only rewrite records 999998 and 999999. During the writing process,
1429 it will never have more than a few kilobytes of data in memory at any
1430 time, even if the two records are very large.
1434 Since changes to C<DB_File> files only appear when you do C<untie>, it
1435 can be inconvenient to arrange for concurrent access to the same file
1436 by two or more processes. Each process needs to call C<$db-E<gt>sync>
1437 after every write. When you change a C<Tie::File> array, the changes
1438 are reflected in the file immediately; no explicit C<-E<gt>sync> call
1439 is required. (Or you can enable deferred writing mode to require that
1440 changes be explicitly sync'ed.)
1444 C<DB_File> is only installed by default if you already have the C<db>
1445 library on your system; C<Tie::File> is pure Perl and is installed by
1446 default no matter what. Starting with Perl 5.7.3 you can be
1447 absolutely sure it will be everywhere. You will never have that
1448 surety with C<DB_File>. If you don't have C<DB_File> yet, it requires
1449 a C compiler. You can install C<Tie::File> from CPAN in five minutes
1454 C<DB_File> is written in C, so if you aren't allowed to install
1455 modules on your system, it is useless. C<Tie::File> is written in Perl,
1456 so even if you aren't allowed to install modules, you can look into
1457 the source code, see how it works, and copy the subroutines or the
1458 ideas from the subroutines directly into your own Perl program.
1462 Except in very old, unsupported versions, C<DB_File>'s free license
1463 requires that you distribute the source code for your entire
1464 application. If you are not able to distribute the source code for
1465 your application, you must negotiate an alternative license from
1466 Sleepycat, possibly for a fee. Tie::File is under the Perl Artistic
1467 license and can be distributed free under the same terms as Perl
1476 To contact the author, send email to: C<mjd-perl-tiefile+@plover.com>
1478 To receive an announcement whenever a new version of this module is
1479 released, send a blank email message to
1480 C<mjd-perl-tiefile-subscribe@plover.com>.
1482 The most recent version of this module, including documentation and
1483 any news of importance, will be available at
1485 http://perl.plover.com/TieFile/
1490 C<Tie::File> version 0.50 is copyright (C) 2002 Mark Jason Dominus.
1492 This library is free software; you may redistribute it and/or modify
1493 it under the same terms as Perl itself.
1495 These terms are your choice of any of (1) the Perl Artistic Licence,
1496 or (2) version 2 of the GNU General Public License as published by the
1497 Free Software Foundation, or (3) any later version of the GNU General
1500 This library is distributed in the hope that it will be useful,
1501 but WITHOUT ANY WARRANTY; without even the implied warranty of
1502 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1503 GNU General Public License for more details.
1505 You should have received a copy of the GNU General Public License
1506 along with this library program; it should be in the file C<COPYING>.
1507 If not, write to the Free Software Foundation, Inc., 59 Temple Place,
1508 Suite 330, Boston, MA 02111 USA
1510 For licensing inquiries, contact the author at:
1514 Philadelphia, PA 19107
1518 C<Tie::File> version 0.50 comes with ABSOLUTELY NO WARRANTY.
1519 For details, see the license.
1523 Gigantic thanks to Jarkko Hietaniemi, for agreeing to put this in the
1524 core when I hadn't written it yet, and for generally being helpful,
1525 supportive, and competent. (Usually the rule is "choose any one.")
1526 Also big thanks to Abhijit Menon-Sen for all of the same things.
1528 Special thanks to Craig Berry and Peter Prymmer (for VMS portability
1529 help), Randy Kobes (for Win32 portability help), Clinton Pierce and
1530 Autrijus Tang (for heroic eleventh-hour Win32 testing above and beyond
1531 the call of duty), and the rest of the CPAN testers (for testing
1534 Additional thanks to:
1539 Tassilo von Parseval /
1543 Autrijus Tang (again) /
1548 Test DELETE machinery more carefully.
1550 More tests. (C<mode> option. _twrite should be tested separately,
1551 because there are a lot of weird special cases lurking in there.)
1553 More tests. (Stuff I didn't think of yet.)
1561 Maybe an autolocking mode?
1565 Record locking with fcntl()? Then you might support an undo log and
1566 get real transactions. What a coup that would be. All would bow