If you know that you are looking for a pattern that will not stretch over more than 2 (or, any n) consecutive records, then you can simply build an array of the first n records in the file (to “prime the pump” ...), then proceed as follows:
- Concatenate all n records in the current list, into a single string. Then, search within that string.
- unshift the first record from the head of the array, and push the next record onto the tail of it.
- Rinse and repeat, until an undef indicates that you have reached the end of the file.
No matter how enormous the file being processed may be, the memory requirements are negligible.