Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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
[choroba]: LanX I miss working in a bank sometimes...
[Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
[Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
[Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)
[LanX]: Choroba: do you miss chaos with ties? apply at the US government.. ;)
[ambrus]: Corion: those are good rules.
[ambrus]: Discipulus: oh sure. the input data has different filenames every time I get them.
[ambrus]: the directory structure may be 1, 2, or 3 deep, it may have spaces in the filename or not, it has dates in various format, different keywords for the same meanings, and the dates and other keywords are assembled in various ways.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2017-03-29 12:12 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.