File Coverage

lib/Perlmazing/Perlmazing/rmdir.pm
Criterion Covered Total %
statement 26 26 100.0
branch 10 16 62.5
condition 3 3 100.0
subroutine 4 4 100.0
pod 0 1 0.0
total 43 50 86.0


line stmt bran cond sub pod time code
1 1     1   9 use Perlmazing qw(croak);
  1         2  
  1         7  
2 1     1   7 use File::Spec;
  1         2  
  1         456  
3              
4             sub main {
5 2     2 0 2217 my $path = shift;
6 2 50       36 croak "$path doesn't exist or can't be read" unless -e $path;
7 2 50       22 croak "$path is not a valid directory" unless -d $path;
8 2         7 my $item_count = _clean_level($path);
9 2 50       87 $item_count += CORE::rmdir($path) or die "Can't remove $path: $!";
10 2         163 return $item_count;
11             }
12              
13             sub _clean_level {
14 4     4   8 my $path = shift;
15 4         7 my $item_count = 0;
16 4 50       126 opendir my $dir, $path or die "Can't open directory $path for reading: $!";
17 4         81 while (my $item = readdir $dir) {
18 12 100       74 next if $item =~ /^\.{1,2}$/;
19 4         42 my $full_item = File::Spec->catdir($path, $item);
20 4 50       14 next if $full_item eq $path;
21 4 100 100     91 if (-d $full_item and not readlink $full_item) {
22 2         10 _clean_level($full_item);
23 2         114 $item_count += CORE::rmdir($full_item);
24             } else {
25 2 50       107 unlink $full_item or die "Cannot remove $full_item: $!";
26 2         18 $item_count++;
27             }
28             }
29 4         50 closedir $dir;
30 4         18 return $item_count;
31             }
32              
33             1;