Here's another common problem which is trivially solved by Tie::File. How do I add a new record at the beginning of a file instead of at the end? unshift @lines, $new; # Or add more than one record