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

I would like to make a change to a relatively small file. Is it possible to make change to the text without having to read it in, make the change, and write it out?

Thanks

  • Comment on Is it possible to alter a flat file in memory?

Replies are listed 'Best First'.
Re: Is it possible to alter a flat file in memory?
by broquaint (Abbot) on May 21, 2003 at 17:24 UTC
    Sure, you could always use Tie::File or put the ever-handy -i switch to use e.g
    use Tie::File; tie my @fh, 'Tie::File', "yourfile" or die("ack - $!"); s/$ARGV[0]/$ARGV[1]/g for @fh;
    Or
    perl -pi -e 's/THIS/THAT/g' yourfile

    HTH

    _________
    broquaint

      broquaint,
      I learn new stuff every day, so forgive me if this sounds obvious.

      The magic of the -i doesn't preclude the file from being opened, read, written, and closed does it?

      I thought it opened the original file, wrote a new temporary file as it modified lines as appropriate, and then overwrote the original by moving the temporary file in place.

      If that really isn't how it works under the hood - please let me know. I think for all intents and purposes, it meets the OP's needs. Besides - Tie::File has to read the entire file to index the newlines as well for the array - or am I wrong about that too?

      No need to reply if my assumptions are correct - thanks.

      Cheers - L~R

        The magic of the -i doesn't preclude the file from being opened, read, written, and closed does it?
        It renames the given file then creates an empty file in it's place and writes to that (see. the -i section of perlrun).
        I think for all intents and purposes, it meets the OP's needs
        Indeed. I considered giving a solution where the code did most of the work inplace, but I figured that the OP just wanted ease of use (that's why they're using perl afterall :)
        Besides - Tie::File has to read the entire file to index the newlines as well for the array - or am I wrong about that too?
        It does indeed read the whole file into memory, but as its Dominus' code it's all terrifically optimal :)
        HTH

        _________
        broquaint

Re: Is it possible to alter a flat file in memory?
by del (Pilgrim) on May 21, 2003 at 17:29 UTC
    Try Tie::File . You are still reading and writing the file, but it will keep you from having to call open and close directly.
Re: Is it possible to alter a flat file in memory?
by halley (Prior) on May 21, 2003 at 17:23 UTC

    Some operating systems support "memory mapped files," and there may indeed be a module which offers an API to such features in Perl.

    But memory mapped files are just glossing over the details of the inevitable: the only way to change some bits on a drive is to read the drive, modify the bits, and write the bits onto the drive. What the API looks like doesn't really change that.

    --
    [ e d @ h a l l e y . c c ]