Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

detecting file changes

by Anonymous Monk
on Oct 14, 2006 at 15:03 UTC ( [id://578289]=perlquestion: print w/replies, xml ) Need Help??

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

OK...here is my dilema. I have a file that is constantly being appended too from another script using a while loop. I'm trying to ficure out an easy way to detect any changes in the files contents. I've thought about monitoring the file timestamp change but it only gets updated after the script writing to the file terminates. I've also tried reading the contents, sleeping a second, reading it again and I'm able to determine the delta. This work but I was wondering if there is a more elegant solution. Thanks

Replies are listed 'Best First'.
Re: detecting file changes
by Albannach (Monsignor) on Oct 14, 2006 at 19:15 UTC
    Ah, if it's for Windows, then Win32::ChangeNotify should be the answer.

    --
    I'd like to be able to assign to an luser

Re: detecting file changes
by graff (Chancellor) on Oct 14, 2006 at 15:39 UTC
    You don't mention what OS you're using, and I don't actually know if this is an OS-dependent issue, but I do know that on any unix-like system, the file size reported by "-s" will reflect the recent growth of a file while it is actively being written to by some other process:
    my $path = "dir/file_to_watch"; my $prev_size = -s $path; while (1) { sleep 5; my $size_now = -s $path; if ( $size_now > $prev_size ){ print "file grew as of ".scalar localtime().$/; $prev_size = $size_now; } }
      I should have mentioned that this is for Win32
Re: detecting file changes
by blazar (Canon) on Oct 14, 2006 at 15:13 UTC

    I think you may be after the content of perldoc -q tail, which explictly also points to File::Tail, a module you may be interested in.

Re: detecting file changes
by rafl (Friar) on Oct 14, 2006 at 16:16 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://578289]
Approved by prasadbabu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found