Hi there!
You definitely want Jason-Mark Dominus's Tie::File CPAN module (part of the CORE since Perl 5.8.0)
An example:
use Tie::File for my $file (@files) { my @file; tie @file, 'Tie::File', $file or die $!; for my $line ( @file ) { # do stuff on the lines you want # it reflects 'inmediately' on the file } untie @file; }
Definitely million times better!
best regards,
Update:
While "perl -wnli.bak -e 's///;s///...' *" will make a backup copy of the files processed so far (if it happens to crash), it will scan the whole file, no matter what. From your example that seems what you need, so it's a very good reply (I am voting for it) but I forgot to add that if you know which lines you are changing (headers, or if they come from a template) with Tie::File you can access individual lines using $file5 (the 6th line). What's more, you can easily cut the loop ig you know all changes will be in the first 50 lines of header, or whatever. Tie::File only reads as many lines as needed to get the job done, and it's very fast (not new files, works inside the file itself) and AFAIK completely reliable. You can get it from CPAN (www.cpan.org) (perl -MCPAN -e 'install Tie::File' if you have it configured to go beyond a proxy; try doing perl -MCPAN -e shell first, and write the 'install Tie::File' there) for Perl 5.6.1 (I have it in production code with this release of Perl on GNU/Linux) or if you're using ActivePerl, try using the tool to install compiled modules in Perl (was it called ppm?). Good luck,
--
our $Perl6 is Fantastic;
In reply to Re: Changing data in alot of files
by Excalibor
in thread Changing data in alot of files
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |