1 package File::ChangeNotify::Watcher;
8 use File::ChangeNotify::Event;
9 use List::MoreUtils qw(all);
11 use Moose::Util::TypeConstraints;
12 use MooseX::Params::Validate qw( pos_validated_list );
17 default => sub {qr/.*/},
20 my $dir = subtype as 'Str' => where { -d $_ } =>
21 message {"$_ is not a valid directory"};
23 my $array_of_dirs = subtype
24 as 'ArrayRef[Str]', => where {
26 } => message {"@{$_} is not a list of valid directories"};
28 coerce $array_of_dirs => from $dir => via { [$_] };
32 writer => '_set_directories',
33 isa => $array_of_dirs,
38 has follow_symlinks => (
47 default => 'File::ChangeNotify::Event',
50 has sleep_interval => (
56 my $files_or_regexps = subtype as 'ArrayRef[Str|RegexpRef]';
60 isa => $files_or_regexps,
61 default => sub { [] },
68 Class::MOP::load_class( $self->event_class() );
74 return $self->_interesting_events();
81 return if grep { $_ eq $dir } $self->directories();
83 push @{ $self->directories() }, $dir;
86 sub _path_is_excluded {
90 foreach my $excluded ( @{ $self->exclude } ) {
92 if ( ref $excluded && ref $excluded eq 'Regexp' ) {
93 return 1 if $path =~ /$excluded/;
96 return 1 if $path eq $excluded;
103 sub _remove_directory {
107 $self->_set_directories(
108 [ grep { $_ ne $dir } @{ $self->directories() } ] );
112 no Moose::Util::TypeConstraints;
113 no MooseX::Params::Validate;
115 __PACKAGE__->meta()->make_immutable();
123 File::ChangeNotify::Watcher - Base class for all watchers
128 File::ChangeNotify->instantiate_watcher
129 ( directories => [ '/my/path', '/my/other' ],
130 filter => qr/\.(?:pm|conf|yml)$/,
131 exclude => ['t', 'root', qr(/(?!\.)[^/]+$)],
134 if ( my @events = $watcher->new_events() ) { ... }
137 while ( my @events = $watcher->wait_for_events() ) { ... }
141 A C<File::ChangeNotify::Watcher> class monitors a directory for
142 changes made to any file. You can provide a regular expression to
143 filter out files you are not interested in. It handles the addition of
144 new subdirectories by adding them to the watch list.
146 Note that the actual granularity of what each watcher subclass reports
147 may vary across subclasses. Implementations that hook into some sort
148 of kernel event interface (Inotify, for example) have much better
149 knowledge of exactly what changes are happening than one implemented
150 purely in userspace code (like the Default subclass).
152 By default, events are returned in the form
153 L<File::ChangeNotify::Event> objects, but this can be overridden by
154 providing an "event_class" attribute to the constructor.
156 The watcher can operate in a blocking/callback style, or you can
157 simply ask it for a list of new events as needed.
161 =head2 File::ChangeNotify::Watcher::Subclass->new(...)
163 This method creates a new watcher. It accepts the following arguments:
167 =item * directories => $path
169 =item * directories => \@paths
171 This argument is required. It can be either one or many paths which
172 should be watched for changes.
174 =item * filter => qr/.../
176 This is an optional regular expression that will be used to check if a
177 file is of interest. This filter is only applied to files.
179 By default, all files are included.
181 =item * exclude => [...]
183 An optional list of paths to exclude. This list can contain either plain
184 strings or regular expressions. If you provide a string it should contain the
185 complete path to be excluded.
187 The paths can be either directories or specific files. If the exclusion
188 matches a directory, all of its files and subdirectories are ignored.
190 =item * follow_symlinks => $bool
192 By default, symlinks are ignored. Set this to true to follow them.
194 If this symlinks are being followed, symlinks to files and directories
195 will be followed. Directories will be watched, and changes for
196 directories and files reported.
198 =item * sleep_interval => $number
200 For watchers which call C<sleep> to implement the C<<
201 $watcher->wait_for_events() >> method, this argument controls how long
202 it sleeps for. The value is a number in seconds.
204 The default is 2 seconds.
206 =item * event_class => $class
208 This can be used to change the class used to report events. By
209 default, this is L<File::ChangeNotify::Event>.
213 =head2 $watcher->wait_for_events()
215 This method causes the watcher to block until it sees interesting
216 events, and then return them as a list.
218 Some watcher subclasses may implement blocking as a sleep loop, while
219 others may actually block.
221 =head2 $watcher->new_events()
223 This method returns a list of any interesting events seen since the
224 last time the watcher checked.
226 =head2 $watcher->sees_all_events()
228 If this is true, the watcher will report on all events.
230 Some watchers, like the Default subclass, are not smart enough to
231 track things like a file being created and then immediately deleted,
232 and can only detect changes between snapshots of the file system.
234 Other watchers, like the Inotify subclass, see all events that happen
239 Dave Rolsky, E<lt>autarch@urth.orgE<gt>
241 =head1 COPYRIGHT & LICENSE
243 Copyright 2009 Dave Rolsky, All Rights Reserved.
245 This program is free software; you can redistribute it and/or modify
246 it under the same terms as Perl itself.