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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on HELP!! urgent!writing contents toa specific line in an existing file

Replies are listed 'Best First'.
Re: HELP!! urgent!writing contents toa specific line in an existing file
by Corion (Patriarch) on Nov 14, 2007 at 13:52 UTC

    Have you looked at Tie::File? Otherwise, you will have to read in the file, make your changes and then write out the whole file again.

    Good luck with your teacher!

Re: HELP!! urgent!writing contents toa specific line in an existing file
by Joost (Canon) on Nov 14, 2007 at 14:23 UTC
Re: HELP!! urgent!writing contents toa specific line in an existing file
by cdarke (Prior) on Nov 14, 2007 at 14:29 UTC
    If you are on *nix, look at the file using od -xc filename. When you say that a line is "blank" what do you mean? I suspect you mean that this position in the file is taken up by a newline ("\n" on Unix, "\r\n" on Windows). This is only a single character, and if you opened the file for read-write ('+<') then you would still have a problem of fitting 6 characters ("hello\n") where there is only room for one.

    Text files on UNIX and Windows are just a single stream of bytes, they are not physically split into records. The "lines" which we see are only a convention and convienient way of viewing them.
Re: HELP!! urgent!writing contents toa specific line in an existing file
by locked_user sundialsvc4 (Abbot) on Nov 15, 2007 at 03:24 UTC

    Basically, friend, you're dealing with several problems at once... and you need to sort them out. Let's try to do that...

    First of all, whenever a computer-program needs to regard a file as is input, it probably needs to first open that file for input, and read that file completely, before it opens the same file for output ... because, generally speaking, "opening a file for output" erases it.

    (Opening a file for "read/write" will not erase it.)

    Let us assume for the sake of argument what is usually the case: that the input file and the output file for a particular program are separate and distinct files. If this be the case, then our problem is simplified: we merely need to recognize "blank lines" and substitute them with "whatever..."

Re: HELP!! urgent!writing contents toa specific line in an existing file
by bart (Canon) on Nov 15, 2007 at 12:01 UTC
    There's an entry in perlfaq5, How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?. The current answer is quite boring:
    Use the Tie::File module, which is included in the standard distribution since Perl 5.8.0.

    I like the old answer (perl 5.6.x) much better — and much more fitting for solving a homework problem:

    Those are operations of a text editor. Perl is not a text editor. Perl is a programming language. You have to decompose the problem into low-level calls to read, write, open, close, and seek.

    Although humans have an easy time thinking of a text file as being a sequence of lines that operates much like a stack of playing cards -- or punch cards -- computers usually see the text file as a sequence of bytes. In general, there's no direct way for Perl to seek to a particular line of a file, insert text into a file, or remove text from a file.

    (etc...)

      I liked that old answer so much I copied it here. It's a shame they gutted the entry.