http://www.perlmonks.org?node_id=638423


in reply to Need a little help appending lines

It's generally a bad idea to edit a file in place.

What I'd suggest is that you create a new file with the desired content, and the move the new file to the place of the old file.

You could first read things that you want to append into @list, and then do the "real" operation:

use File::Copy; # for move() open my $of, '<', $old_file or die "Can't read $old_file: $!"; open my $nf, '>', $new_file or die "Can't write $new_file: $!"; my $line_number = 0; while (my $line = <$of>){ chomp $line; print $nf $line, $list[$line_number]; } close $nf; close $of; move $new_file, $old_file;