File Coverage

blib/lib/Dir/Watch.pm
Criterion Covered Total %
statement 9 48 18.7
branch 0 4 0.0
condition n/a
subroutine 3 5 60.0
pod 2 2 100.0
total 14 59 23.7


line stmt bran cond sub pod time code
1             package Dir::Watch;
2              
3 1     1   23395 use warnings;
  1         2  
  1         30  
4 1     1   5 use strict;
  1         2  
  1         34  
5 1     1   5 use Cwd;
  1         5  
  1         439  
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             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             sub new{
41 0     0 1   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])){
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             sub check{
84 0     0 1   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])){
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])) {
103 0 0         if (!defined( $self->{dirhash}{ $direntries[$int] } )) {
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} });
  0            
114 0           while (defined( $keys[$int] )) {
115 0 0         if (!defined( $dirhash{ $keys[$int] } )) {
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<< >>
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests to C, or through
137             the web interface at L. 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
157              
158             =item * AnnoCPAN: Annotated CPAN documentation
159              
160             L
161              
162             =item * CPAN Ratings
163              
164             L
165              
166             =item * Search CPAN
167              
168             L
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             1; # End of Dir::Watch