in reply to Read and write files concurrently in Perl

If your plan is to replace the contents of the file at certain locations, then you'll be using seek, as suggested above by kyle.

But in order for that to work as expected, you'll have to be careful to make sure that the replacement data you put in contains exactly the same number of bytes as the original data you are replacing in the file.

For example, suppose you have a string like "foobar", starting at byte offset 20 in the file. If you seek to that offset and write "goober" (same number of bytes), that will "replace" the foobar string just fine. But if you seek to byte offset 20 and write something shorter, the last byte(s) of "foobar" will still be in file unchanged, and if you write something longer, whatever follows "foobar" will be overwritten as well.

If your plan involves cases where the file should end up with a different byte count when you're done, you won't be able to do the editing "in place". You can try looking at Tie::File, although I'm not sure it does what you want in particular.

If you want better help, try showing us a small amount of real data, together with some code that you've tried, and tell us what kind of result you really want, given that your code isn't producing that result.

  • Comment on Re: Read and write files concurrently in Perl