http://qs1969.pair.com?node_id=873392


in reply to how to read every 10 line from input text file ?

From your question, it looks like you trying to monitor a file that is being continuously appended to by another process, and you want to read the recent changes.

If this is the case, then the simple method of sleeping and then reading to the end of the file may not work, as you find yourself Suffering from Buffering. In short, if you read to the end of the file, and then the other process add more, then your perl process may not see any more unless you re-open the file, as perl will have buffered the contents of the file already.

Instead, can I suggest that you look into using something like File::Tail, which will let you read to the end of the file, and then block until more data is added. Each time the other process adds more lines to the file, your process will unblock and read it, and so is able to process the new data immediately and reliably.

Replies are listed 'Best First'.
Re^2: how to read every 10 line from input text file ?
by johngg (Canon) on Nov 24, 2010 at 10:32 UTC
    In short, if you read to the end of the file, and then the other process add more, then your perl process may not see any more unless you re-open the file, as perl will have buffered the contents of the file already.

    It's not a case of the Perl process already having buffered the contents, it's that the eof error has been set. This can be cleared using seek and further reads can then be performed, as demonstrated in the documentation.

    Cheers,

    JohnGG