in reply to Is it possible to alter a flat file in memory?

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

Replies are listed 'Best First'.
Re: Re: Is it possible to alter a flat file in memory?
by Limbic~Region (Chancellor) on May 21, 2003 at 23:12 UTC
    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

        broquaint,
        I have been going through my old nodes looking for interesting stuff. I aplogize for a reply almost 3 years later.

        There is a slight difference between my statement of
        "has to read the entire file to index the newlines..."
        and your statement of
        "It does indeed read the whole file into memory,.."

        As best as I understand Tie::File, it doesn't actually read the file into memory. It reads the entire file yes, but only enough stays in memory as is needed while it searches for input record separators for indexing.

        Cheers - L~R