blib/lib/File/ConfigDir.pm

filter: Statementstmt 92.4% 196 / 212 Branchbran 78.5% 64 / 84 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 45 / 45 Podpod 100% 13 / 13 totaltotal 89.5% 324 / 364 CC 46 SCAR 11.1
CC
46
Cov
88%
__ANON__
7.1
config_dirs
7.0
__ANON__
5.6
CRAP 3.0
↑ Summary
1 package File::ConfigDir;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use FindBin ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 File::ConfigDir - Get directories of configuration files
19  
20 =begin html
21  
22 <a href="https://travis-ci.org/perl5-utils/File-ConfigDir"><img src="https://travis-ci.org/perl5-utils/File-ConfigDir.svg?branch=master" alt="Travis CI"/></a>
23 <a href='https://coveralls.io/github/perl5-utils/File-ConfigDir?branch=master'><img src='https://coveralls.io/repos/github/perl5-utils/File-ConfigDir/badge.svg?branch=master' alt='Coverage Status'/></a>
24  
25 =end html
26  
27 =cut
28  
29 5 $VERSION = '0.019';
30 5 @EXPORT_OK = (
31 qw(config_dirs system_cfg_dir desktop_cfg_dir),
32 qw(xdg_config_dirs machine_cfg_dir),
33 qw(core_cfg_dir site_cfg_dir vendor_cfg_dir),
34 qw(locallib_cfg_dir local_cfg_dir),
35 qw(here_cfg_dir singleapp_cfg_dir vendorapp_cfg_dir),
36 qw(xdg_config_home user_cfg_dir)
37 );
38 5 %EXPORT_TAGS = (
39 ALL => [@EXPORT_OK],
40 );
41  
42 5 ▶eval "use List::MoreUtils qw/distinct/;"; ## no strict (BuiltinFunctions::ProhibitStringyEval)
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶__PACKAGE__->can("distinct") or eval <<'EOP';
Branch: 1 branch
BranchTrueFalseTotal
__PACKAGE__->can("distinct") or eval " # from PP part of List::MoreUtils\nsub distinct(&\@) {\n my %h;\n map { \$h{\$_}++ == 0 ? \$_ : () } \@_;\n}\n" 0 5 5
44 # from PP part of List::MoreUtils
45 sub distinct(&@) {
46 my %h;
47 map { $h{$_}++ == 0 ? $_ : () } @_;
48 }
49 EOP
50  
51 =head1 SYNOPSIS
52  
53 use File::ConfigDir ':ALL';
54  
55 my @cfgdirs = config_dirs();
56 my @appcfgdirs = config_dirs('app');
57  
58 # install support
59 my $site_cfg_dir = (site_cfg_dir())[0];
60 my $vendor_cfg_dir = (site_cfg_dir()))[0];
61  
62 =head1 DESCRIPTION
63  
64 This module is a helper for installing, reading and finding configuration
65 file locations. It's intended to work in every supported Perl5 environment
66 and will always try to Do The Right Thing(tm).
67  
68 C<File::ConfigDir> is a module to help out when perl modules (especially
69 applications) need to read and store configuration files from more than
70 one location. Writing user configuration is easy thanks to
71 L<File::HomeDir>, but what when the system administrator needs to place
72 some global configuration or there will be system related configuration
73 (in C</etc> on UNIX(tm) or C<$ENV{windir}> on Windows(tm)) and some
74 network configuration in nfs mapped C</etc/p5-app> or
75 C<$ENV{ALLUSERSPROFILE} . "\\Application Data\\p5-app">, respectively.
76  
77 C<File::ConfigDir> has no "do what I mean" mode - it's entirely up to the
78 user to pick the right directory for each particular application.
79  
80 =head1 EXPORT
81  
82 Every function listed below can be exported, either by name or using the
83 tag C<:ALL>.
84  
85 =head1 SUBROUTINES/METHODS
86  
87 All functions can take one optional argument as application specific
88 configuration directory. If given, it will be embedded at the right (tm)
89 place of the resulting path.
90  
91 =cut
92  
93 sub _find_common_base_dir
94 {
95 7 ▶ my ($dira, $dirb) = @_;
Subroutine _find_common_base_dir: called CC=4 SCAR=13.9
96 7 my ($va, $da, undef) = File::Spec->splitpath($dira);
97 7 my ($vb, $db, undef) = File::Spec->splitpath($dirb);
98 7 my @dirsa = File::Spec->splitdir($da);
99 7 my @dirsb = File::Spec->splitdir($db);
100 7 my @commondir;
101 7 ▶ my $max = $#dirsa < $#dirsb ? $#dirsa : $#dirsb;
Branch: 1 branch
BranchTrueFalseTotal
$#dirsa < $#dirsb ? : 1 6 7
102 7 for my $i (0 .. $max)
103 {
104 21 ▶ $dirsa[$i] eq $dirsb[$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$dirsa[$i] eq $dirsb[$i] or last 2 19 21
105 19 push(@commondir, $dirsa[$i]);
106 }
107  
108 7 File::Spec->catdir($va, @commondir);
109 }
110  
111 =head2 system_cfg_dir
112  
113 Returns the configuration directory where configuration files of the
114 operating system resides. For Unices this is C</etc>, for MSWin32 it's
115 the value of the environment variable C<%windir%>.
116  
117 =cut
118  
119 my $system_cfg_dir = sub {
120 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
121 4 ▶ my @dirs = File::Spec->catdir($^O eq "MSWin32" ? $ENV{windir} : "/etc", @cfg_base);
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 4 4
122 4 @dirs;
123 5 };
124  
125 sub system_cfg_dir
126 {
127 2 ▶ my @cfg_base = @_;
Subroutine system_cfg_dir: called CC=2 SCAR=6.9
Pod documented
128 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('system_cfg_dir(;$), not system_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
129 and croak "system_cfg_dir(;\$), not system_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
130 1 $system_cfg_dir->(@cfg_base);
131 }
132  
133 =head2 machine_cfg_dir
134  
135 Alias for desktop_cfg_dir - deprecated.
136  
137 =head2 xdg_config_dirs
138  
139 Alias for desktop_cfg_dir
140  
141 =head2 desktop_cfg_dir
142  
143 Returns the configuration directory where configuration files of the
144 desktop applications resides. For Unices this is C</etc/xdg>, for MSWin32
145 it's the value of the environment variable C<%ALLUSERSPROFILE%>
146 concatenated with the basename of the environment variable C<%APPDATA%>.
147  
148 =cut
149  
150 my $desktop_cfg_dir = sub {
151 5 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=3 SCAR=14.2
152 5 my @dirs;
153 5 ▶ if ($^O eq "MSWin32")
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 5 5
154 {
155 0 my $alluserprof = $ENV{ALLUSERSPROFILE};
156 0 my $appdatabase = File::Basename::basename($ENV{APPDATA});
157 0 @dirs = (File::Spec->catdir($alluserprof, $appdatabase, @cfg_base));
158 }
159 else
160 {
161 5 ▶ if ($ENV{XDG_CONFIG_DIRS})
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'XDG_CONFIG_DIRS'}) { } 0 5 5
162 {
163 0 @dirs = split(":", $ENV{XDG_CONFIG_DIRS});
164 0 @dirs = map { File::Spec->catdir($_, @cfg_base) } @dirs;
165 }
166 else
167 {
168 5 @dirs = (File::Spec->catdir("/etc", "xdg", @cfg_base));
169 }
170 }
171 5 @dirs;
172 5 };
173  
174 sub desktop_cfg_dir
175 {
176 3 ▶ my @cfg_base = @_;
Subroutine desktop_cfg_dir: called CC=2 SCAR=6.9
Pod documented
177 3 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('desktop_cfg_dir(;$), not desktop_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 2 3
178 and croak "desktop_cfg_dir(;\$), not desktop_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
179 2 $desktop_cfg_dir->(@cfg_base);
180 }
181  
182 5 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
183 5 *machine_cfg_dir = \&desktop_cfg_dir;
184 5 *xdg_config_dirs = \&desktop_cfg_dir;
185 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 =head2 core_cfg_dir
188  
189 Returns the C<etc> directory below C<$Config{prefix}>.
190  
191 =cut
192  
193 my $core_cfg_dir = sub {
194 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
195 4 my @dirs = (File::Spec->catdir($Config{prefix}, "etc", @cfg_base));
196 4 @dirs;
197 5 };
198  
199 sub core_cfg_dir
200 {
201 2 ▶ my @cfg_base = @_;
Subroutine core_cfg_dir: called CC=2 SCAR=6.9
Pod documented
202 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('core_cfg_dir(;$), not core_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
203 and croak "core_cfg_dir(;\$), not core_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
204 1 $core_cfg_dir->(@cfg_base);
205 }
206  
207 =head2 site_cfg_dir
208  
209 Returns the C<etc> directory below C<$Config{sitelib_stem}> or the common
210 base directory of C<$Config{sitelib}> and C<$Config{sitebin}>.
211  
212 =cut
213  
214 my $site_cfg_dir = sub {
215 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=2 SCAR=7.6
216 4 my @dirs;
217  
218 4 ▶ if ($Config{sitelib_stem})
Branch: 1 branch
BranchTrueFalseTotal
if ($File::ConfigDir::Config{'sitelib_stem'}) { } 4 0 4
219 {
220 4 push(@dirs, File::Spec->catdir($Config{sitelib_stem}, "etc", @cfg_base));
221 }
222 else
223 {
224 0 my $sitelib_stem = _find_common_base_dir($Config{sitelib}, $Config{sitebin});
225 0 push(@dirs, File::Spec->catdir($sitelib_stem, "etc", @cfg_base));
226 }
227  
228 4 @dirs;
229 5 };
230  
231 sub site_cfg_dir
232 {
233 2 ▶ my @cfg_base = @_;
Subroutine site_cfg_dir: called CC=2 SCAR=6.9
Pod documented
234 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('site_cfg_dir(;$), not site_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
235 and croak "site_cfg_dir(;\$), not site_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
236 1 $site_cfg_dir->(@cfg_base);
237 }
238  
239 =head2 vendor_cfg_dir
240  
241 Returns the C<etc> directory below C<$Config{vendorlib_stem}> or the common
242 base directory of C<$Config{vendorlib}> and C<$Config{vendorbin}>.
243  
244 =cut
245  
246 my $vendor_cfg_dir = sub {
247 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
248 4 my @dirs;
249  
250 4 ▶ if ($Config{vendorlib_stem})
Branch: 1 branch
BranchTrueFalseTotal
if ($File::ConfigDir::Config{'vendorlib_stem'}) { } 0 4 4
251 {
252 0 push(@dirs, File::Spec->catdir($Config{vendorlib_stem}, "etc", @cfg_base));
253 }
254 else
255 {
256 4 my $vendorlib_stem = _find_common_base_dir($Config{vendorlib}, $Config{vendorbin});
257 4 push(@dirs, File::Spec->catdir($vendorlib_stem, "etc", @cfg_base));
258 }
259  
260 4 @dirs;
261 5 };
262  
263 sub vendor_cfg_dir
264 {
265 2 ▶ my @cfg_base = @_;
Subroutine vendor_cfg_dir: called CC=2 SCAR=6.9
Pod documented
266 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('vendor_cfg_dir(;$), not vendor_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
267 and croak "vendor_cfg_dir(;\$), not vendor_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
268 1 $vendor_cfg_dir->(@cfg_base);
269 }
270  
271 =head2 singleapp_cfg_dir
272  
273 Returns the configuration file for standalone installed applications. In
274 Unix speak, installing JRE to C<< /usr/local/jre-<version> >> means there is
275 a C<< /usr/local/jre-<version>/bin/java >> and going from it's directory
276 name one above and into C<etc> there is the I<singleapp_cfg_dir>. For a
277 Perl module it means, we're assuming that C<$FindBin::Bin> is installed as
278 a standalone package somewhere, eg. into C</usr/pkg> - as recommended for
279 L<pkgsrc|http://www.pkgsrc.org/>.
280  
281 =cut
282  
283 my $singleapp_cfg_dir = sub {
284 my @dirs = (
285 map {
286 4 ▶ eval { Cwd::abs_path($_) }
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
eval { do { Cwd::abs_path($_) } } or "File::Spec"->canonpath($_) 0 4 4
287 or File::Spec->canonpath($_)
288 } File::Spec->catdir($FindBin::RealDir, "..", "etc")
289 );
290 4 @dirs;
291 5 };
292  
293 sub singleapp_cfg_dir
294 {
295 2 ▶ my @cfg_base = @_;
Subroutine singleapp_cfg_dir: called CC=2 SCAR=6.9
Pod documented
296 2 ▶ 0 == scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
0 == scalar @cfg_base or croak('singleapp_cfg_dir(), not singleapp_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
297 or croak "singleapp_cfg_dir(), not singleapp_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
298 1 $singleapp_cfg_dir->();
299 }
300  
301 =head2 vendorapp_cfg_dir
302  
303 Returns the configuration file for vendot installed applications. In Unix
304 speak, installing bacula to C<< /opt/${vendor} >> means there is
305 a C<< /opt/${vendor}/bin/bacula >> and going from it's directory
306 name one above and into C<etc> there is the I<vendorapp_cfg_dir>. For a
307 Perl module it means, we're assuming that C<$FindBin::Bin> is installed as
308 a standalone package somewhere, eg. into C</usr/pkg> - as recommended for
309 L<pkgsrc|http://www.pkgsrc.org/>.
310  
311 =cut
312  
313 my $vendorapp_cfg_dir = sub {
314 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
315 my @dirs = (
316 map {
317 4 ▶ eval { Cwd::abs_path($_) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { Cwd::abs_path($_) } } or "File::Spec"->canonpath($_) 1 3 4
318 or File::Spec->canonpath($_)
319 } File::Spec->catdir($FindBin::RealDir, "..", "etc", @cfg_base)
320 );
321 4 @dirs;
322 5 };
323  
324 sub vendorapp_cfg_dir
325 {
326 2 ▶ my @cfg_base = @_;
Subroutine vendorapp_cfg_dir: called CC=2 SCAR=6.9
Pod documented
327 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('vendorapp_cfg_dir(;$), not vendorapp_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
328 and croak "vendorapp_cfg_dir(;\$), not vendorapp_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
329 1 $vendorapp_cfg_dir->(@cfg_base);
330 }
331  
332 =head2 local_cfg_dir
333  
334 Returns the configuration directory for distribution independent, 3rd
335 party applications. While this directory doesn't exists for MSWin32,
336 there will be only the path C</usr/local/etc> for Unices.
337  
338 =cut
339  
340 my $local_cfg_dir = sub {
341 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
342 4 my @dirs;
343  
344 4 ▶ unless ($^O eq "MSWin32")
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "MSWin32") 4 0 4
345 {
346 4 push(@dirs, File::Spec->catdir("/usr", "local", "etc", @cfg_base));
347 }
348  
349 4 @dirs;
350 5 };
351  
352 sub local_cfg_dir
353 {
354 2 ▶ my @cfg_base = @_;
Subroutine local_cfg_dir: called CC=2 SCAR=6.9
Pod documented
355 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('local_cfg_dir(;$), not local_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
356 and croak "local_cfg_dir(;\$), not local_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
357 1 $local_cfg_dir->(@cfg_base);
358 }
359  
360 =head2 locallib_cfg_dir
361  
362 Extracts the C<INSTALL_BASE> from C<$ENV{PERL_MM_OPT}> and returns the
363 C<etc> directory below it.
364  
365 =cut
366  
367 5 my $haveLocalLib;
368  
369 BEGIN
370 {
371 # uncoverable branch false
372 5 ▶ defined $haveLocalLib or $haveLocalLib = eval "use local::lib (); local::lib->can('active_paths');";
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine BEGIN: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
defined $haveLocalLib or $haveLocalLib = eval "use local::lib (); local::lib->can('active_paths');" 5 0 5
373 5 ▶ defined $haveLocalLib or $haveLocalLib = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $haveLocalLib or $haveLocalLib = 0 5 0 5
374 }
375  
376 my $locallib_cfg_dir = sub {
377 1 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=2 SCAR=7.4
378 1 my @dirs;
379  
380 1 ▶ $haveLocalLib and push(@dirs, map { File::Spec->catdir($_, "etc", @cfg_base) } local::lib->active_paths);
Branch: 1 branch
BranchTrueFalseTotal
$haveLocalLib and push @dirs, map({"File::Spec"->catdir($_, "etc", @cfg_base);} "local::lib"->active_paths) 0 1 1
381  
382 1 @dirs;
383 5 };
384  
385 sub locallib_cfg_dir
386 {
387 2 ▶ my @cfg_base = @_;
Subroutine locallib_cfg_dir: called CC=2 SCAR=6.9
Pod documented
388 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('locallib_cfg_dir(;$), not locallib_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
389 and croak "locallib_cfg_dir(;\$), not locallib_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
390 1 $locallib_cfg_dir->(@cfg_base);
391 }
392  
393 =head2 here_cfg_dir
394  
395 Returns the path for the C<etc> directory below the current working directory.
396  
397 =cut
398  
399 my $here_cfg_dir = sub {
400 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
401 4 my @dirs = (File::Spec->catdir(File::Spec->rel2abs(File::Spec->curdir()), @cfg_base, "etc"));
402 4 @dirs;
403 5 };
404  
405 sub here_cfg_dir
406 {
407 2 ▶ my @cfg_base = @_;
Subroutine here_cfg_dir: called CC=2 SCAR=6.9
Pod documented
408 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('here_cfg_dir(;$), not here_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
409 and croak "here_cfg_dir(;\$), not here_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
410 1 $here_cfg_dir->(@cfg_base);
411 }
412  
413 5 my $haveFileHomeDir;
414  
415 BEGIN
416 {
417 # uncoverable branch false
418 5 ▶ defined $haveFileHomeDir or $haveFileHomeDir = eval "use File::HomeDir; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $haveFileHomeDir or $haveFileHomeDir = eval "use File::HomeDir; 1" 5 0 5
419 5 ▶ defined $haveFileHomeDir or $haveFileHomeDir = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $haveFileHomeDir or $haveFileHomeDir = 0 0 5 5
420 }
421  
422 =head2 user_cfg_dir
423  
424 Returns the users home folder using L<File::HomeDir>. Without
425 File::HomeDir, nothing is returned.
426  
427 =cut
428  
429 my $user_cfg_dir = sub {
430 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=6 SCAR=19.6
431 4 my @dirs;
432  
433 my $homedir;
434 4 ▶ $haveFileHomeDir and $homedir = File::HomeDir->my_home();
Branch: 1 branch
BranchTrueFalseTotal
$haveFileHomeDir and $homedir = "File::HomeDir"->my_home 4 0 4
435 4 ▶ $homedir |= $ENV{HOME} if defined $ENV{HOME};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"HOME"} 4 0 4
436 4 ▶ $homedir |= File::Spec->catpath($ENV{HOMEDRIVE}, $ENV{HOMEPATH}) if defined $ENV{HOMEDRIVE} and defined $ENV{HOMEPATH};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"HOMEDRIVE"} and defined $ENV{"HOMEPATH"} 0 4 4
Condition33%1/3
defined $ENV{"HOMEDRIVE"} and defined $ENV{"HOMEPATH"}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $ENV{"HOMEDRIVE"} and defined $ENV{"HOMEPATH"}
defined $ENV{"HOMEDRIVE"} defined $ENV{"HOMEPATH"}
Truth table33%1/3
A and B
A= defined $ENV{"HOMEDRIVE"}B= defined $ENV{"HOMEPATH"}
ABresult
0X0
100
111
437  
438 4 ▶ $homedir and @dirs = (File::Spec->catdir($homedir, map { "." . $_ } @cfg_base));
Branch: 1 branch
BranchTrueFalseTotal
$homedir and @dirs = "File::Spec"->catdir($homedir, map({'.' . $_;} @cfg_base)) 4 0 4
439  
440 4 @dirs;
441 5 };
442  
443 sub user_cfg_dir
444 {
445 2 ▶ my @cfg_base = @_;
Subroutine user_cfg_dir: called CC=2 SCAR=6.9
Pod documented
446 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('user_cfg_dir(;$), not user_cfg_dir(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
447 and croak "user_cfg_dir(;\$), not user_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
448 1 $user_cfg_dir->(@cfg_base);
449 }
450  
451 =head2 xdg_config_home
452  
453 Returns the user configuration directory for desktop applications.
454 If C<< $ENV{XDG_CONFIG_HOME} >> is not set, for MSWin32 the value
455 of C<< $ENV{APPDATA} >> is return and on Unices the C<.config> directory
456 in the users home folder. Without L<File::HomeDir>, on Unices the returned
457 list might be empty.
458  
459 =cut
460  
461 my $xdg_config_home = sub {
462 4 ▶ my @cfg_base = @_;
Subroutine __ANON__: called CC=4 SCAR=17.3
463 4 my @dirs;
464  
465 4 ▶ if ($ENV{XDG_CONFIG_HOME})
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'XDG_CONFIG_HOME'}) { } 0 4 4
elsif ($^O eq 'MSWin32') { } 0 4 4
466 {
467 0 @dirs = split(":", $ENV{XDG_CONFIG_HOME});
468 0 @dirs = map { File::Spec->catdir($_, @cfg_base) } @dirs;
469 }
470 elsif ($^O eq "MSWin32")
471 {
472 0 @dirs = (File::Spec->catdir($ENV{APPDATA}, @cfg_base));
473 }
474 else
475 {
476 4 ▶ $haveFileHomeDir and @dirs = (File::Spec->catdir(File::HomeDir->my_home(), ".config", @cfg_base));
Branch: 1 branch
BranchTrueFalseTotal
$haveFileHomeDir and @dirs = "File::Spec"->catdir("File::HomeDir"->my_home, ".config", @cfg_base) 4 0 4
477 }
478  
479 4 @dirs;
480 5 };
481  
482 sub xdg_config_home
483 {
484 2 ▶ my @cfg_base = @_;
Subroutine xdg_config_home: called CC=2 SCAR=6.9
Pod documented
485 2 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('xdg_config_home(;$), not xdg_config_home(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 1 2
486 and croak "xdg_config_home(;\$), not xdg_config_home(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
487 1 $xdg_config_home->(@cfg_base);
488 }
489  
490 5 my (@extensible_bases, @pure_bases);
491 5 push(@extensible_bases,
492 $system_cfg_dir, $desktop_cfg_dir, $local_cfg_dir, $singleapp_cfg_dir, $vendorapp_cfg_dir, $core_cfg_dir,
493 $site_cfg_dir, $vendor_cfg_dir, $here_cfg_dir, $user_cfg_dir, $xdg_config_home);
494 5 push(@pure_bases, 3);
495  
496 =head2 config_dirs
497  
498 @cfgdirs = config_dirs();
499 @cfgdirs = config_dirs( 'appname' );
500  
501 Tries to get all available configuration directories as described above.
502 Returns those who exists and are readable.
503  
504 =cut
505  
506 sub config_dirs
507 {
508 4 ▶ my @cfg_base = @_;
Subroutine config_dirs: called CC=7 SCAR=19.5
Pod documented
509 4 ▶ 1 < scalar(@cfg_base)
Branch: 1 branch
BranchTrueFalseTotal
1 < scalar @cfg_base and croak('config_dirs(;$), not config_dirs(' . join(",", ("\$") x scalar(@cfg_base)) . ')') 1 3 4
510 and croak "config_dirs(;\$), not config_dirs(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
511 3 my @dirs = ();
512  
513 3 my $pure_idx = 0;
514 3 foreach my $idx (0 .. $#extensible_bases)
515 {
516 37 my $pure;
517 37 ▶ $pure_idx <= $#pure_bases and $idx == $pure_bases[$pure_idx] and $pure = ++$pure_idx;
Branch: 1 branch
BranchTrueFalseTotal
$pure_idx <= $#pure_bases and $idx == $pure_bases[$pure_idx] and $pure = ++$pure_idx 5 32 37
Condition100%3/3
$pure_idx <= $#pure_bases and $idx == $pure_bases[$pure_idx]
!ll&&!rl&&r
7255
MC/DC100%2/2
$pure_idx <= $#pure_bases and $idx == $pure_bases[$pure_idx]
$pure_idx <= $#pure_bases $idx == $pure_bases[$pure_idx]
Truth table100%3/3
A and B
A= $pure_idx <= $#pure_basesB= $idx == $pure_bases[$pure_idx]
ABresult
0X0
100
111
518 37 ▶ push(@dirs, $extensible_bases[$idx]->(($pure ? () : @cfg_base)));
Branch: 1 branch
BranchTrueFalseTotal
$pure ? : 5 32 37
519 }
520  
521 3 ▶ @dirs = grep { -d $_ && -r $_ } distinct(@dirs);
Branch: 1 branch
BranchTrueFalseTotal
-d $_ and -r $_ 10 17 27
522  
523 3 @dirs;
524 }
525  
526 =head2 _plug_dir_source
527  
528 my $dir_src = sub { return _better_config_dir(@_); }
529 File::ConfigDir::_plug_dir_source($dir_src);
530  
531 my $pure_src = sub { return _better_config_plain_dir(@_); }
532 File::ConfigDir::_plug_dir_source($pure_src, 1); # see 2nd arg is true
533  
534 Registers more sources to ask for suitable directories to check or search
535 for config files. Each L</config_dirs> will traverse them in subsequent
536 invocations, too.
537  
538 Returns the number of directory sources in case of succes. Returns nothing
539 when C<$dir_src> is not a code ref.
540  
541 =cut
542  
543 sub _plug_dir_source
544 {
545 5 ▶ my ($dir_source, $pure) = @_;
Subroutine _plug_dir_source: called CC=4 SCAR=13.9
546  
547 5 ▶ $dir_source or return;
Branch: 1 branch
BranchTrueFalseTotal
$dir_source or return 2 3 5
548 3 ▶ "CODE" eq ref $dir_source or return;
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq ref $dir_source or return 1 2 3
549  
550 2 push(@extensible_bases, $dir_source);
551 2 ▶ $pure and push(@pure_bases, $#extensible_bases);
Branch: 1 branch
BranchTrueFalseTotal
$pure and push @pure_bases, $#extensible_bases 1 1 2
552 2 1;
553 }
554  
555 =head1 AUTHOR
556  
557 Jens Rehsack, C<< <rehsack at cpan.org> >>
558  
559 =head1 BUGS
560  
561 Please report any bugs or feature requests to
562 C<bug-File-ConfigDir at rt.cpan.org>, or through the web interface at
563 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-ConfigDir>.
564 I will be notified, and then you'll automatically be notified of progress
565 on your bug as I make changes.
566  
567 =head1 SUPPORT
568  
569 You can find documentation for this module with the perldoc command.
570  
571 perldoc File::ConfigDir
572  
573 You can also look for information at:
574  
575 =over 4
576  
577 =item * RT: CPAN's request tracker
578  
579 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-ConfigDir>
580  
581 =item * AnnoCPAN: Annotated CPAN documentation
582  
583 L<http://annocpan.org/dist/File-ConfigDir>
584  
585 =item * CPAN Ratings
586  
587 L<http://cpanratings.perl.org/d/File-ConfigDir>
588  
589 =item * Search CPAN
590  
591 L<http://search.cpan.org/dist/File-ConfigDir/>
592  
593 =back
594  
595 =head1 ACKNOWLEDGEMENTS
596  
597 Thanks are sent out to Lars Dieckow (daxim) for his suggestion to add
598 support for the Base Directory Specification of the Free Desktop Group.
599 Matthew S. Trout (mst) earns the credit to suggest C<singleapp_cfg_dir>
600 and remind about C</usr/local/etc>.
601  
602 =head1 LICENSE AND COPYRIGHT
603  
604 Copyright 2010-2018 Jens Rehsack.
605  
606 This program is free software; you can redistribute it and/or modify it
607 under the terms of either: the GNU General Public License as published
608 by the Free Software Foundation; or the Artistic License.
609  
610 See http://dev.perl.org/licenses/ for more information.
611  
612 =head1 SEE ALSO
613  
614 L<File::HomeDir>, L<File::ShareDir>, L<File::BaseDir> (Unices only)
615  
616 =cut
617  
618 5 1; # End of File::ConfigDir
↑ Summary