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

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: 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 (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

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://578289]
Approved by prasadbabu
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2018-06-20 17:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.