|
NAMEFile::Monitor - Monitor files and directories for changes.VERSIONThis document describes File::Monitor version 1.00SYNOPSISuse File::Monitor; my $monitor = File::Monitor->new(); # Just watch $monitor->watch('somefile.txt'); # Watch with callback $monitor->watch('otherfile.txt', sub { my ($name, $event, $change) = @_; # Do stuff }); # Watch a directory $monitor->watch( { name => 'somedir', recurse => 1, callback => { files_created => sub { my ($name, $event, $change) = @_; # Do stuff } } } ); # First scan just finds out about the monitored files. No changes # will be reported. $object->scan; # Later perform a scan and gather any changes my @changes = $object->scan; DESCRIPTIONThis module provides a simple interface for monitoring one or more files or directories and reporting any changes that are made to them.It can
Some possible applications include
In order to monitor a single file create a new monitor object: my $monitor = File::Monitor->new(); Add the file to it: $monitor->watch( 'somefile.txt' ); And then call "scan" periodically to check for changes: my @changes = $monitor->scan; The first call to "scan" will never report any changes; it captures a snapshot of the state of all monitored files and directories so that subsequent calls to "scan" can report any changes. Note that "File::Monitor" doesn't provide asynchronous notifications of file changes; you have to call "scan" to learn if there have been any changes. To monitor multiple files call "watch" for each of them: for my $file ( @files ) { $monitor->watch( $file ); } If there have been any changes "scan" will return a list of File::Monitor::Delta objects. my @changes = $monitor->scan; for my $change (@changes) { warn $change->name, " has changed\n"; } Consult the documentation for File::Monitor::Delta for more information. If you prefer you may register callbacks to be triggered when changes occur. # Gets called for all changes $monitor->callback( sub { my ($file_name, $event, $change) = @_; warn "$file_name has changed\n"; } ); # Called when file size changes $monitor->callback( size => sub { my ($file_name, $event, $change) = @_; warn "$file_name has changed size\n"; } ); See File::Monitor::Delta for more information about the various event types for which callbacks may be registered. You may register callbacks for a specific file or directory. # Gets called for all changes to server.conf $monitor->watch( 'server.conf', sub { my ($file_name, $event, $change) = @_; warn "Config file $file_name has changed\n"; } ); # Gets called if the owner of server.conf changes $monitor->watch( { name => 'server.conf', callback => { uid => sub { my ($file_name, $event, $change) = @_; warn "$file_name has changed owner\n"; } } } ); This last example shows the canonical way of specifying the arguments to "watch" as a hash reference. See "watch" for more details. DirectoriesWhen monitoring a directory you can choose to ignore its contents, scan its contents one level deep or perform a recursive scan of all its subdirectories.See File::Monitor::Object for more information and caveats. INTERFACE
Callback subroutines are called with the following arguments:
As a convenience "watch" may be called with a simpler form of arguments: $monitor->watch( $name ); is equivalent to $monitor->watch( { name => $name } ); And $monitor->watch( $name, $callback ); is eqivalent to $monitor->watch( { name => $name callback => { change => $callback } } );
DIAGNOSTICS
CONFIGURATION AND ENVIRONMENTFile::Monitor requires no configuration files or environment variables.DEPENDENCIESNone.INCOMPATIBILITIESNone reported.BUGS AND LIMITATIONSNo bugs have been reported.Please report any bugs or feature requests to "bug-file-monitor@rt.cpan.org", or through the web interface at <http://rt.cpan.org>. AUTHORAndy Armstrong "<andy@hexten.net>"Faycal Chraibi originally registered the File::Monitor namespace and then kindly handed it to me. LICENCE AND COPYRIGHTCopyright (c) 2007, Andy Armstrong "<andy@hexten.net>". All rights reserved.This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. DISCLAIMER OF WARRANTYBECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Visit the GSP FreeBSD Man Page Interface. |