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

Hello again, I know that it's possible to create a file, and then write too it fairly easily in perl. But what about writing to a file that already exists, I have I notion that this must be possible. I'd like to take it a step further; Is it possible to write to a specified spot in a file, (not the beginning or end...the middle). If so, can the write location determined by linenum or by regexs, or (hopefully) either/both.
I know that you can overwrite any/all text w/ perl by using the '>$file' (or '>>$file' to append) open method, but what about just overwriting certain specified lines? Any generic code or links would be must appreciated, TIA, Average Joe user.

Replies are listed 'Best First'.
Re: Write to file
by Fletch (Bishop) on Oct 17, 2005 at 18:59 UTC
    freebie:~ 716> perldoc -q 'delete a line' Found in /System/Library/Perl/5.8.6/pods/perlfaq5.pod How do I change one line in a file/delete a line in a file/inse +rt a line in the middle of a file/append to the beginning of a file? Use the Tie::File module, which is included in the standard dis +tribu- tion since Perl 5.8.0.
Re: Write to file
by ikegami (Patriarch) on Oct 17, 2005 at 20:27 UTC

    As mentioned (indirectly) by Fletch, Tie::File probably fits your needs best. It allows you to treat a file as an array. Anything you do to the array will be reflected by the file. I just wanted to provide some usage tips:

    • To remove a line from the file, use splice. For example, splice(@tied_to_file, 10, 1); removes one line, starting with the 11th one (0-based indexes).

    • splice is also used to insert lines. For example, splice(@tied_to_file, 10, 0, @lines_to_insert); inserts the provided lines before the 11th line (0-based indexes).

    • You can use grep to apply filters to every line of your file. For example, @tied_to_file = grep { !/DELETE ME/ } @tied_to_file;

    • You can use map to apply transformations to every line of your file. For example, @tied_to_file = map { uc($_) } @tied_to_file;

Re: Write to file
by blue_cowdawg (Monsignor) on Oct 17, 2005 at 20:25 UTC
        I'd like to take it a step further; Is it possible to write to a specified spot in a file, (not the beginning or end...the middle).

    Take a look at Tie::File and see what you think. It allows you to treat a file as if it were an array and do the the file anything else you would do to an array of scalars.

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Write to file
by Trix606 (Monk) on Oct 17, 2005 at 18:56 UTC
    It sounds like seek or sysseek is what you are looking for.
      I thought seek was only to find the beginning and/or end of a file?

        seek will allow you to position the file pointer to any offset within the file. The problem is that files are ordered sequences of octets, not lines. Unless you're dealing with fixed line lengths you're more than likely going to overwrite something if you try and do things in place yourself. You want to use the module mentioned in the FAQ I've referenced.