blib/lib/File/SearchPath.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 76.0% 35 / 46 Conditioncond 72.2% 13 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 85.1% 143 / 168 CC 31 SCAR 23.0
CC
31
Cov
88%
searchpath
15.2
_env_to_dirs
10.9
_file_ok
8.2
CRAP 10.0
↑ Summary
1 package File::SearchPath;
2  
3 =head1 NAME
4  
5 File::SearchPath - Search for a file in an environment variable path
6  
7 =head1 SYNOPSIS
8  
9 use File::SearchPath qw/ searchpath /;
10  
11 $file = searchpath( 'libperl.a', env => 'LD_LIBRARY_PATH' );
12 $file = searchpath( 'my.cfg', env => 'CFG_DIR', subdir => 'ME' );
13  
14 $path = searchpath( $file, env => 'PATH', exe => 1 );
15 $path = searchpath( $file, env => 'PATH', dir => 1 );
16  
17 $file = searchpath( 'ls', $ENV{PATH} );
18  
19 $exe = searchpath( 'ls' );
20  
21 =head1 DESCRIPTION
22  
23 This module provides the ability to search a path-like environment
24 variable for a file (that does not necessarily have to be an
25 executable).
26  
27 =cut
28  
29 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use base qw/ Exporter /;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use vars qw/ $VERSION @EXPORT_OK /;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 $VERSION = '0.07';
41  
42 1 @EXPORT_OK = qw( searchpath );
43  
44 =head1 FUNCTIONS
45  
46 The following functions can be exported by this module.
47  
48 =over 4
49  
50 =item B<searchpath>
51  
52 This is the core function. The only mandatory argument is the name of
53 a file to be located. The filename should not be absolute although it
54 can include directory specifications.
55  
56 $path = searchpath( $file );
57 @matches = searchpath( $file );
58  
59 If only two arguments are provided, it is assumed that the second
60 argument is a path-like string. This interface is provided for
61 backwards compatibility with C<File::SearchPath> version 0.01. It is not
62 as portable as specifying the name of the environment variable. Note also
63 that no specific attempt will be made to check whether the file is
64 executable when the subroutine is called in this way.
65  
66 $path = searchpath( $file, $ENV{PATH} );
67  
68 By default, this will search in $PATH for executable files and is
69 equivalent to:
70  
71 $path = searchpath( $file, env => 'PATH', exe => 0 );
72  
73 Hash-like options can be used to alter the behaviour of the
74 search:
75  
76 =over 8
77  
78 =item env
79  
80 Name of the environment variable to use as a starting point for the
81 search. Should be a path-like environment variable such as $PATH,
82 $LD_LIBRARY_PATH etc. Defaults to $PATH. An error occurs if the
83 environment variable is not set or not defined. If it is defined but
84 contains a blank string, the current directory will be assumed.
85  
86 =item exe
87  
88 If true, only executable files will be located in the search path.
89 If $PATH is being searched, the default is for this to be true. For all
90 other environment variables the default is false. If "dir" option
91 is specified "exe" will always default to false.
92  
93 =item dir
94  
95 If true, only directories will be located in the search path. Default
96 is false. "dir" and "exe" are not allowed to be true in the same
97 call. (triggering a croak() on error).
98  
99 =item subdir
100  
101 If you know that your file is in a subdirectory of the path described
102 by the environment variable, this direcotry can be specified here.
103 Alternatively, the path can be included in the file name itself.
104  
105 =back
106  
107 In scalar context the first match is returned. In list context all
108 matches are returned in the order corresponding to the directories
109 listed in the environment variable.
110  
111 Returns undef (or empty list) if no match could be found.
112  
113 If an absolute file name is provided, that filename is returned if it
114 exists and is readable, else undef is returned.
115  
116 =cut
117  
118 10 ▶sub searchpath {
Subroutine searchpath: called CC=15 SCAR=27.2
Pod documented
119 10 my $file = shift;
120  
121 # read our arguments and assign defaults. Behaviour depends on whether
122 # we have a single argument remaining or not.
123 10 my %options;
124  
125 # If we only have one more argument then it must be the contents
126 # of a path variable
127 my $path_contents;
128 10 ▶ if ( scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 1 9 10
129 # Read the contents and store in options hash
130 # along with the backwards compatibility behaviour
131 1 %options = ( contents => shift,
132 exe => 0,
133 dir => 0,
134 subdir => File::Spec->curdir,
135 );
136  
137 } else {
138  
139 # options handling since we have zero or more than one argument.
140 # set up the default behaviour
141 # The exe() defaulting is env dependent
142 9 my %defaults = ( env => 'PATH', subdir => File::Spec->curdir );
143  
144 9 %options = ( %defaults, @_ );
145  
146 # if we specify a dir option then we default to no exe regardless
147 # of PATH
148 9 ▶ if (!exists $options{exe} && !exists $options{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"exe"} or exists $options{"dir"}) 4 5 9
Condition100%3/3
exists $options{"exe"} or exists $options{"dir"}
l!l&&r!l&&!r
324
MC/DC100%2/2
exists $options{"exe"} or exists $options{"dir"}
exists $options{"exe"} exists $options{"dir"}
Truth table100%3/3
A or B
A= exists $options{"exe"}B= exists $options{"dir"}
ABresult
1X1
011
000
149 # exe was not specified
150 4 ▶ $options{exe} = ( $options{env} eq 'PATH' ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$options{'env'} eq 'PATH' ? : 0 4 4
151 }
152  
153 croak "Both exe and dir options were set in call to searchpath()"
154 9 ▶ if ($options{exe} && $options{dir});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"exe"} and $options{"dir"} 0 9 9
Condition66%2/3
$options{"exe"} and $options{"dir"}
!ll&&!rl&&r
720
MC/DC0%0/2
$options{"exe"} and $options{"dir"}
$options{"exe"} $options{"dir"}
Truth table66%2/3
A and B
A= $options{"exe"}B= $options{"dir"}
ABresult
0X0
100
111
155  
156 }
157  
158 # check for absolute file name and behave accordingly
159 10 ▶ if (File::Spec->file_name_is_absolute( $file )) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($file)) 1 9 10
160 1 ▶ return (_file_ok($file, $options{exe}, $options{dir}) ? $file : () );
Branch: 1 branch
BranchTrueFalseTotal
_file_ok($file, $options{'exe'}, $options{'dir'}) ? : 1 0 1
161 }
162  
163 # if exe is true we can simply use Env::Path directly. It doesn't
164 # really simplify any code though since we still have to write
165 # the other search
166  
167 # first get the search directories from the path variable
168 9 my @searchdirs = _env_to_dirs( $options{env}, $options{contents} );
169  
170 # Now do the looping
171 9 my @matches;
172  
173 9 for my $d (@searchdirs) {
174 # blank means current directory
175 19 ▶ $d = File::Spec->curdir unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 19 19
176  
177 # Create the filename
178 19 my $testfile;
179 19 ▶ if ($options{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'dir'}) { } 2 17 19
180 2 $testfile = File::Spec->catdir( $d, $options{subdir}, $file);
181 } else {
182 17 $testfile = File::Spec->catfile( $d, $options{subdir}, $file);
183 }
184  
185 # does the file exist?
186 19 ▶ next unless _file_ok( $testfile, $options{exe}, $options{dir} );
Branch: 1 branch
BranchTrueFalseTotal
unless _file_ok($testfile, $options{"exe"}, $options{"dir"}) 9 10 19
187  
188 # File looks to be found store it
189 10 push(@matches, $testfile);
190  
191 # if we are in a scalar context we do not need to keep on looking
192 10 ▶ last unless wantarray();
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 4 6 10
193  
194 }
195  
196 # return the result
197 9 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 6 9
198 3 return @matches;
199 } else {
200 6 return $matches[0];
201 }
202 }
203  
204 =back
205  
206 =begin __PRIVATE__FUNCTIONS__
207  
208 =head2 Private Functions
209  
210 =over 4
211  
212 =item B<_env_to_dirs>
213  
214 Given an environment variable, splits it into chunks and returns
215 the list of directories to be searched.
216  
217 If Env::Path is installed, it is used since it understands a more
218 varied set of path delimiters, otherwise the variable is split on
219 the value of $Config{path_sep}.
220  
221 @dirs = _env_to_dirs( 'PATH' );
222  
223 Also, we can pass in the actual contents as a second argument. In this
224 case it is only read if the first is undef.
225  
226 @dirs = _env_to_dirs( undef, 'dir1:dir2' );
227  
228 =cut
229  
230 9 ▶sub _env_to_dirs {
Subroutine _env_to_dirs: called CC=10 SCAR=23.9
231 9 my $var = shift;
232 9 my $contents = shift;
233  
234 9 ▶ if (!defined $var && !defined $contents) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $var or defined $contents) 0 9 9
Condition66%2/3
defined $var or defined $contents
l!l&&r!l&&!r
810
MC/DC0%0/2
defined $var or defined $contents
defined $var defined $contents
Truth table66%2/3
A or B
A= defined $varB= defined $contents
ABresult
1X1
011
000
235 0 croak "Error extracting directories from environment. No defined values supplied. Internal programming error";
236 }
237  
238 # behaviour now depends on whether we were given the actual
239 # contents or the name of the variable. Variable name trumps contents.
240  
241 9 ▶ if (defined $var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var) 8 1 9
242  
243 croak "Environment variable $var is not defined. Unable to search it\n"
244 8 ▶ if !exists $ENV{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$var} 0 8 8
245  
246 croak "Environment variable does exist but it is not defined. Unable to search it\n"
247 8 ▶ unless defined $ENV{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$var} 0 8 8
248 }
249 9 my $use_env_path;
250 {
251 # Localise $@ so that we can use this command from perldl shell
252 9 local $@;
253 9 eval { require Env::Path };
254 9 ▶ $use_env_path = ( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 9 9
255 }
256 9 ▶ if (!$use_env_path || defined $contents) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $use_env_path or defined $contents) { } 1 8 9
Condition66%2/3
not $use_env_path or defined $contents
l!l&&r!l&&!r
018
MC/DC50%1/2
not $use_env_path or defined $contents
not $use_env_path defined $contents
Truth table66%2/3
not(A) or B
A= not $use_env_pathB= defined $contents
ABresult
1X1
011
000
257 # no Env::Path so we just split on :
258 1 ▶ my $path = (defined $contents? $contents : $ENV{$var});
Branch: 1 branch
BranchTrueFalseTotal
defined $contents ? : 1 0 1
259 1 my $ps = $Config{path_sep};
260 1 return split(/\Q$ps\E/, $path);
261 } else {
262 8 my $path = Env::Path->$var;
263 8 return $path->List;
264 }
265 }
266  
267 =item B<_file_ok>
268  
269 Tests the file for existence, fileness and readability.
270  
271 $isthere = _file_ok( $file );
272  
273 Returns true if the file passes.
274  
275 An optional argument can be used to add a test for exectuableness.
276  
277 $isthere_and_exe = _file_ok( $file, 1 );
278  
279 An additional optional argument can be used to add a test for
280 directory as opposed to file existence.
281  
282 $isthere_and_dir = _file_ok( $dir, 0, 1 );
283  
284 =cut
285  
286 20 ▶sub _file_ok {
Subroutine _file_ok: called CC=8 SCAR=21.1
287 20 my $testfile = shift;
288 20 my $testexe = shift;
289 20 my $testdir = shift;
290  
291 # do not allow both dir and exe flags
292 20 ▶ return 0 if ($testexe && $testdir);
Branch: 1 branch
BranchTrueFalseTotal
if $testexe and $testdir 0 20 20
Condition66%2/3
$testexe and $testdir
!ll&&!rl&&r
1730
MC/DC0%0/2
$testexe and $testdir
$testexe $testdir
Truth table66%2/3
A and B
A= $testexeB= $testdir
ABresult
0X0
100
111
293  
294 20 ▶ return unless -e $testfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $testfile 7 13 20
295 13 ▶ return unless -r $testfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $testfile 0 13 13
296  
297 13 ▶ if ($testdir) {
Branch: 2 branches
BranchTrueFalseTotal
if ($testdir) { } 2 11 13
elsif ($testexe) { } 2 9 11
298 2 return (-d $testfile);
299 } elsif ($testexe) {
300 2 ▶ return (-f $testfile && -x $testfile);
Condition66%2/3
-f $testfile && -x $testfile
!ll&&!rl&&r
011
MC/DC50%1/2
-f $testfile && -x $testfile
-f $testfile -x $testfile
Truth table66%2/3
A && B
A= -f $testfileB= -x $testfile
ABresult
0X0
100
111
301 } else {
302 9 return (-f $testfile);
303 }
304 }
305  
306  
307 =end __PRIVATE__FUNCTIONS__
308  
309 =head1 HISTORY
310  
311 C<File::SearchPath> used to exist on CPAN (now on backpan) and was
312 written by Robert Spier. This version is completely new but retains
313 an interface that is compatible with Robert's version. Thanks to
314 Robert for allowing me to reuse this module name.
315  
316 =head1 NOTES
317  
318 If C<Env::Path> module is installed it will be used. This allows for
319 more flexibility than simply assuming colon-separated paths.
320  
321 =head1 SEE ALSO
322  
323 L<Env::Path>, L<File::Which>, L<File::Find>, L<File::Find::Run>,
324 L<File::Where>.
325  
326 =head1 AUTHOR
327  
328 Tim Jenness E<lt>tjenness@cpan.orgE<gt>
329  
330 Copyright (C) 2005,2006, 2008 Particle Physics and Astronomy Research Council.
331 Copyright (C) 2009-2010 Science and Technology Facilities Council.
332 Copyright (C) 2015 Tim Jenness
333 All Rights Reserved.
334  
335 This program is free software; you can redistribute it and/or modify it under
336 the terms of the GNU General Public License as published by the Free Software
337 Foundation; either version 2 of the License, or (at your option) any later
338 version.
339  
340 This program is distributed in the hope that it will be useful,but WITHOUT ANY
341 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
342 PARTICULAR PURPOSE. See the GNU General Public License for more details.
343  
344 You should have received a copy of the GNU General Public License along with
345 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
346 Place,Suite 330, Boston, MA 02111-1307, USA
347  
348 =cut
349  
350 1 1;
↑ Summary