It's not especially hard to prepend information. You can do it by in-place editing of one file, but what I usually do is open the old file for reading, open a
new file for writing, add the new information to the new file and then copy everything from the old file into the new file. After that, assuming it all goes well, you can switch the names easily (via Perl or via the shell) so that the old file becomes "file.bak" and the new file gets the name of the old file. If you do that all within one Perl script, it happens so quickly that it looks like you edited in place, but you didn't.
Here's an example of what this might look like:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
# Get everyone's names settled
my $old = 'foo.txt';
my $new = 'new.txt';
my $bak = $old . '.bak';
# Open everyone; check for errors
open my $in, '<', $old
or die "Can't open $old for reading: $!";
open my $out, '>', $new
or die "Can't open $new for writing: $!";
# First print new lines to the new file since we want these lines on the top
print $out "\n";
print $out "That's an empty line above this one.\n";
print $out "\n";
print $out "There's another.\n";
# Now copy all the lines of $old to $new - below the new lines
while (my $line = <$in>) {
print $out $line;
}
# Close everyone and check for any errors
close $in or die "Can't close $old: $!";
close $out or die "Can't close $new: $!";
# Rename so that $old is $bak and $new is $old
rename $old, $bak or die "Failed to rename $old to $bak: $!";
rename $new, $old or die "Failed to rename $new to $old: $!";