sbank has asked for the wisdom of the Perl Monks concerning the following question:

How do a append something to the beginning of a file? I try the following but it doesn't seem to work.
use IO::Seekable; open(OUTPUT, ">> $outfile") or die "can't open file $outfile: $!"; seek OUTPUT, 0, 0; # seek to the beginning of the file? print OUTPUT "\037\213"; # append gzip magic (0x1F 0x8B) close OUTPUT;
But when I look at the $outfile, I see 0x1F and 0x8B at the end of the file.

Edit: chipmunk 2001-05-22

Replies are listed 'Best First'.
Re: Appending to the embeginning/em of a file.
by merlyn (Sage) on May 22, 2001 at 21:35 UTC

      i think what merlyn is trying to tell you is that you do it by rewriting the whole file... meaning, read it in, and write it back out with what you want in front of it.

      seek, if i have it right, affects only whence you read, not whither you write.

      (in other words, as some might say, "it can't be done" -- appending is inherently to the end of a file. sad, isn't it?)

      .
      you can look into This module which provides and easy OO way of working with files.