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

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:


Hi..

This is my code
open (FILE,"file.txt"); while (my $data = <FILE>) { chomp($data); print ("$data\n"); } close(FILE);

I am plan to read every 10 line of the data and sleep 60 second before next looping. How could i do that ?. Could somebody help me ?

Thank you,

baharin

Replies are listed 'Best First'.
Re: how to read every 10 line from input text file ?
by toolic (Bishop) on Nov 24, 2010 at 01:44 UTC
    sleep after every 10th line. See also $.
    open (FILE,"file.txt"); while (my $data = <FILE>) { chomp($data); print ("$data\n"); sleep 60 if $. % 10 == 0; } close(FILE);

      "toolic" code will make it sleep at 10th line but if u need to read every 10th line following is the modified code
      open (FILE,"file.txt"); while (my $data = <FILE>) { if($. % 10 ==0) { chomp($data); print ("$data\n"); sleep 60; } } close(FILE);
      Thanks
      AvantA

      Lets not use the abbreviated perl predefined variables, that confuse newbies, and are partly responsible for perl's reputation as a read only language. Instead can I suggest:

      use English '-no_match_vars'; ... sleep 60 if 0 == ($INPUT_LINE_NUMBER % 10)

      Or better still:

      sleep 60 if 0 == (FILE->input_line_number() %10)
        Lets not pretend like that has any truth to it, or that anyone actually uses English module
Re: how to read every 10 line from input text file ?
by chrestomanci (Priest) on Nov 24, 2010 at 09:18 UTC

    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.

      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

Re: how to read every 10 line from input text file ?
by 7stud (Deacon) on Nov 24, 2010 at 06:56 UTC

    This is the modern way to open a file in perl:

    open my $INFILE, '<', 'file.txt' or die "Couldn't open file.txt: $!";