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

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

Replies are listed 'Best First'.
Re^3: Elegant Way of Inserting Text at the Start of the File
by ig (Vicar) on Nov 19, 2008 at 03:04 UTC

    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?