これがあなたのやりたいことだと思います...
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, "<", "testfile";
open my $w_fh, ">>", "testfile.new";
# loop over your current file, one line at a time
while( my $line = <$fh> ){
# 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);
コードの実行:
$ 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