in reply to Elegant Way of Inserting Text at the Start of the File

Perhaps open the file for appendation (noun form of append :P) and seek to the beginning of the file. Then just print FILEHANDE "new text";

May not be that elegant, though. :\

Update: Don't know what I am talking about - I just assumed that I did :D.

I'm so adjective, I verb nouns!

chomp; # nom nom nom

Replies are listed 'Best First'.
Re^2: Elegant Way of Inserting Text at the Start of the File
by ikegami (Patriarch) on Nov 19, 2008 at 01:29 UTC

    That won't insert. You can't insert into a file. You need to move everything that comes afterwards yourself.

    My test (Windows and linux) shows it still appends despite the seek.

    >copy con file foo bar ^Z 1 file(s) copied. >perl -le"open my $fh, '>>', 'file' or die $!; seek $fh, 0, 0 or die $ +!; print $fh 'baz';" >type file foo bar baz

      You can use '+<' to open the file for read and write without truncating. Then seek does work and the print overwrites the beginning of the file.

      $ echo -ne "foo\nbar\n" > file $ cat file foo bar $ perl -le'open my $fh, "+<", "file" or die $!; seek $fh, 0, 0 or die +$!; print $fh "bazz";' $ cat file bazz ar
        I know. How does that help?