CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem while trailing a growing file

 



Cupidvogel
Novice

Mar 24, 2012, 6:36 AM

Post #1 of 2 (715 views)
Problem while trailing a growing file Can't Post

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; }
}


Line 3 maintains the number of lines newly appended. If 0, then current value of $rem is increased by 1 signifying that 4 more of that, and the code should exit, while line 5 flushes $rem 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


the code prints out


Code
a 
b
c


instantly, but then the cursor keeps on blinking without printing out d. 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 d, and program terminates. Where am I erring, and how can I rectify it?


rovf
Veteran

Mar 29, 2012, 5:11 AM

Post #2 of 2 (688 views)
Re: [Cupidvogel] Problem while trailing a growing file [In reply to] Can't Post

See File::Tail on CPAN.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives