.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "File::Monitor 3pm" .TH File::Monitor 3pm "2022-10-21" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" File::Monitor \- Monitor files and directories for changes. .SH "VERSION" .IX Header "VERSION" This document describes File::Monitor version 1.00 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use File::Monitor; \& \& my $monitor = File::Monitor\->new(); \& \& # Just watch \& $monitor\->watch(\*(Aqsomefile.txt\*(Aq); \& \& # Watch with callback \& $monitor\->watch(\*(Aqotherfile.txt\*(Aq, sub { \& my ($name, $event, $change) = @_; \& # Do stuff \& }); \& \& # Watch a directory \& $monitor\->watch( { \& name => \*(Aqsomedir\*(Aq, \& 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; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides a simple interface for monitoring one or more files or directories and reporting any changes that are made to them. .PP It can .IP "\(bu" 4 monitor existing files for changes to any of the attributes returned by the \f(CW\*(C`stat\*(C'\fR function .IP "\(bu" 4 monitor files that don't yet exist and notify you if they are created .IP "\(bu" 4 notify when a monitored file is deleted .IP "\(bu" 4 notify when files are added or removed from a directory .PP Some possible applications include .IP "\(bu" 4 monitoring the configuration file(s) of a long running process so they can be automatically re-read if they change .IP "\(bu" 4 implementing a 'drop box' directory that receives files to be processed in some way .IP "\(bu" 4 automatically rebuilding a cached object that depends on a number of files if any of those files changes .PP In order to monitor a single file create a new monitor object: .PP .Vb 1 \& my $monitor = File::Monitor\->new(); .Ve .PP Add the file to it: .PP .Vb 1 \& $monitor\->watch( \*(Aqsomefile.txt\*(Aq ); .Ve .PP And then call \f(CW\*(C`scan\*(C'\fR periodically to check for changes: .PP .Vb 1 \& my @changes = $monitor\->scan; .Ve .PP The first call to \f(CW\*(C`scan\*(C'\fR will never report any changes; it captures a snapshot of the state of all monitored files and directories so that subsequent calls to \f(CW\*(C`scan\*(C'\fR can report any changes. .PP Note that \f(CW\*(C`File::Monitor\*(C'\fR doesn't provide asynchronous notifications of file changes; you have to call \f(CW\*(C`scan\*(C'\fR to learn if there have been any changes. .PP To monitor multiple files call \f(CW\*(C`watch\*(C'\fR for each of them: .PP .Vb 3 \& for my $file ( @files ) { \& $monitor\->watch( $file ); \& } .Ve .PP If there have been any changes \f(CW\*(C`scan\*(C'\fR will return a list of File::Monitor::Delta objects. .PP .Vb 4 \& my @changes = $monitor\->scan; \& for my $change (@changes) { \& warn $change\->name, " has changed\en"; \& } .Ve .PP Consult the documentation for File::Monitor::Delta for more information. .PP If you prefer you may register callbacks to be triggered when changes occur. .PP .Vb 5 \& # Gets called for all changes \& $monitor\->callback( sub { \& my ($file_name, $event, $change) = @_; \& warn "$file_name has changed\en"; \& } ); \& \& # Called when file size changes \& $monitor\->callback( size => sub { \& my ($file_name, $event, $change) = @_; \& warn "$file_name has changed size\en"; \& } ); .Ve .PP See File::Monitor::Delta for more information about the various event types for which callbacks may be registered. .PP You may register callbacks for a specific file or directory. .PP .Vb 5 \& # Gets called for all changes to server.conf \& $monitor\->watch( \*(Aqserver.conf\*(Aq, sub { \& my ($file_name, $event, $change) = @_; \& warn "Config file $file_name has changed\en"; \& } ); \& \& # Gets called if the owner of server.conf changes \& $monitor\->watch( { \& name => \*(Aqserver.conf\*(Aq, \& callback => { \& uid => sub { \& my ($file_name, $event, $change) = @_; \& warn "$file_name has changed owner\en"; \& } \& } \& } ); .Ve .PP This last example shows the canonical way of specifying the arguments to \&\f(CW\*(C`watch\*(C'\fR as a hash reference. See \f(CW\*(C`watch\*(C'\fR for more details. .SS "Directories" .IX Subsection "Directories" When 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. .PP See File::Monitor::Object for more information and caveats. .SH "INTERFACE" .IX Header "INTERFACE" .ie n .IP """new( %args )""" 4 .el .IP "\f(CWnew( %args )\fR" 4 .IX Item "new( %args )" Create a new \f(CW\*(C`File::Monitor\*(C'\fR object. Any options should be passed as a reference to a hash as follows: .Sp .Vb 12 \& my $monitor = File::Monitor\->new( { \& base => $some_dir, \& callback => { \& uid => sub { \& my ($file_name, $event, $change) = @_; \& warn "$file_name has changed owner\en"; \& }, \& size => sub { \& my ($file_name, $event, $change) = @_; \& warn "$file_name has changed size\en"; \& } \& } ); .Ve .Sp Both options (\f(CW\*(C`base\*(C'\fR and \f(CW\*(C`callback\*(C'\fR) are optional. .Sp The \f(CW\*(C`base\*(C'\fR option specifies a base directory. When a base directory has been specified all pathnames will internally be stored relative to it. This doesn't affect the public interface which still uses absolute paths but it does makes it possible to relocate a File::Monitor if the directory it's watching is moved. .Sp The \f(CW\*(C`callback\*(C'\fR option must be a reference to a hash that maps event types to handler subroutines. See File::Monitor::Delta for a full list of available event types. .ie n .IP """watch( $name, $callback | { args } )""" 4 .el .IP "\f(CWwatch( $name, $callback | { args } )\fR" 4 .IX Item "watch( $name, $callback | { args } )" Create a new File::Monitor::Object and add it to this monitor. .Sp The passed hash reference contains various options as follows: .Sp .Vb 10 \& $monitor\->watch( { \& name => $file_or_directory_name, \& recurse => $should_recurse_directory, \& files => $should_read_files_in_directory, \& callback => { \& $some_event => sub { \& # Handler for $some_event \& }, \& $other_event => sub { \& # Handler for $other_event \& } \& } \& } ); .Ve .Sp Here are those options in more detail: .RS 4 .ie n .IP """name""" 4 .el .IP "\f(CWname\fR" 4 .IX Item "name" The name of the file or directory to be monitored. Relative paths will be made absolute relative to the current directory at the time of the call. This option is mandatory; \f(CW\*(C`new\*(C'\fR will croak if it is missing. .ie n .IP """recurse""" 4 .el .IP "\f(CWrecurse\fR" 4 .IX Item "recurse" If this is a directory and \f(CW\*(C`recurse\*(C'\fR is true monitor the entire directory tree below this directory. .ie n .IP """files""" 4 .el .IP "\f(CWfiles\fR" 4 .IX Item "files" If this is a directory and \f(CW\*(C`files\*(C'\fR is true monitor the files and directories immediately below this directory but don't recurse down the directory tree. .Sp Note that if you specify \f(CW\*(C`recurse\*(C'\fR or \f(CW\*(C`files\*(C'\fR only the \fInames\fR of contained files will be monitored. Changes to the contents of contained files are not detected. .ie n .IP """callback""" 4 .el .IP "\f(CWcallback\fR" 4 .IX Item "callback" Provides a reference to a hash of callback handlers the keys of which are the names of events as described in File::Monitor::Delta. .RE .RS 4 .Sp Callback subroutines are called with the following arguments: .ie n .IP "$name" 4 .el .IP "\f(CW$name\fR" 4 .IX Item "$name" The name of the file or directory that has changed. .ie n .IP "$event" 4 .el .IP "\f(CW$event\fR" 4 .IX Item "$event" The type of change. If the callback was registered for a specific event it will be passed here. The actual event may be one of the events below the specified event in the event hierarchy. See File::Monitor::Delta for more details. .ie n .IP "$delta" 4 .el .IP "\f(CW$delta\fR" 4 .IX Item "$delta" The File::Monitor::Delta object that describes this change. .RE .RS 4 .Sp As a convenience \f(CW\*(C`watch\*(C'\fR may be called with a simpler form of arguments: .Sp .Vb 1 \& $monitor\->watch( $name ); .Ve .Sp is equivalent to .Sp .Vb 3 \& $monitor\->watch( { \& name => $name \& } ); .Ve .Sp And .Sp .Vb 1 \& $monitor\->watch( $name, $callback ); .Ve .Sp is eqivalent to .Sp .Vb 6 \& $monitor\->watch( { \& name => $name \& callback => { \& change => $callback \& } \& } ); .Ve .RE .ie n .IP """unwatch( $name )""" 4 .el .IP "\f(CWunwatch( $name )\fR" 4 .IX Item "unwatch( $name )" Remove the watcher (if any) that corresponds with the specified file or directory. .Sp .Vb 2 \& my $file = \*(Aqconfig.cfg\*(Aq; \& $monitor\->watch( $file ); # Now we\*(Aqre watching it \& \& $monitor\->unwatch( $file ); # Now we\*(Aqre not .Ve .ie n .IP """scan()""" 4 .el .IP "\f(CWscan()\fR" 4 .IX Item "scan()" Perform a scan of all monitored files and directories and return a list of changes. Any callbacks that are registered will have been triggered before \f(CW\*(C`scan\*(C'\fR returns. .Sp When \f(CW\*(C`scan\*(C'\fR is first called the current state of the various monitored files and directories will be captured but no changes will be reported. .Sp The return value is a list of File::Monitor::Delta objects, one for each changed file or directory. .Sp .Vb 1 \& my @changes = $monitor\->scan; \& \& for my $change ( @changes ) { \& warn $change\->name, " changed\en"; \& } .Ve .ie n .IP """callback( [ $event, ] $coderef )""" 4 .el .IP "\f(CWcallback( [ $event, ] $coderef )\fR" 4 .IX Item "callback( [ $event, ] $coderef )" Register a callback. If \f(CW$event\fR is omitted the callback will be called for all changes. Specify \f(CW$event\fR to limit the callback to certain event types. See File::Monitor::Delta for a full list of events. .Sp .Vb 3 \& $monitor\->callback( sub { \& # called for all changes \& } ); \& \& $monitor\->callback( metadata => sub { \& # called for changes to file/directory metatdata \& } ); .Ve .Sp The callback subroutine will be called with the following arguments: .RS 4 .ie n .IP "$name" 4 .el .IP "\f(CW$name\fR" 4 .IX Item "$name" The name of the file or directory that has changed. .ie n .IP "$event" 4 .el .IP "\f(CW$event\fR" 4 .IX Item "$event" The type of change. If the callback was registered for a specific event it will be passed here. The actual event may be one of the events below the specified event in the event hierarchy. See File::Monitor::Delta for more details. .ie n .IP "$delta" 4 .el .IP "\f(CW$delta\fR" 4 .IX Item "$delta" The File::Monitor::Delta object that describes this change. .RE .RS 4 .RE .ie n .IP """base""" 4 .el .IP "\f(CWbase\fR" 4 .IX Item "base" Get or set the base directory. This allows the entire monitor tree to be relocated. .Sp .Vb 4 \& # Create a monitor and watch a couple of files \& my $monitor = File::Monitor\->new( { base => $some_dir } ); \& $monitor\->watch( "$some_dir/source.c" ); \& $monitor\->watch( "$some_dir/notes.text" ); \& \& # Now move the directory and patch up the monitor \& rename( $some_dir, $other_dir ); \& $monitor\->base( $other_dir ); \& \& # Still works \& my @changes = $monitor\->scan; .Ve .Sp If you are going to specify a base directory you must do so before any watches are added. .ie n .IP """has_monitors""" 4 .el .IP "\f(CWhas_monitors\fR" 4 .IX Item "has_monitors" Returns true if this File::Monitor has any monitors attached to it. Used internally to police the restriction that a base directory may not be set when monitors have been added. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" .ie n .IP """A filename must be specified""" 4 .el .IP "\f(CWA filename must be specified\fR" 4 .IX Item "A filename must be specified" You must pass \f(CW\*(C`unwatch\*(C'\fR the name of a file or directory to stop watching. .SH "CONFIGURATION AND ENVIRONMENT" .IX Header "CONFIGURATION AND ENVIRONMENT" File::Monitor requires no configuration files or environment variables. .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" None. .SH "INCOMPATIBILITIES" .IX Header "INCOMPATIBILITIES" None reported. .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" No bugs have been reported. .PP Please report any bugs or feature requests to \&\f(CW\*(C`bug\-file\-monitor@rt.cpan.org\*(C'\fR, or through the web interface at . .SH "AUTHOR" .IX Header "AUTHOR" Andy Armstrong \f(CW\*(C`\*(C'\fR .PP Faycal Chraibi originally registered the File::Monitor namespace and then kindly handed it to me. .SH "LICENCE AND COPYRIGHT" .IX Header "LICENCE AND COPYRIGHT" Copyright (c) 2007, Andy Armstrong \f(CW\*(C`\*(C'\fR. All rights reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. .SH "DISCLAIMER OF WARRANTY" .IX Header "DISCLAIMER OF WARRANTY" \&\s-1BECAUSE 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 \*(L"AS IS\*(R" 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.\s0 .PP \&\s-1IN 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\s0 (\s-1INCLUDING 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\s0), \s-1EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\s0