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 | [reply] [d/l] [select] |
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
| [reply] |
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
| [reply] |
Try Tie::File . You are still reading and writing the file, but it will keep you from having to call open and close directly. | [reply] |
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 ]
| [reply] |