Hi, I have a text file which has some lines, I may append others periodically, so I want a code that trails it (say at an interval of 5 seconds). When it sees that for 5 consecutive intervals no new line has been appended, the code decides to exit. Here's what I have come up with:

[code]
open f, "trail.txt";
while (1) {
while (<f>) {$count++; print; }
$rem++ if $count == 0;
$rem = 0 if $count > 0;
if ($rem < 5) {
sleep 10;
seek(f, 0, 2);
$count = 0;
}
else { last; }
}
[/code]

Line 3 maintains the number of lines newly appended. If 0, then current value of [b]$rem[/b] is increased by 1 signifying that 4 more of that, and the code should exit, while line 5 flushes [b]$rem[/b] if some line is appended. After printing out the newly appended lines, the code waits for 10 seconds, and repeats its action. The problem is that if the text file contains something like this initially:

[code]
a
b
c
d[/code]

the code prints out

[code]
a
b
c
[/code]

instantly, but then the cursor keeps on blinking without printing out [b]d[/b]. Even if I append some line, the cursor keeps on blinking all the same. If I don't do anything, at the end of 50 seconds it prints out [b]d[/b], and program terminates. Where am I erring, and how can I rectify it?

Here's what i found regarding this topic. Maybe it can help you out.

use strict;
use warnings "all";

open my $IN, "<in.txt" or die "cannot open file.\n";
my $timer = 0;
my $_loc = -s $IN;
seek($IN, $_loc, 0);
while(1) 
{   
    if($timer >= 25)
    {
        die "No new lines added lately, exiting...\n";
    }
    my $loc = -s $IN;
    while($loc != $_loc) 
    {
        my $new = <$IN>;
        if(defined $new)
        {
            $timer = 0;
        }   
        $_loc = tell($IN);
    }
    $timer += 5;
    sleep(5);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.