in reply to Handling large files
The problem is that unless you read the file and count record terminators (usually '\n'), you can't possibly know how many bytes into the file a particular line is. ...unless each line is of uniform length, in which case simple math and seek is all it takes. But the fact is that there is no master index, by default, of a file telling Perl or any other program what offset within the file each line starts at.
Imagine the scenario of someone telling you "Take the third right turn after the stop light." Then you get in your car, and decide, "I'll bet the third right past the stop light is in exactly 2.3 miles."... having never looked at a map and never driven the road before. If you blindly turn right at 2.3 miles, you're going to end up running into a house or something, because you cannot possibly know the exact mileage to that third right turn until you've driven to it at least once, and having done so, taken notice of the mileage.
So there's the rub. If you want to find a particular point in a file, but you don't know exactly where that point is going to be, you're going to have to skim through the file until you find it. If you're lucky enough to have a situation where the file's future modifications are within your control, you should be able to at least document where that third right turn is found, and keep your "index" up to date if the position ever changes.
This isn't a problem unique to Perl. It's not even a problem unique to computers. Right now, without looking at any table of contents, find me the first page of chapter three in the book To Kill a Mockingbird. You can't find it without physically skimming through the book.
To answer the second part of your question... If you use File::Readbackwards to find that position within the file where the regexp is located, you can use tell to ascertain where, within the file, you actually are. tell gives an absolute location, not related to things like newlines or delimiters. That location can be used later by seek to set your next read/write position within a file.
Dave
|
|---|