File Coverage

blib/lib/SHARYANTO/File/Util.pm
Criterion Covered Total %
statement 30 32 93.7
branch 11 14 78.5
condition 3 3 100.0
subroutine 7 7 100.0
pod 3 3 100.0
total 54 59 91.5


line stmt bran cond sub pod time code
1             package SHARYANTO::File::Util;
2              
3 1     1   70731 use 5.010001;
  1         3  
  1         39  
4 1     1   7 use strict;
  1         2  
  1         29  
5 1     1   6 use warnings;
  1         2  
  1         33  
6              
7 1     1   6 use Cwd ();
  1         1  
  1         389  
8              
9             require Exporter;
10             our @ISA = qw(Exporter);
11             our @EXPORT_OK = qw(file_exists l_abs_path dir_empty);
12              
13             our $VERSION = '0.57'; # VERSION
14              
15             our %SPEC;
16              
17             sub file_exists {
18 4     4 1 1948 my $path = shift;
19              
20 4 100 100     108 !(-l $path) && (-e _) || (-l _);
21             }
22              
23             sub l_abs_path {
24 5     5 1 3126 my $path = shift;
25 5 100       121 return Cwd::abs_path($path) unless (-l $path);
26              
27 4         8 $path =~ s!/\z!!;
28 4         5 my ($parent, $leaf);
29 4 50       23 if ($path =~ m!(.+)/(.+)!s) {
30 4         96 $parent = Cwd::abs_path($1);
31 4 50       11 return undef unless defined($path);
32 4         9 $leaf = $2;
33             } else {
34 0         0 $parent = Cwd::getcwd();
35 0         0 $leaf = $path;
36             }
37 4         25 "$parent/$leaf";
38             }
39              
40             sub dir_empty {
41 6     6 1 3353 my ($dir) = @_;
42 6 100       93 return undef unless (-d $dir);
43 5 50       123 return undef unless opendir my($dh), $dir;
44 5 100       125 my @d = grep {$_ ne '.' && $_ ne '..'} readdir($dh);
  13         63  
45 5         11 my $res = !@d;
46             #$log->tracef("dir_is_empty(%s)? %d", $dir, $res);
47 5         73 $res;
48             }
49              
50             1;
51             # ABSTRACT: File-related utilities
52              
53             __END__