Find/Any.pm

filter: Statementstmt 41.4% 41 / 99 Branchbran 12.5% 8 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 52.1% 12 / 23 Podpod 100% 13 / 13 totaltotal 37.1% 74 / 199 CC 34 SCAR 28.3
CC
34
Cov
30%
_install
56.0
_temp_dir
30.0
create_parent_dir
30.0
CRAP 17.0
« Find/WinAny.pm ↑ Summary Find/Win2k.pm »
1 package Config::Find::Any;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # private methods
12  
13 6 ▶sub _find {
Subroutine _find: called CC=4 SCAR=14.3
14 6 my ($class, $write, $global, @names)=@_;
15
16 6 for my $n (@names) {
17 8 my $fn;
18 8 ▶ if ($n=~/^(.*?)\/(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ m[^(.*?)/(.*)$]) { } 0 8 8
19 0 my ($dir, $file)=($1, $2);
20 0 $fn=$class->look_for_dir_file($dir, $file, $write, $global);
21 } else {
22 8 $fn=$class->look_for_file($n, $write, $global);
23 }
24 8 ▶ return $fn if defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fn 5 3 8
25 }
26  
27 1 return undef;
28 }
29  
30 0 ▶sub _open {
Subroutine _open: not called CC=3 SCAR=24.8
31 0 my ($class, $write, $global, $fn)=@_;
32
33 0 ▶ if ($write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write) 0 0 0
34 0 $class->create_parent_dir($fn);
35 0 return IO::File->new($fn, 'w');
36 }
37  
38 0 ▶ defined($fn) ? IO::File->new($fn, 'r') : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $fn ? : 0 0 0
39 }
40  
41 0 ▶sub _install {
Subroutine _install: not called CC=7 SCAR=40.3
42 0 my ($class, $orig, $write, $global, $fn)=@_;
43 0 ▶ croak "install mode has to be 'write'" unless $write;
Branch: 1 branch
BranchTrueFalseTotal
unless $write 0 0 0
44  
45 0 ▶ my $oh=IO::File->new($orig, 'r')
Branch: 1 branch
BranchTrueFalseTotal
my $oh = "IO::File"->new($orig, "r") or croak("unable to open '${orig}'") 0 0 0
46 or croak "unable to open '$orig'";
47 0 ▶ my $fh=$class->_open($write, $global, $fn)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $class->_open($write, $global, $fn) or croak("unable to create config file '${fn}'") 0 0 0
48 or croak "unable to create config file '$fn'";
49
50 0 ▶ while(<$oh>) { $fh->print($_) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $oh)) 0 0 0
51
52 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "unable to write config file '${fn}'" 0 0 0
53 or die "unable to write config file '$fn'";
54 0 ▶ close $oh
Branch: 1 branch
BranchTrueFalseTotal
close $oh or die "unable to read '${orig}'" 0 0 0
55 or die "unable to read '$orig'";
56 0 return $fn;
57 }
58  
59 0 ▶sub _temp_dir {
Subroutine _temp_dir: not called CC=5 SCAR=34.0
60 0 my ($class, $name, $more_name, $scope)=@_;
61  
62 0 my $stemp=$class->system_temp;
63  
64 0 ▶ if ($scope eq 'global') {
Branch: 4 branches
BranchTrueFalseTotal
if ($scope eq 'global') { } 0 0 0
elsif ($scope eq 'user') { } 0 0 0
elsif ($scope eq 'app') { } 0 0 0
elsif ($scope eq 'process') { } 0 0 0
65 0 $class->my_catdir($stemp, $name, $more_name)
66 } elsif ($scope eq 'user') {
67 0 $class->my_catdir($stemp, $class->my_getlogin, $name, $more_name)
68 } elsif ($scope eq 'app') {
69 0 $class->my_catdir($class->app_dir($name), 'tmp', $more_name)
70 } elsif ($scope eq 'process') {
71 0 $class->my_catdir($stemp, $class->my_getlogin, $name, $$, $more_name)
72 } else {
73 0 croak "scope '$scope' is not valid for temp_dir method";
74 }
75 }
76  
77 # public methods, to be overridden
78  
79 0 ▶sub look_for_file {
Subroutine look_for_file: not called CC=1 SCAR=6.9
Pod documented
80 0 my $class=shift;
81 0 die "unimplemented virtual method $class->look_for_file() called";
82 }
83  
84 0 ▶sub look_for_dir_file {
Subroutine look_for_dir_file: not called CC=1 SCAR=6.9
Pod documented
85 0 my $class=shift;
86 0 die "unimplemented virtual method $class->look_for_dir_file() called";
87 }
88  
89 # public methods, inherited by sub-classes
90  
91 14 ▶sub guess_full_script_name {
Subroutine guess_full_script_name: called CC=4 SCAR=19.7
Pod documented
92 14 my $path = (File::Spec->splitpath($0))[1];
93 14 ▶ if ($path eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 14 14
94 0 ▶ if (my $script = File::Which::which($0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $script = File::Which::which($0)) 0 0 0
95 0 return File::Spec->rel2abs($script);
96 }
97 }
98  
99 14 ▶ return File::Spec->rel2abs($0) if -e $0;
Branch: 1 branch
BranchTrueFalseTotal
if -e $0 14 0 14
100  
101 0 carp "unable to determine script '$0' location";
102 }
103  
104 0 ▶sub guess_script_name {
Subroutine guess_script_name: not called CC=3 SCAR=24.8
Pod documented
105 0 my $name;
106 0 (undef, undef, $name)=File::Spec->splitpath($0);
107 0 ▶ $name=~/^(.+)\..*$/ and return $1;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^(.+)\..*$/ and return $1 0 0 0
108 0 ▶ return undef if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 0 0
109 0 return $name;
110 }
111  
112 14 ▶sub guess_script_dir {
Subroutine guess_script_dir: called CC=1 SCAR=0.0
Pod documented
113 14 my $class=shift;
114 14 my $script=$class->guess_full_script_name;
115 14 my ($unit, $dir)=File::Spec->splitpath($script, 0);
116 14 File::Spec->catpath($unit, $dir, '');
117 }
118  
119 7 ▶sub is_one_liner { return $0 eq '-e' }
Subroutine is_one_liner: called CC=1 SCAR=0.0
Pod documented
120  
121 7 ▶sub add_extension {
Subroutine add_extension: called CC=2 SCAR=7.1
Pod documented
122 7 my ($class, $name, $ext)=@_;
123 7 ▶ return $name if ($name=~/\./);
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\./ 0 7 7
124 7 return $name.'.'.$ext;
125 }
126  
127 0 ▶sub create_parent_dir {
Subroutine create_parent_dir: not called CC=5 SCAR=34.0
Pod documented
128 0 my ($class, $fn)=@_;
129 0 my $parent=$class->parent_dir($fn);
130 0 ▶ if (-e $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $parent) { } 0 0 0
131 0 ▶ -d $parent
Branch: 1 branch
BranchTrueFalseTotal
-d $parent or croak("'${parent}' exists but is not a directory") 0 0 0
132 or croak "'$parent' exists but is not a directory";
133 0 ▶ -W $parent
Branch: 1 branch
BranchTrueFalseTotal
-W $parent or croak("not allowed to write on directory '${parent}'") 0 0 0
134 or croak "not allowed to write on directory '$parent'";
135 } else {
136 0 $class->create_parent_dir($parent);
137 0 ▶ mkdir $parent
Branch: 1 branch
BranchTrueFalseTotal
mkdir $parent or die "unable to create directory '${parent}' ($!)" 0 0 0
138 or die "unable to create directory '$parent' ($!)";
139 }
140 }
141  
142 14 ▶sub parent_dir {
Subroutine parent_dir: called CC=3 SCAR=11.4
Pod documented
143 14 my ($class, $dir)=@_;
144 # print "creating dir $dir\n";
145 14 my @dirs=File::Spec->splitdir($dir);
146 14 ▶ pop(@dirs) eq '' and pop(@dirs);
Branch: 1 branch
BranchTrueFalseTotal
pop @dirs eq "" and pop @dirs 14 0 14
147 14 ▶ File::Spec->catfile(@dirs ? @dirs : File::Spec->rootdir);
Branch: 1 branch
BranchTrueFalseTotal
@dirs ? : 14 0 14
148 }
149  
150 0 ▶sub create_dir {
Subroutine create_dir: not called CC=4 SCAR=30.0
Pod documented
151 0 my ($class, $dir)=@_;
152 0 ▶ if (-e $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir) { } 0 0 0
153 0 ▶ -d $dir or croak "'$dir' exists but is not a directory";
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or croak("'${dir}' exists but is not a directory") 0 0 0
154 } else {
155 0 $class->create_parent_dir($dir);
156 0 ▶ mkdir $dir
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die "unable to create directory '${dir}' ($!)" 0 0 0
157 or die "unable to create directory '$dir' ($!)";
158 }
159 0 $dir;
160 }
161  
162 0 ▶sub my_catfile {
Subroutine my_catfile: not called CC=2 SCAR=17.9
Pod documented
163 0 my $class=shift;
164 0 ▶ pop @_ unless defined $_[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[-1] 0 0 0
165 0 File::Spec->catfile(@_);
166 }
167  
168 0 ▶sub my_catdir {
Subroutine my_catdir: not called CC=2 SCAR=17.9
Pod documented
169 0 my $class=shift;
170 0 ▶ pop @_ unless defined $_[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[-1] 0 0 0
171 0 File::Spec->catdir(@_);
172 }
173  
174 0 ▶sub my_getlogin {
Subroutine my_getlogin: not called CC=2 SCAR=17.9
Pod documented
175 0 my $login=getlogin();
176 0 ▶ $login = '_UNKNOW_' unless defined $login;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $login 0 0 0
177 0 $login;
178 }
179  
180 6 1;
181  
182 __END__
« Find/WinAny.pm ↑ Summary Find/Win2k.pm »