Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

monitor text files

by grashoper (Monk)
on Apr 16, 2009 at 14:37 UTC ( #758002=perlquestion: print w/replies, xml ) Need Help??

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

updated title. to more closely reflect my goal. update.. I have my file mover built, now I want to monitor the srcdir, to make sure the other program is not hung so i installed file monitor but its giving me this error, I am not sure i am even referring to the objects correctly here and apparently I am not as I get this message.. global symbol object requires explicit package name in line 36 global symbol object requires explicit package name in line 39 I guess I am just dense but I don't understand the watch with callback, I am guessing that means if it changes to call some other file?
use File::Copy::Recursive; use File::Find; use strict; my $srcdir='c:\\inetpub\\performancetesting\\output\\new\\'; my $destdir='c:\\'; File::Copy::Recursive::dircopy $srcdir, $destdir or die "Copy failed: + $!"; use File::Monitor; my $monitor = File::Monitor->new(); # Just watch $monitor->watch('c:\\inetpub\\performancetesting\\output\\new\\mlx +\\aar\\04-16-2009.txt'); # Watch with callback $monitor->watch('otherfile.txt', sub { my ($name, $event, $change) = @_; print "file has been changed"; # Do stuff; }); # Watch a directory $monitor->watch( { name => 'c:\\inetpub\\performancetesting\\output\\new\\ +mlx\\', recurse => 1, callback => { files_created => sub { my ($name, $event, $change) = @_; print "something modified in directory mlx"; } } } ); # First scan just finds out about the monitored files. No changes # will be reported. $object->scan; my $monitor->scan; # Later perform a scan and gather any changes my @changes = $object->scan;
I have a need to monitor text files as they are being updated by another application, ideally I would like to access the files shortly after the other program has written to them to check values against a threshold, as well as validate them to ensure they are in a proper format for input into a db, these checks can occur after the other program is finished with them if need be, but I would like to be able to send alerts in real time should the values for these transactions exceed the limits, in addition I would like the limits themselves to be user configurable and I would like to then copy the files into another location so my sql transaction can pick them up.

Replies are listed 'Best First'.
Re: can perl do this?
by Corion (Patriarch) on Apr 16, 2009 at 14:42 UTC
      I cannot seem to get file::tail to work. I think I am closer with file watcher, wish some more detail was provided in the readmes for a lot of these modules.
Re: can perl do this?
by lostjimmy (Chaplain) on Apr 16, 2009 at 14:50 UTC
Re: monitor text files
by ig (Vicar) on Apr 16, 2009 at 20:13 UTC

    The various examples in the synopsis of File::Monitor are not all consistent with each other and you have copied some of the inconsistencies into your program. In particular, you create a $monitor object but then try to call the scan method in $object, which doesn't exist.

    Here is a working example of a monitor with callback.

    use strict; use warnings; use File::Monitor; my $monitor = File::Monitor->new(); $monitor->watch( '/tmp/otherfile.txt', sub { my ( $name, $event, $change ) = @_; print "file has been changed\n"; } ); while(1) { foreach my $delta ($monitor->scan) { print $delta->name . " has changed\n"; } sleep(10); }

    But this depends on polling for changes, which is not ideal.

    Can you have your application which creates the data initiate the further processing? This would minimize delay and eliminate the waste of polling.

      actually I do want to poll for changes primarily wish to scan periodically to ensure that mtime is still changing, I can have the scan run once the files are copied, but then it will only happen every few hours, it should not cause too much extra processor or disk usage right? all that is being written to the file currently is a single line of data, but there are 50+ files and the data consists of time tests to perform operations on an application which is dependent in turn on ie and active x so I can't easily code my own testing app for it. Eventually I will probably do something with .net and fiddler to create a more custom solution but for now I just need to make sure its running when it should, I will need yet another script to validate the data files but I plan on doing that after they have been copied over to the destination directories. Thank you for the example.
Re: can perl do this?
by BJ_Covert_Action (Beadle) on Apr 16, 2009 at 16:21 UTC
    In the interest of jest and quipiness, to any questions involving, "Can Perl ... ?" The answer is, of course, yes (with some creativity and a lot pattern matching of course). On that note, I have a a new micro-controller for my toaster that I need to go configure with Perl (one day....)


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-05-18 06:11 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (68 votes). Check out past polls.