use strict; use warnings; my @new_lines = some_function; # something that retrieve new lines my $file_path = 'file_path.ext'; open my $orig_file, '<', $file_path or die "unable to open '$file_path' for read!"; my @orig_lines = <$orig_file>; close $orig_file; #reopen it wiping the content open my $new_file, '>', $file_path or die "unable to open '$file_path' for write!"; #print first new get lines and then old ones print $new_file $_ for @new_lines,@orig_lines;