in reply to Rewriting into file
You can do it using one of the easy ways, or one of the hard ways. The hard way is to open the file for read/write, read the target line, formulate a replacement line of exactly the same number of bytes, seek back to the start of the line, and write it back out. Be sure to obtain a file lock while you're there.
An easier way would be to open a temporary output file (see File::Temp), read the input file, simultaneously writing to your temp file. When you get to the line(s) you need to change, make your change and write them out to the output file too. Continue through to the end of the file. Close both files. Rename the output file over the input file. It would be wise to test your output thoroughly, at least while in development to make sure your plan worked before you go clobbering your input file. If there's any possibility your input file could get modified while you're working be sure to obtain a lock.
Another easy solution is to use Tie::File. Find the line(s) of interest, make your change, and make your changes. Tie::File handles some of the details without you dealing with them. But locking could still be important in a multi-user environment.
I would prefer the second solution if it were me. An alternate on that strategy would be to rename the input file to input.name.bak, iterate over it, writing to a new file with the original file's name. Now you've got a backup.
Dave
|
|---|