blib/lib/No/Worries/Dir.pm

filter: Statementstmt 100% 61 / 61 Branchbran 78.1% 25 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 92.6% 114 / 123 CC 19 SCAR 12.1
CC
19
Cov
92%
dir_ensure
5.2
dir_parent
5.0
_mkdir
4.0
CRAP 3.4
« blib/lib/No/Worries/DN.pm ↑ Summary blib/lib/No/Worries/Date.pm »
1 #+##############################################################################
2 # #
3 # File: No/Worries/Dir.pm #
4 # #
5 # Description: directory handling without worries #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package No::Worries::Dir;
14 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 our $VERSION = "1.7";
17 17 our $REVISION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 17 ▶use No::Worries qw($_IntegerRegexp);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 17 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 17 ▶use Params::Validate qw(validate :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #
29 # change the working directory
30 #
31  
32 2 ▶sub dir_change ($) {
Subroutine dir_change: called CC=2 SCAR=7.2
Pod documented
33 2 my($path) = @_;
34  
35 2 ▶ chdir($path) or dief("cannot chdir(%s): %s", $path, $!);
Branch: 1 branch
BranchTrueFalseTotal
chdir $path or &dief("cannot chdir(%s): %s", $path, $!) 2 0 2
36 }
37  
38 #
39 # ensure that a directory exists
40 #
41  
42 # really make a directory, recursively
43  
44 sub _mkdir ($$);
45 3 ▶sub _mkdir ($$) {
Subroutine _mkdir: called CC=4 SCAR=13.9
46 3 my($path, $mode) = @_;
47  
48 3 ▶ if ($path =~ m{^(.+)/[^/]+$} and not -d $1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^(.+)/[^/]+$] and not -d $1) 1 2 3
Condition100%3/3
$path =~ m[^(.+)/[^/]+$] and not -d $1
!ll&&!rl&&r
111
MC/DC100%2/2
$path =~ m[^(.+)/[^/]+$] and not -d $1
$path =~ m[^(.+)/[^/]+$] not -d $1
Truth table100%3/3
A and not(B)
A= $path =~ m[^(.+)/[^/]+$]B= not -d $1
ABresult
0X0
100
111
49 1 _mkdir($1, $mode);
50 }
51 3 ▶ mkdir($path, $mode)
Branch: 1 branch
BranchTrueFalseTotal
mkdir $path, $mode or &dief("cannot mkdir(%s, %04o): %s", $path, $mode, $!) 3 0 3
52 or dief("cannot mkdir(%s, %04o): %s", $path, $mode, $!);
53 }
54  
55 # public interface
56  
57 17 my %dir_ensure_options = (
58 mode => { optional => 1, type => SCALAR, regex => $_IntegerRegexp },
59 );
60  
61 4 ▶sub dir_ensure ($@) {
Subroutine dir_ensure: called CC=5 SCAR=16.5
Pod documented
62 4 my($path, %option);
63  
64 4 $path = shift(@_);
65 4 ▶ %option = validate(@_, \%dir_ensure_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
66 4 ▶ $option{mode} = oct(777) unless defined($option{mode});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{"mode"} 4 0 4
67 4 $path =~ s{/+$}{};
68 4 ▶ _mkdir($path, $option{mode}) unless $path eq "" or -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "" or -d $path 2 2 4
Condition66%2/3
$path eq "" or -d $path
l!l&&r!l&&!r
022
MC/DC50%1/2
$path eq "" or -d $path
$path eq "" -d $path
Truth table66%2/3
A or B
A= $path eq ""B= -d $path
ABresult
1X1
011
000
69 }
70  
71 #
72 # make a directory
73 #
74  
75 17 my %dir_make_options = (
76 mode => { optional => 1, type => SCALAR, regex => $_IntegerRegexp },
77 );
78  
79 3 ▶sub dir_make ($@) {
Subroutine dir_make: called CC=4 SCAR=13.9
Pod documented
80 3 my($path, %option);
81  
82 3 $path = shift(@_);
83 3 ▶ %option = validate(@_, \%dir_make_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
84 3 ▶ $option{mode} = oct(777) unless defined($option{mode});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{"mode"} 2 1 3
85 mkdir($path, $option{mode})
86 3 ▶ or dief("cannot mkdir(%s, %04o): %s", $path, $option{mode}, $!);
Branch: 1 branch
BranchTrueFalseTotal
mkdir $path, $option{"mode"} or &dief("cannot mkdir(%s, %04o): %s", $path, $option{"mode"}, $!) 2 1 3
87 }
88  
89 #
90 # return the parent directory of the given path
91 #
92  
93 13 ▶sub dir_parent ($) {
Subroutine dir_parent: called CC=5 SCAR=16.1
Pod documented
94 13 my($path) = @_;
95  
96 13 ▶ return(".") if $path eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 1 12 13
97 12 $path =~ s{/+$}{};
98 12 ▶ return("/") if $path eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 2 10 12
99 10 $path =~ s{[^/]+$}{};
100 10 ▶ return(".") if $path eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 2 8 10
101 8 $path =~ s{/+$}{};
102 8 ▶ return("/") if $path eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 2 6 8
103 6 return($path);
104 }
105  
106 #
107 # read a directory
108 #
109  
110 2 ▶sub dir_read ($) {
Subroutine dir_read: called CC=3 SCAR=11.2
Pod documented
111 2 my($path) = @_;
112 2 my($dh, @list);
113  
114 2 ▶ opendir($dh, $path) or dief("cannot opendir(%s): %s", $path, $!);
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $path or &dief("cannot opendir(%s): %s", $path, $!) 2 0 2
115 2 @list = grep($_ !~ /^\.\.?$/, readdir($dh));
116 2 ▶ closedir($dh) or dief("cannot closedir(%s): %s", $path, $!);
Branch: 1 branch
BranchTrueFalseTotal
closedir $dh or &dief("cannot closedir(%s): %s", $path, $!) 2 0 2
117 2 return(@list);
118 }
119  
120 #
121 # remove a directory
122 #
123  
124 3 ▶sub dir_remove ($) {
Subroutine dir_remove: called CC=2 SCAR=7.2
Pod documented
125 3 my($path) = @_;
126  
127 3 ▶ rmdir($path) or dief("cannot rmdir(%s): %s", $path, $!);
Branch: 1 branch
BranchTrueFalseTotal
rmdir $path or &dief("cannot rmdir(%s): %s", $path, $!) 3 0 3
128 }
129  
130 #
131 # export control
132 #
133  
134 34 ▶sub import : method {
Subroutine import: called CC=1 SCAR=0.0
135 34 my($pkg, %exported);
136  
137 34 $pkg = shift(@_);
138 34 grep($exported{$_}++,
139 map("dir_$_", qw(change ensure make parent read remove)));
140 34 export_control(scalar(caller()), $pkg, \%exported, @_);
141 }
142  
143 17 1;
144  
145 __DATA__
« blib/lib/No/Worries/DN.pm ↑ Summary blib/lib/No/Worries/Date.pm »