You haven't really shown us enough of your code for us to be very much help. But, in particular, you haven't shown any code where you write the changed data back to an output file.
In general, tasks like this boil down to three stages:
- Read data from file.
- Update data in some way.
- Write changed data back to file.
One common approach is to have two filehandles open - one to your input file and another to a new output file. That makes it simple to process the file a line at a time.
while (<$input_fh>) {
if (this is a line you need to change) {
# make changes to line (which is in $_)
# Perhaps print an extra line here.
}
print $output_fh $_;
}
Another approach (which trades speed for ease of use) is to use the Tie::File module (which is part of all Perl distributions since 5.8).
As always, the Perl FAQ is a good place for more information. In this case, you probably want to look at perlfaq5, which contains the question How do I change, delete, or insert a line in a file, or append to the beginning of a file?
Update: You already have one (slightly problematic) Tie::File-based solution. So here's mine:
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
tie my @lines, 'Tie::File', 'somefile.txt'
or die "Can't tie file: $!\n";
for (@lines) {
$_ .= "\nTEST line" if $_ eq 'second line';
}