The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

prepend_file

This sub writes data to the beginning of a file. The previously existing data is written after that so the effect is prepending data in front of a file. It is a counterpart to the append_file sub in this module.

  use File::Slurp qw( prepend_file ) ;
  prepend_file( 'filename', @data ) ;

The first argument to prepend_file is the filename. The next argument is an optional hash reference and it contains key/values that can modify the behavior of prepend_file. The rest of the argument list is the data to be written to the file.

  prepend_file( 'filename', {binmode => ':raw'}, $buffer ) ;

prepend_file works by calling read_file and then write_file with the new data and the existing data just read in. Read the documentation for those calls for details not covered here.

The options are:

binmode

If you set the binmode option, then its value is passed to a call to binmode on the opened handle. You can use this to set the file to be read in binary mode, utf8, etc. See perldoc -f binmode for more.

        prepend_file( $bin_file, {binmode => ':raw'}, @data ) ;
        prepend_file( $bin_file, {binmode => ':utf8'}, $utf_text ) ;

err_mode

You can use this option to control how prepend_file behaves when an error occurs. See the documentation in read_file and write_file for more on this.

atomic mode is always enabled

In the internal call to write_file, the atomic flag is always set which means you will always have a stable file, either the old one or the complete new one. See the atomic option in write_file for more on this.