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);
| [reply] [Watch: Dir/Any] [d/l] |
"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
| [reply] [Watch: Dir/Any] [d/l] |
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)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Lets not pretend like that has any truth to it, or that anyone actually uses English module
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] [d/l] |
open my $INFILE, '<', 'file.txt'
or die "Couldn't open file.txt: $!";
| [reply] [Watch: Dir/Any] [d/l] |