| 1 |
package File::Monitor::Lite; |
| 2 |
|
| 3 |
use 5.8.6; |
| 4 |
our $VERSION = '0.652003'; |
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use File::Spec::Functions ':ALL'; |
| 8 |
use File::Find::Rule; |
| 9 |
use File::Monitor; |
| 10 |
use base 'Class::Accessor::Fast'; |
| 11 |
__PACKAGE__->mk_accessors( |
| 12 |
qw( |
| 13 |
monitor |
| 14 |
watch_list |
| 15 |
)); |
| 16 |
|
| 17 |
|
| 18 |
sub new { |
| 19 |
my $class = shift; |
| 20 |
my $self = {@_}; |
| 21 |
bless $self, $class; |
| 22 |
$self->_init; |
| 23 |
return $self; |
| 24 |
} |
| 25 |
|
| 26 |
sub _init{ |
| 27 |
my $self = shift; |
| 28 |
-d $self->{in} or die "invalid director\n"; |
| 29 |
$self->{in}=rel2abs($self->{in}); |
| 30 |
my %w_list = |
| 31 |
map{$_ => 1} |
| 32 |
File::Find::Rule |
| 33 |
->file() |
| 34 |
->name($self->{name}) |
| 35 |
->in($self->{in}); |
| 36 |
$self->watch_list(\%w_list); |
| 37 |
$self->monitor(new File::Monitor); |
| 38 |
$self->monitor->watch($_)for keys %w_list; |
| 39 |
$self->monitor->scan; |
| 40 |
$self->{observed}=[keys %w_list]; |
| 41 |
$self->{created}=[]; |
| 42 |
$self->{deleted}=[]; |
| 43 |
$self->{modified}=[]; |
| 44 |
1; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
sub check { |
| 50 |
my $self = shift; |
| 51 |
my $w_list=$self->watch_list; |
| 52 |
my @new_file_list = |
| 53 |
File::Find::Rule |
| 54 |
->file() |
| 55 |
->name($self->{name}) |
| 56 |
->in($self->{in}); |
| 57 |
my @new_files = grep { not exists $$w_list{$_} } @new_file_list; |
| 58 |
my @changes = $self->monitor->scan; |
| 59 |
my @deleted_files = |
| 60 |
map{$_->name} |
| 61 |
grep{$_->deleted} @changes; |
| 62 |
my @modified_files = |
| 63 |
map{$_->name} |
| 64 |
grep{not $_->deleted} @changes; |
| 65 |
|
| 66 |
# update waching list |
| 67 |
foreach(@new_files){ |
| 68 |
$$w_list{$_}=1; |
| 69 |
$self->monitor->watch($_); |
| 70 |
} |
| 71 |
# unwatch deleted files |
| 72 |
foreach(@deleted_files){ |
| 73 |
$self->monitor->unwatch($_); |
| 74 |
delete $$w_list{$_}; |
| 75 |
} |
| 76 |
$self->monitor->scan; |
| 77 |
# do a scan first before next check |
| 78 |
# else next check $self->monitor cannot find differences |
| 79 |
|
| 80 |
|
| 81 |
$self->{watch_list}=($w_list); |
| 82 |
$self->{created}= [@new_files]; |
| 83 |
$self->{modified}=[@modified_files]; |
| 84 |
$self->{observed}=[ keys %$w_list]; |
| 85 |
$self->{deleted}= [@deleted_files]; |
| 86 |
$self->{anychange}= ( |
| 87 |
scalar(@new_files)+ |
| 88 |
scalar(@modified_files)+ |
| 89 |
scalar(@deleted_files)) ? 1 : 0; |
| 90 |
|
| 91 |
1; |
| 92 |
} |
| 93 |
sub created { |
| 94 |
my $self = shift; |
| 95 |
#return () unless @{$self->{created}}; |
| 96 |
return sort @{$self->{created}}; |
| 97 |
} |
| 98 |
sub modified { |
| 99 |
my $self = shift; |
| 100 |
return sort @{$self->{modified}}; |
| 101 |
} |
| 102 |
sub observed { |
| 103 |
my $self = shift; |
| 104 |
return sort @{$self->{observed}}; |
| 105 |
} |
| 106 |
sub deleted { |
| 107 |
my $self = shift; |
| 108 |
return sort @{$self->{deleted}}; |
| 109 |
} |
| 110 |
sub anychange{ |
| 111 |
my $self = shift; |
| 112 |
return $self->{anychange}; |
| 113 |
} |
| 114 |
|
| 115 |
# Preloaded methods go here. |
| 116 |
|
| 117 |
1; |
| 118 |
__END__ |
| 119 |
# Below is stub documentation for your module. You'd better edit it! |
| 120 |
|
| 121 |
=head1 NAME |
| 122 |
|
| 123 |
File::Monitor::Lite - Monitor file changes |
| 124 |
|
| 125 |
=head1 SYNOPSIS |
| 126 |
|
| 127 |
use File::Monitor::Lite; |
| 128 |
|
| 129 |
my $monitor = File::Monitor::Lite->new ( |
| 130 |
in => '.', |
| 131 |
name => '*.html', |
| 132 |
); |
| 133 |
|
| 134 |
while ($monitor->check() and sleep 1){ |
| 135 |
my @deleted_files = $monitor->deleted; |
| 136 |
my @modified_files = $monitor->modified; |
| 137 |
my @created_files = $monitor->created; |
| 138 |
my @observing_files = $monitor->observed; |
| 139 |
`do blah..` if $monitor->anychange; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
=head1 DESCRIPTION |
| 144 |
|
| •145 |
This is another implementation of File::Monitor. While File::Monitor cannot detect file creation (unless you declare file name first), I use File::Find::Rule to rescan files every time when $monitor->check() is executed. To use this module, just follow synopsis above. |
| 146 |
|
| 147 |
Currently one cannot change file observing rules. To do so, create another monitor object with new rules. |
| 148 |
|
| 149 |
$m1=File::Monitor::Lite->new( |
| 150 |
name => '*.html', |
| 151 |
in => '.', |
| 152 |
); |
| 153 |
$m1->check(); |
| 154 |
#blah... |
| 155 |
$m2=File::Monitor::Lite->new( |
| 156 |
name => '*.css', |
| 157 |
in => '.', |
| 158 |
); |
| 159 |
$m2->check(); |
| 160 |
#blah... |
| 161 |
|
| 162 |
=head1 INTERFACE |
| 163 |
|
| 164 |
=over |
| 165 |
|
| 166 |
=item C< new ( args ) > |
| 167 |
|
| 168 |
Create a new C<File::Monitor::Lite> object. |
| 169 |
|
| 170 |
my $monitor = File::Monitor::Lite->new( |
| 171 |
in => '.', |
| 172 |
name => '*.mp3', |
| 173 |
); |
| 174 |
|
| 175 |
The syntax is inherited from L<File::Find::Rule>. It will applied on L<File::Find::Rule> as |
| 176 |
|
| 177 |
File::Find::Rule |
| 178 |
->file() |
| 179 |
->name('*.mp3') |
| 180 |
->in('.'); |
| 181 |
|
| 182 |
As described in L<File::Find::Rule>, name can be globs or regular expressions. |
| 183 |
|
| 184 |
name => '*.pm', # a simple glob |
| 185 |
name => qr/.+\.pm$/, # regex |
| 186 |
name => ['*.mp3', qr/.+\.ogg$/], # array of rules |
| 187 |
name => @rules, |
| 188 |
|
| 189 |
=item C< check() > |
| 190 |
|
| 191 |
Check if any file recognized by File::Find::Rule has changed (created, modified, deleted.) The usage is simple: |
| 192 |
|
| 193 |
$monitor->check(); |
| 194 |
|
| 195 |
=item C< created > |
| 196 |
|
| 197 |
Returns an array of file names which has been created since last check. |
| 198 |
|
| 199 |
=item C< modified > |
| 200 |
|
| 201 |
Returns an array of file names which has been modified since last check. |
| 202 |
|
| 203 |
=item C< deleted > |
| 204 |
|
| 205 |
Returns an array of file names which has been deleted since last check. |
| 206 |
|
| 207 |
=item C< observed > |
| 208 |
|
| 209 |
Returns an array of file names which monitor is observing at. |
| 210 |
|
| 211 |
=back |
| 212 |
|
| 213 |
=head1 SEE ALSO |
| 214 |
|
| 215 |
L<File::Find::Rule>, L<File::Monitor> |
| 216 |
|
| 217 |
=head1 AUTHOR |
| 218 |
|
| 219 |
dryman, E<lt>[email protected]<gt> |
| 220 |
|
| 221 |
=head1 COPYRIGHT AND LICENSE |
| 222 |
|
| 223 |
Copyright (C) 2011 by dryman |
| 224 |
|
| 225 |
This library is free software; you can redistribute it and/or modify |
| 226 |
it under the same terms as Perl itself, either Perl version 5.10.0 or, |
| 227 |
at your option, any later version of Perl 5 you may have available. |
| 228 |
|
| 229 |
|
| 230 |
=cut |