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

hi fellas how can i write in fron of a file? thanks

Replies are listed 'Best First'.
Re: Writing infront of the file
by merlyn (Sage) on Aug 09, 2000 at 14:44 UTC
Re: Writing infront of the file
by BigJoe (Curate) on Aug 09, 2000 at 17:14 UTC
    First read what merlyn posted. There is a great deal of good information that comes out of these documents. But for ideas here is one
    use strict; my $wholepage = `/bin/cat filename.txt`; open(OUTFILE, ">filename.txt") || die "Could not open outfile $!"; print OUTFILE "your lines"; print OUTFILE $wholepage; close OUTFILE;
    Remember this was just an idea. It probably isn't close to best solution.

    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
Re: Writing infront of the file
by fundflow (Chaplain) on Aug 10, 2000 at 00:37 UTC
    Note that while this is possible, in many file-systems this means alot of copies, as they are designed for appending to files in the end (and even then this causes problems such as fragmantation).

    If you expect this file to grow large, it might be better to store it in reverse and then read it backwards or just reverse it (check the FAQ) once you are done.


    Just wondering, why do you need it anyway?
Re: Writing in front of the file
by turnstep (Parson) on Aug 09, 2000 at 19:49 UTC

    Here is how I would do it:

    my $file = "/foo/bar.txt"; open(FOO, "+< $file") or die qq[Could not open "$file": $!\n]; @slurp = <FOO>; seek(FOO, 0, 0); print FOO "I am at the very start of the file!\n"; print FOO @slurp; truncate(FOO, tell(FOO)); close(FOO);