File::LineEdit - Small utility for editing each line of a file
# object interface my $le = File::LineEdit->new('myfile.txt'); foreach my $line (@$le) {$line =~ s|foo|bar|}
# tied array interface my (@le); tie @le, 'File::LineEdit::Tie', 'myfile.txt'; foreach my $line (@le) {$line =~ s|foo|bar|} untie @le;
File::LineEdit can be installed with the usual routine:
perl Makefile.PL
make
make test
make install
You can also just copy LineEdit.pm into the File/ directory of one of your library trees.
File::LineEdit is just a small utility to simplify modiyfying a file line-by-line. It performs the boring tasks of slurping in the file, chomping each line (if you want it to), and then, after changes are made, writing the data back to the file.
The basic usage is quite simple: instantiate a File::LineEdit object, loop through the object as if it were an array, modifying each line however you want. When the object falls out of scope, it automatically writes the modified lines back to the file. Here's a simple example (actually, the same example used in the synopsis above, this time with a little more documentation):
# instantiate a File::LineEdit object, passing in # the path to the file as the only required argument. my $le = File::LineEdit->new('myfile.txt'); # loop through the lines in the file foreach my $line (@$le) { # change the line in some way $line =~ s|foo|bar|; } # The data is saved back to the file # automatically when the object falls # out of scope. No need for an # explicit save.
There are a few variations on this theme.
By default, LineEdit objects save their line data back
If you just somehow don't trust the concept of saving on object destruction, you can tell your LineEdit object to explicitly save:
$le->save;
If you don't want the object to automatically save on destruction,
add the autosave argument to the instantiation params:
my $le = File::LineEdit->new('myfile.txt', autosave=>0);
You might prefer to slurp data in from one file, then save to another.
To do so, just add the savepath parameter to the new() call.
For example, the following command slurps from source.txt and
saves to target.txt:
my $le = File::LineEdit->new('source.txt', savepath=>'target.txt');
You can also set the save path after object creation by setting
the savepath property:
By default, LineEdit automatically chomps the end of each line when it slurps
in the data from the file. If you prefer to keep your lines unchomped then
add an autochomp argument to the instantiation params:
my $le = File::LineEdit->new('myfile.txt', autochomp=>0);
You can also use File::LineEdit as a tied array. Just tie your array to File::LineEdit::Tie, passing the file path as the only argument:
# tied array interface my (@le); tie @le, 'File::LineEdit::Tie', 'myfile.txt'; foreach my $line (@le) {$line =~ s|foo|bar|} untie @le;
There are a couple modules on CPAN that provide similar functionality. File::Searcher provides the ability to do regular expression based search and replaces on groups of files. File::Data provides several ways to slurp in, modify, and write files. File::Data also uses regular expressions for searching and replacing. Be sure to look at both of those modules if you are interested in simplified modification of files.
File::LineEdit differs from File::Searcher and File::Data in that File::LineEdit focuses on line-based edits. The object of File::LineEdit is to provide a simplified manner for looking at and modifying files one line at a time.
Copyright (c) 2003 by Miko O'Sullivan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This software comes with NO WARRANTY of any kind.
Miko O'Sullivan miko@idocs.com
Useful contribution by Dan Brook.
Initial release
Added overloading so that you can reference the LineEdit object itself as if it were a reference to an array. That was Dan Brook's idea. Thanks Dan!
Then I took Dan's idea a step further and added the ability to tie File::LineEdit to an array using File::LineEdit::Tie.
Added ability to set the object to save to a different file than the file the data was slurped from. Also tidied up documentation a little.