| 1 | package Dir::Watch; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Dir::Watch - Watches the current directory for file additions or removals. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | Version 0.0.0 | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 2 | our $VERSION = '0.0.0'; | |||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | use Dir::Watch; | ||||||||||
| 23 | |||||||||||
| 24 | $dirwatch=Dir::Watch->new; | ||||||||||
| 25 | |||||||||||
| 26 | if($dirwatch->check){ | ||||||||||
| 27 | print "There are new items\n"; | ||||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | =head1 METHODS | ||||||||||
| 31 | |||||||||||
| 32 | =head2 new | ||||||||||
| 33 | |||||||||||
| 34 | This initiates the object. | ||||||||||
| 35 | |||||||||||
| 36 | $dirwatch=Dir::Watch->new; | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | sub new{ | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 41 | 0 | my %args; | |||||||||
| 42 | # if (defined($_[1])) { | ||||||||||
| 43 | # %args=%{$_[1]}; | ||||||||||
| 44 | # } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | $args{dir}=cwd; | |||||||||
| 47 | |||||||||||
| 48 | 0 | my $self={ dir=>$args{dir} }; | |||||||||
| 49 | 0 | bless $self; | |||||||||
| 50 | |||||||||||
| 51 | #get the stuff in the current directory | ||||||||||
| 52 | 0 | opendir(NEWREAD, $args{dir}); | |||||||||
| 53 | 0 | my @direntries=readdir(NEWREAD); | |||||||||
| 54 | 0 | closedir(NEWREAD); | |||||||||
| 55 | |||||||||||
| 56 | #builds the hash that will be used for checking | ||||||||||
| 57 | 0 | my %dirhash; | |||||||||
| 58 | 0 | my $int=0; | |||||||||
| 59 | 0 | ▶ | while(defined($direntries[$int])){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | $dirhash{$direntries[$int]}=1; | |||||||||
| 61 | |||||||||||
| 62 | 0 | $int++; | |||||||||
| 63 | } | ||||||||||
| 64 | 0 | $self->{dirhash}=\%dirhash; | |||||||||
| 65 | |||||||||||
| 66 | 0 | return $self; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | =head2 check | ||||||||||
| 70 | |||||||||||
| 71 | This checks for a new directories or files. | ||||||||||
| 72 | |||||||||||
| 73 | If any thing has been added or removed, true is returned. | ||||||||||
| 74 | |||||||||||
| 75 | If nothing has been added or removed, false is returned. | ||||||||||
| 76 | |||||||||||
| 77 | if(!$dirwatch->check){ | ||||||||||
| 78 | print "There have been either files/directories added or removed.\n"; | ||||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub check{ | ||||||||
|
Subroutine
check: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 84 | 0 | my $self=$_[0]; | |||||||||
| 85 | |||||||||||
| 86 | #get the stuff in the current directory | ||||||||||
| 87 | 0 | opendir(CHECKREAD, $self->{dir}); | |||||||||
| 88 | 0 | my @direntries=readdir(CHECKREAD); | |||||||||
| 89 | 0 | closedir(CHECKREAD); | |||||||||
| 90 | |||||||||||
| 91 | #builds the hash that will be used for checking | ||||||||||
| 92 | 0 | my %dirhash; | |||||||||
| 93 | 0 | my $int=0; | |||||||||
| 94 | 0 | ▶ | while(defined($direntries[$int])){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | $dirhash{$direntries[$int]}=1; | |||||||||
| 96 | |||||||||||
| 97 | 0 | $int++; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | #check for anything new | ||||||||||
| 101 | 0 | $int=0; | |||||||||
| 102 | 0 | ▶ | while (defined($direntries[$int])) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | ▶ | if (!defined( $self->{dirhash}{ $direntries[$int] } )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $self->{dirhash}=\%dirhash; | |||||||||
| 105 | 0 | return 1; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 0 | $int++; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | #check for any thing removed | ||||||||||
| 112 | 0 | $int=0; | |||||||||
| 113 | 0 | my @keys=keys(%{ $self->{dirhash} }); | |||||||||
| 114 | 0 | ▶ | while (defined( $keys[$int] )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | ▶ | if (!defined( $dirhash{ $keys[$int] } )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | $self->{dirhash}=\%dirhash; | |||||||||
| 117 | 0 | return 1; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 0 | $int++; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | #saves the dir hash for checking later | ||||||||||
| 124 | 0 | $self->{dirhash}=\%dirhash; | |||||||||
| 125 | |||||||||||
| 126 | #return false as if we got here nothing new was found or old was removed | ||||||||||
| 127 | 0 | return 0; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | =head1 AUTHOR | ||||||||||
| 131 | |||||||||||
| 132 | Zane C. Bowers, C<< <vvelox at vvelox.net> >> | ||||||||||
| 133 | |||||||||||
| 134 | =head1 BUGS | ||||||||||
| 135 | |||||||||||
| 136 | Please report any bugs or feature requests to C<bug-dir-watch at rt.cpan.org>, or through | ||||||||||
| 137 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dir-Watch>. I will be notified, and then you'll | ||||||||||
| 138 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | =head1 SUPPORT | ||||||||||
| 144 | |||||||||||
| 145 | You can find documentation for this module with the perldoc command. | ||||||||||
| 146 | |||||||||||
| 147 | perldoc Dir::Watch | ||||||||||
| 148 | |||||||||||
| 149 | |||||||||||
| 150 | You can also look for information at: | ||||||||||
| 151 | |||||||||||
| 152 | =over 4 | ||||||||||
| 153 | |||||||||||
| 154 | =item * RT: CPAN's request tracker | ||||||||||
| 155 | |||||||||||
| 156 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dir-Watch> | ||||||||||
| 157 | |||||||||||
| 158 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 159 | |||||||||||
| 160 | L<http://annocpan.org/dist/Dir-Watch> | ||||||||||
| 161 | |||||||||||
| 162 | =item * CPAN Ratings | ||||||||||
| 163 | |||||||||||
| 164 | L<http://cpanratings.perl.org/d/Dir-Watch> | ||||||||||
| 165 | |||||||||||
| 166 | =item * Search CPAN | ||||||||||
| 167 | |||||||||||
| 168 | L<http://search.cpan.org/dist/Dir-Watch/> | ||||||||||
| 169 | |||||||||||
| 170 | =back | ||||||||||
| 171 | |||||||||||
| 172 | |||||||||||
| 173 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 174 | |||||||||||
| 175 | |||||||||||
| 176 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 177 | |||||||||||
| 178 | Copyright 2009 Zane C. Bowers, all rights reserved. | ||||||||||
| 179 | |||||||||||
| 180 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 181 | under the same terms as Perl itself. | ||||||||||
| 182 | |||||||||||
| 183 | |||||||||||
| 184 | =cut | ||||||||||
| 185 | |||||||||||
| 186 | 2 | 1; # End of Dir::Watch | |||||||||