Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

writing to a file

by Anonymous Monk
on Jul 01, 2005 at 09:32 UTC ( #471613=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am aware of how to write to files involving overwrite and append but is it possible to write text to the first line of a file rather than appending to the end? Thanks

Replies are listed 'Best First'.
Re: writing to a file
by virtualsue (Vicar) on Jul 01, 2005 at 09:44 UTC
    This is a frequently asked question, and is therefore covered in the Perl FAQs
Re: writing to a file
by Joost (Canon) on Jul 01, 2005 at 09:44 UTC
    Not in general. I believe some operating systems / file systems support these kind of actions (when they use a record-based system for storing lines of text instead of using one large stream of bytes), but generally (at least on *nix and windows), the best you can do is write a new file and then move it over the original.

    There are some perl modules that do what you describe, but they're generally not very efficient. See Tie::File for an example. For large files, you'd probably be better off using 2 files as described above.

Re: writing to a file
by joeface (Pilgrim) on Jul 01, 2005 at 14:55 UTC
    You can use Tie::File to treat the file like an array, then use unshift to add to the beginning of the array
    use Tie::File; my $file = 'filename'; my @tiedfile; my $new_content = "foo"; ## Whatever you want to prepend to the file tie(@tiedfile, 'Tie::File', $file); unshift(@tiedfile, $new_content); untie(@tiedfile);
    If you need to add more than one line, you can get your new content into an array and loop through it:
    foreach my $line (reverse(@new_content)) { unshift(@tiedfile, $line); }
Re: writing to a file
by sh1tn (Priest) on Jul 01, 2005 at 09:53 UTC
    Sure it's possible:
    open FH, "+< $filename" or die $!; seek FH, 0, 0 or die $!; print FH ""; # printing to the first line

      That reminds me of the user who asked the admin for more disk space.

      user: I need more disk space
      admin (types): You have 20 MB free
      user: Oh great, then I have 40 MB all in all now.
      admin: err, no.

      holli, /regexed monk/
      That would overwrite from the beginning rather than append. Unless the file is small enough to read into an array, unshift the new first line in, do the same zero-seek and then print it back in this overwrite mode, a much more complex solution would be required using read aheads, rewriting the previous buffer and iterating through the whole file.

      One world, one people

Re: writing to a file
by TedPride (Priest) on Jul 02, 2005 at 00:31 UTC
    Open a handle to the old file. Open a handle to a new file. Print your start material to the new file. Run through the old file in chunks and print it to the new file. Delete (unlink) the old file. Rename the new file to the old file name.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://471613]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-02-18 22:30 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.