my $inp = "abc.xml"; my $out = "new_abc.xml"; # or maybe some more meaningful name... open( INP, $inp ) or die "$inp: $!" ); open( OUT, $out ) or die "$out: $!" ); my @array = ; # read all input lines into @array # ... $xml_add has to come from somewhere ... $array[$#array] = $xml_add; # replace last line print OUT join "", @array;