HomeDir.pm

filter: Statementstmt 57.3% 51 / 89 Branchbran 26.8% 22 / 82 Conditioncond 38.8% 7 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 68.9% 20 / 29 Podpod 100% 17 / 17 totaltotal 47.9% 117 / 244 SCAR 58.5
CC
40
Cov
42%
my_dist_config
72.0
my_dist_data
56.0
_DRIVER
10.0
CRAP 346.9
« HomeDir/Windows.pm ↑ Summary HomeDir/Darwin/Cocoa.pm »
1 package File::HomeDir;
2  
3 # See POD at end for documentation
4  
5 7 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Which ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Globals
14 7 ▶use vars qw{$VERSION @EXPORT @EXPORT_OK $IMPLEMENTED_BY}; ## no critic qw(AutomaticExportation)
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 BEGIN
18 {
19 7 ▶ $VERSION = '1.004';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Inherit manually
22 7 require Exporter;
23 7 @EXPORT = qw{home};
24 7 @EXPORT_OK = qw{
25 home
26 my_home
27 my_desktop
28 my_documents
29 my_music
30 my_pictures
31 my_videos
32 my_data
33 my_dist_config
34 my_dist_data
35 users_home
36 users_desktop
37 users_documents
38 users_music
39 users_pictures
40 users_videos
41 users_data
42 };
43 }
44  
45 # Inlined Params::Util functions
46 sub _CLASS ($) ## no critic qw(SubroutinePrototypes)
47 {
48 7 ▶ (defined $_[0] and not ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $_[0] : undef;
Subroutine _CLASS: called CC=4 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !ref($_[0]) && $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s ? : 7 0 7
Condition33%2/6
defined $_[0] and not ref $_[0] and $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
!ll&&!rl&&r
007
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
007
MC/DC0%0/3
defined $_[0] and not ref $_[0] and $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
defined $_[0] not ref $_[0] $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
Truth table25%1/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
ABCresult
0XX0
10X0
1100
1111
49 }
50  
51 sub _DRIVER ($$) ## no critic qw(SubroutinePrototypes)
52 {
53 7 ▶ (defined _CLASS($_[0]) and eval "require $_[0]; 1" and $_[0]->isa($_[1]) and $_[0] ne $_[1]) ? $_[0] : undef;
Subroutine _DRIVER: called CC=5 SCAR=23.0
Branch: 1 branch
BranchTrueFalseTotal
defined &_CLASS($_[0]) && eval "require $_[0]; 1" && $_[0]->isa($_[1]) && $_[0] ne $_[1] ? : 7 0 7
Condition33%3/9
defined &_CLASS($_[0]) and eval "require $_[0]; 1" and $_[0]->isa($_[1]) and $_[0] ne $_[1]
!ll&&!rl&&r
007
defined &_CLASS($_[0]) and eval "require $_[0]; 1" and $_[0]->isa($_[1])
!ll&&!rl&&r
007
defined &_CLASS($_[0]) and eval "require $_[0]; 1"
!ll&&!rl&&r
007
MC/DC0%0/4
defined &_CLASS($_[0]) and eval "require $_[0]; 1" and $_[0]->isa($_[1]) and $_[0] ne $_[1]
defined &_CLASS($_[0]) eval "require $_[0]; 1" $_[0]->isa($_[1]) $_[0] ne $_[1]
Truth table20%1/5
A and B and C and D
A= defined &_CLASS($_[0])B= eval "require $_[0]; 1"C= $_[0]->isa($_[1])D= $_[0] ne $_[1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
54 }
55  
56 # Platform detection
57 7 ▶if ($IMPLEMENTED_BY)
Branch: 5 branches
BranchTrueFalseTotal
if ($File::HomeDir::IMPLEMENTED_BY) { } 1 6 7
elsif ($^O eq 'MSWin32') { } 0 6 6
elsif ($^O eq 'darwin') { } 0 6 6
elsif ($^O eq 'MacOS') { } 0 6 6
elsif (File::Which::which('xdg-user-dir')) { } 0 6 6
58 {
59 # Allow for custom HomeDir classes
60 # Leave it as the existing value
61 }
62 elsif ($^O eq 'MSWin32')
63 {
64 # All versions of Windows
65 0 $IMPLEMENTED_BY = 'File::HomeDir::Windows';
66 }
67 elsif ($^O eq 'darwin')
68 {
69 # 1st: try Mac::SystemDirectory by chansen
70 0 ▶ if (eval "require Mac::SystemDirectory; 1")
Branch: 2 branches
BranchTrueFalseTotal
if (eval 'require Mac::SystemDirectory; 1') { } 0 0 0
elsif (eval 'require Mac::Files; 1') { } 0 0 0
71 {
72 0 $IMPLEMENTED_BY = 'File::HomeDir::Darwin::Cocoa';
73 }
74 elsif (eval "require Mac::Files; 1")
75 {
76 # 2nd try Mac::Files: Carbon - unmaintained since 2006 except some 64bit fixes
77 0 $IMPLEMENTED_BY = 'File::HomeDir::Darwin::Carbon';
78 }
79 else
80 {
81 # 3rd: fallback: pure perl
82 0 $IMPLEMENTED_BY = 'File::HomeDir::Darwin';
83 }
84 }
85 elsif ($^O eq 'MacOS')
86 {
87 # Legacy Mac OS
88 0 $IMPLEMENTED_BY = 'File::HomeDir::MacOS9';
89 }
90 elsif (File::Which::which('xdg-user-dir'))
91 {
92 # freedesktop unixes
93 0 $IMPLEMENTED_BY = 'File::HomeDir::FreeDesktop';
94 }
95 else
96 {
97 # Default to Unix semantics
98 6 $IMPLEMENTED_BY = 'File::HomeDir::Unix';
99 }
100  
101 7 ▶unless (_DRIVER($IMPLEMENTED_BY, 'File::HomeDir::Driver'))
Branch: 1 branch
BranchTrueFalseTotal
unless (&_DRIVER($File::HomeDir::IMPLEMENTED_BY, "File::HomeDir::Driver")) 0 7 7
102 {
103 0 Carp::croak("Missing or invalid File::HomeDir driver $IMPLEMENTED_BY");
104 }
105  
106 #####################################################################
107 # Current User Methods
108  
109 sub my_home
110 {
111 6 ▶ $IMPLEMENTED_BY->my_home;
Subroutine my_home: called CC=1 SCAR=0.0
Pod documented
112 }
113  
114 sub my_desktop
115 {
116 3 ▶ $IMPLEMENTED_BY->can('my_desktop')
Subroutine my_desktop: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('my_desktop') ? : 3 0 3
117 ? $IMPLEMENTED_BY->my_desktop
118 : Carp::croak("The my_desktop method is not implemented on this platform");
119 }
120  
121 sub my_documents
122 {
123 3 ▶ $IMPLEMENTED_BY->can('my_documents')
Subroutine my_documents: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('my_documents') ? : 3 0 3
124 ? $IMPLEMENTED_BY->my_documents
125 : Carp::croak("The my_documents method is not implemented on this platform");
126 }
127  
128 sub my_music
129 {
130 3 ▶ $IMPLEMENTED_BY->can('my_music')
Subroutine my_music: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('my_music') ? : 3 0 3
131 ? $IMPLEMENTED_BY->my_music
132 : Carp::croak("The my_music method is not implemented on this platform");
133 }
134  
135 sub my_pictures
136 {
137 3 ▶ $IMPLEMENTED_BY->can('my_pictures')
Subroutine my_pictures: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('my_pictures') ? : 3 0 3
138 ? $IMPLEMENTED_BY->my_pictures
139 : Carp::croak("The my_pictures method is not implemented on this platform");
140 }
141  
142 sub my_videos
143 {
144 3 ▶ $IMPLEMENTED_BY->can('my_videos')
Subroutine my_videos: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('my_videos') ? : 3 0 3
145 ? $IMPLEMENTED_BY->my_videos
146 : Carp::croak("The my_videos method is not implemented on this platform");
147 }
148  
149 sub my_data
150 {
151 3 ▶ $IMPLEMENTED_BY->can('my_data')
Subroutine my_data: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('my_data') ? : 3 0 3
152 ? $IMPLEMENTED_BY->my_data
153 : Carp::croak("The my_data method is not implemented on this platform");
154 }
155  
156 sub my_dist_data
157 {
158 0 ▶ my $params = ref $_[-1] eq 'HASH' ? pop : {};
Subroutine my_dist_data: not called CC=7 SCAR=40.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 0 0
159 0 ▶ my $dist = pop or Carp::croak("The my_dist_data method requires an argument");
Branch: 1 branch
BranchTrueFalseTotal
my $dist = pop() or Carp::croak("The my_dist_data method requires an argument") 0 0 0
160 0 my $data = my_data();
161  
162 # If datadir is not defined, there's nothing we can do: bail out
163 # and return nothing...
164 0 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
165  
166 # On traditional unixes, hide the top-level directory
167 0 ▶ my $var =
Branch: 1 branch
BranchTrueFalseTotal
$data eq &home() ? : 0 0 0
168 $data eq home()
169 ? File::Spec->catdir($data, '.perl', 'dist', $dist)
170 : File::Spec->catdir($data, 'Perl', 'dist', $dist);
171  
172 # directory exists: return it
173 0 ▶ return $var if -d $var;
Branch: 1 branch
BranchTrueFalseTotal
if -d $var 0 0 0
174  
175 # directory doesn't exist: check if we need to create it...
176 0 ▶ return undef unless $params->{create};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"create"} 0 0 0
177  
178 # user requested directory creation
179 0 require File::Path;
180 0 File::Path::mkpath($var);
181 0 return $var;
182 }
183  
184 sub my_dist_config
185 {
186 0 ▶ my $params = ref $_[-1] eq 'HASH' ? pop : {};
Subroutine my_dist_config: not called CC=8 SCAR=42.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 0 0
187 0 ▶ my $dist = pop or Carp::croak("The my_dist_config method requires an argument");
Branch: 1 branch
BranchTrueFalseTotal
my $dist = pop() or Carp::croak("The my_dist_config method requires an argument") 0 0 0
188  
189 # not all platforms support a specific my_config() method
190 0 ▶ my $config =
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('my_config') ? : 0 0 0
191 $IMPLEMENTED_BY->can('my_config')
192 ? $IMPLEMENTED_BY->my_config
193 : $IMPLEMENTED_BY->my_documents;
194  
195 # If neither configdir nor my_documents is defined, there's
196 # nothing we can do: bail out and return nothing...
197 0 ▶ return undef unless defined $config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 0 0
198  
199 # On traditional unixes, hide the top-level dir
200 0 ▶ my $etc =
Branch: 1 branch
BranchTrueFalseTotal
$config eq &home() ? : 0 0 0
201 $config eq home()
202 ? File::Spec->catdir($config, '.perl', $dist)
203 : File::Spec->catdir($config, 'Perl', $dist);
204  
205 # directory exists: return it
206 0 ▶ return $etc if -d $etc;
Branch: 1 branch
BranchTrueFalseTotal
if -d $etc 0 0 0
207  
208 # directory doesn't exist: check if we need to create it...
209 0 ▶ return undef unless $params->{create};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"create"} 0 0 0
210  
211 # user requested directory creation
212 0 require File::Path;
213 0 File::Path::mkpath($etc);
214 0 return $etc;
215 }
216  
217 #####################################################################
218 # General User Methods
219  
220 sub users_home
221 {
222 0 ▶ $IMPLEMENTED_BY->can('users_home')
Subroutine users_home: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('users_home') ? : 0 0 0
223 ? $IMPLEMENTED_BY->users_home($_[-1])
224 : Carp::croak("The users_home method is not implemented on this platform");
225 }
226  
227 sub users_desktop
228 {
229 0 ▶ $IMPLEMENTED_BY->can('users_desktop')
Subroutine users_desktop: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('users_desktop') ? : 0 0 0
230 ? $IMPLEMENTED_BY->users_desktop($_[-1])
231 : Carp::croak("The users_desktop method is not implemented on this platform");
232 }
233  
234 sub users_documents
235 {
236 0 ▶ $IMPLEMENTED_BY->can('users_documents')
Subroutine users_documents: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('users_documents') ? : 0 0 0
237 ? $IMPLEMENTED_BY->users_documents($_[-1])
238 : Carp::croak("The users_documents method is not implemented on this platform");
239 }
240  
241 sub users_music
242 {
243 0 ▶ $IMPLEMENTED_BY->can('users_music')
Subroutine users_music: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('users_music') ? : 0 0 0
244 ? $IMPLEMENTED_BY->users_music($_[-1])
245 : Carp::croak("The users_music method is not implemented on this platform");
246 }
247  
248 sub users_pictures
249 {
250 0 ▶ $IMPLEMENTED_BY->can('users_pictures')
Subroutine users_pictures: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('users_pictures') ? : 0 0 0
251 ? $IMPLEMENTED_BY->users_pictures($_[-1])
252 : Carp::croak("The users_pictures method is not implemented on this platform");
253 }
254  
255 sub users_videos
256 {
257 0 ▶ $IMPLEMENTED_BY->can('users_videos')
Subroutine users_videos: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('users_videos') ? : 0 0 0
258 ? $IMPLEMENTED_BY->users_videos($_[-1])
259 : Carp::croak("The users_videos method is not implemented on this platform");
260 }
261  
262 sub users_data
263 {
264 0 ▶ $IMPLEMENTED_BY->can('users_data')
Subroutine users_data: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$File::HomeDir::IMPLEMENTED_BY->can('users_data') ? : 0 0 0
265 ? $IMPLEMENTED_BY->users_data($_[-1])
266 : Carp::croak("The users_data method is not implemented on this platform");
267 }
268  
269 #####################################################################
270 # Legacy Methods
271  
272 # Find the home directory of an arbitrary user
273 sub home (;$) ## no critic qw(SubroutinePrototypes)
274 {
275 # Allow to be called as a method
276 3 ▶ if ($_[0] and $_[0] eq 'File::HomeDir')
Subroutine home: called CC=7 SCAR=21.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and $_[0] eq "File::HomeDir") 0 3 3
Condition66%2/3
$_[0] and $_[0] eq "File::HomeDir"
!ll&&!rl&&r
210
MC/DC0%0/2
$_[0] and $_[0] eq "File::HomeDir"
$_[0] $_[0] eq "File::HomeDir"
Truth table66%2/3
A and B
A= $_[0]B= $_[0] eq "File::HomeDir"
ABresult
0X0
100
111
277 {
278 0 shift();
279 }
280  
281 # No params means my home
282 3 ▶ return my_home() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
283  
284 # Check the param
285 2 my $name = shift;
286 2 ▶ if (!defined $name)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 1 1 2
287 {
288 1 Carp::croak("Can't use undef as a username");
289 }
290 1 ▶ if (!length $name)
Branch: 1 branch
BranchTrueFalseTotal
unless (length $name) 0 1 1
291 {
292 0 Carp::croak("Can't use empty-string (\"\") as a username");
293 }
294  
295 # A dot also means my home
296 ### Is this meant to mean File::Spec->curdir?
297 1 ▶ if ($name eq '.')
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq ".") 0 1 1
298 {
299 0 return my_home();
300 }
301  
302 # Now hand off to the implementor
303 1 $IMPLEMENTED_BY->users_home($name);
304 }
305  
306 7 1;
307  
308 __END__
« HomeDir/Windows.pm ↑ Summary HomeDir/Darwin/Cocoa.pm »