open (FILE,"external_file.txt"); open (TEMP,">external_file.txt.tmp"); my $i = 0; while (){ # magical - puts every line in $_, until the end of file is reached if ($i == 3){ # if $i is the 3rd line chomp $_; # remove the line break at the end of $_ $_ .= "my new line goes here\n"; # add the string } print TEMP $_; # print the line you just read to the temporary file } continue { # performed at the end of every loop iteration $i++; } close FILE; close TEMP; unlink ("external_file.txt"); # incase rename does not clobber, delete the original rename ("external_file.txt.tmp","external_file.txt");