Tie::File (current version: 0.96; last updated 2003-05-27)
Tie::File associates the lines of a file with elements of
an array. If you read or change the array, that reads or changes the
file in the corresponding way. It's safe, fast, and easy.
perlfaq5 contains the question:
How do I change one line in a file/delete a line in a
file/insert a line in the middle of a file/append to the
beginning of a file?
Tie::File is the answer to this question.
- To change one line in a file, do $line[$n] = $new;.
- To delete a line from a file, use splice @line, $n, 1, or shift @line (to delete the first line) or pop
@line
(to delete the last line).
- To
insert a line in the middle of a file, use splice @line, $n, 0,
$new.
- To append to the beginning of a file, use unshift
@line, $new.
Tie::File also answers some other questions:
- push @line, $new works and does what you expect: It
appends a new line to the end of a file.
- $#line = 3 works and does what you expect: It
sets the size of the file to 4. (This is the same as what it would
do to a regular array.) If the file was longer than 4 lines, the
extra lines are removed. If the file was shorter than 4 lines, it
is padded out with extra empty lines.
- @line = ("I", "like", "pie") works and does what you
expect: It throws away the entire contents of the file and replaces
it with the three lines I, like, and pie.
- for (@line) { s/PERL/Perl/g } works and does what you
expect: It searches through the file and replaces every occurrence
of PERL with Perl.
Changing a file is now as easy as changing an array.
To join a low-volume mailing list for Tie::File
announcements, send a blank mail message to mjd-perl-tiefile-subscribe@plover.com,
or fill in the form below:
What's New in 0.96
What's New in 0.95
- Flushing out the deferred-write buffer is now done in a single pass,
regardless of whether the records in the buffer are contiguous.
- Various time and memory performance enhancements courtesy of Tels.
- flock method now discards read cache and offsets table when
successful.
- Miscellaneous minor bugs fixes and test suite enhancements.
- New offset method for recovering offset table data. (Tels again.)
What's New in 0.93
- No significant changes. (Release was mainly to correct an
error in one of the test files.)
What's New in 0.92
- Bug fixes: Negative nrecs argument to splice
is now handled correctly. Tie::File now behaves correctly even if
you change $\.
- More tests and (very) minor performance enhancements.
What's New in 0.91
- Tie::File now correctly handles attempts to store
undefined values in the array: they are silently converted to
defined empty strings.
- If the last record of the file is not properly terminated with
the record terminator string, and if the file was opened for
writing, Tie::File will silently repair it. Formerly, it
would mangle the file.
- More unit tests.
- Performance enhancements.
What's New in 0.90
- Although I've released many partial versions over the past few
weeks, this is the first complete release. All the features I
originally planned are present and well-tested. Efficiency is good.
For complete details of earlier versions, see the
WHATSNEW file.
Return to:
Universe of Discourse main page |
What's new page |
Perl Paraphernalia
mjd-perl-tiefile+@plover.com
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.