Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

detecting file changes

by Anonymous Monk
on Oct 14, 2006 at 15:03 UTC ( #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

Comment on detecting file changes
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 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 rafl (Friar) on Oct 14, 2006 at 16:16 UTC
Re: detecting file changes
by Albannach (Prior) 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

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-09-18 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls