#!/usr/bin/perl -w use strict; # Open the file in read mode open FILE, "file.txt" or die "Error message here $!"; # Slurp in the file my $str = do{local $/; }; close FILE or die "Error message here $!"; # Reopen the file in write mode (clobbers old file) open FILE, ">file.txt" or die "Error message here $!"; # Add some text print FILE "Prepend this text.\n"; # Add the old contents of the file. print FILE $str; close FILE or die "Error message here $!"; #### #!/usr/bin/perl -w use strict; # Open the file for reading and writing open FILE, "+}; # Rewind the file seek FILE, 0, 0; # Add some text print FILE "Prepend this text.\n"; # Add the old contents of the file. print FILE $str; close FILE or die "Error message here $!";