 <p>I think this is what you want to do... </p> <pre><code>use warnings; use strict; use File::Copy; my $target_file = "testfile"; my $tmp_file = "$target_file.new"; my $str = "some string with stuff"; open my $fh, "&lt;", "testfile"; open my $w_fh, "&gt;&gt;", "testfile.new"; # loop over your current file, one line at a time while( my $line = &lt;$fh&gt; ){ # remove the '\n' so we can add to the existing line chomp $line; # add what you'd like, plus the '\n' my $full_line = "$line $str\n"; # and print this to a tmp file print $w_fh $full_line; } close $fh; close $w_fh; unlink $target_file or die "unable to delete $target_file: $!"; # use the File::Copy sub 'move' # to rename the tmp file to the original name move($tmp_file, $target_file); </code></pre> <p>Running the code:</p> <pre><code>$ cat testfile this is three this is three this is three $ test.pl $ cat testfile this is three some string with stuff this is three some string with stuff this is three some string with stuff </code></pre>