blib/lib/Unix/SearchPathGuess.pm

filter: Statementstmt 96.0% 24 / 25 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 89.4% 34 / 38 CC 3 SCAR 6.3
CC
3
Cov
90%
search_path_guess
3.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.9
↑ Summary
1 package Unix::SearchPathGuess;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # debug tools
6 # use Debug::ShowStuff ':all';
7 # use Debug::ShowStuff::ShowVar;
8  
9 # version
10 1 our $VERSION = '0.11';
11  
12  
13 #------------------------------------------------------------------------------
14 # opening POD
15 #
16  
17 =head1 NAME
18  
19 Unix::SearchPathGuess -- Make an educated guess for $ENV{'PATH'} in a
20 Unixish system
21  
22 =head1 SYNOPSIS
23  
24 # set $ENV{'PATH'} just for this sub
25 sub mysub {
26 local $ENV{'PATH'} = search_path_guess();
27 ...
28 }
29  
30 # make a guess on the path to the ls program
31 my $ls = cmd_path_guess('ls')
32  
33 # only run ls if a path to it was found
34 if ($ls) {
35 ...
36 }
37  
38 =head1 DESCRIPTION
39  
40 Unix::SearchPathGuess helps you make an educated guess about what a useful
41 value for $ENV{'PATH'} would be if, for whatever reason, you don't already
42 know. It also helps you look for a command in that path, and returns the
43 full path to that command if it is found. Unix::SearchPathGuess is only
44 useful on Unixish systems.
45  
46 =head1 INSTALLATION
47  
48 Unix::SearchPathGuess can be installed with the usual routine:
49  
50 perl Makefile.PL
51 make
52 make test
53 make install
54  
55 =head1 GLOBALS AND FUNCTIONS
56  
57 =cut
58  
59 #
60 # opening POD
61 #------------------------------------------------------------------------------
62  
63  
64 #------------------------------------------------------------------------------
65 # export
66 #
67 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use vars qw[@EXPORT_OK %EXPORT_TAGS];
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 @EXPORT_OK = qw{search_path_guess cmd_path_guess};
70 1 %EXPORT_TAGS = ('all' => [@EXPORT_OK]);
71 #
72 # export
73 #------------------------------------------------------------------------------
74  
75  
76 #------------------------------------------------------------------------------
77 # paths to add to "best guess" path for external commands
78 #
79  
80 =head2 @search_paths
81  
82 @search_paths is an array of the guesses for the search path. It consists of
83  
84 /usr/local/sbin
85 /usr/local/bin
86 /usr/sbin
87 /usr/bin
88 /sbin
89 /bin
90 /usr/games
91  
92 =cut
93  
94 1 our @search_paths = qw{
95 /usr/local/sbin
96 /usr/local/bin
97 /usr/sbin
98 /usr/bin
99 /sbin
100 /bin
101 /usr/games
102 };
103 #
104 # paths to add to "best guess" path for external commands
105 #------------------------------------------------------------------------------
106  
107  
108  
109 #------------------------------------------------------------------------------
110 # search_path_guess
111 #
112  
113 =head2 search_path_guess()
114  
115 search_path_guess() returns a string that is a guessed value for $ENV{'PATH'}.
116 Note that search_path_guess() B<DOES NOT> set $ENV{'PATH'}, it just returns a
117 string with which you can set $ENV{'PATH'} as you like.
118  
119 If $ENV{'PATH'} is already defined, the guessed path is appended to it and
120 returned.
121  
122 A good practice is to only use search_path_guess() to set the local value of
123 $ENV{'PATH'}. That way you aren't messing around with a global that some
124 function you don't know about is relying on to be a certain way. So, for
125 example, you could set $ENV{'PATH'} just for a single subroutine:
126  
127 sub mysub {
128 local $ENV{'PATH'} = search_path_guess();
129 ...
130 }
131  
132 =cut
133  
134 2 ▶sub search_path_guess {
Subroutine search_path_guess: called CC=3 SCAR=12.0
Pod documented
135 2 ▶ my $rv = $ENV{'PATH'} || '';
Condition50%1/2
$ENV{'PATH'} || ''
l!l
02
MC/DC0%0/1
$ENV{'PATH'} || ''
$ENV{'PATH'}
Truth table50%1/2
A
A= $ENV{'PATH'}
Aresult
11
00
136
137 # if there is a path, add a colon
138 2 ▶ if ($rv)
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 2 2
139 0 { $rv .= ':' }
140
141 # add "best guess" path
142 2 $rv .= join(':', @search_paths);
143
144 # return
145 2 return $rv;
146 }
147 #
148 # search_path_guess
149 #------------------------------------------------------------------------------
150  
151  
152 #------------------------------------------------------------------------------
153 # cmd_path_guess
154 #
155  
156 =head2 cmd_path_guess()
157  
158 cmd_path_guess() searches the guessed $ENV{'PATH'} for the given command.
159 Returns the full path to that command if it was found, undef otherwise.
160  
161 For example, to find the path to the C<ls> command, you would do something
162 like this:
163  
164 my $ls = cmd_path_guess('ls')
165  
166 # only run ls if a path to it was found
167 if ($ls) {
168 ...
169 }
170  
171 Note that you should check if the command was found before using it.
172  
173 =cut
174  
175 1 ▶sub cmd_path_guess {
Subroutine cmd_path_guess: called CC=1 SCAR=0.0
Pod documented
176 1 my ($cmd) = @_;
177
178 # TESTING
179 # println subname(), ': ', $cmd; ##i
180
181 # set local search path
182 1 local $ENV{'PATH'} = search_path_guess();
183
184 # look for the command in the search path
185 1 return which($cmd);
186 }
187 #
188 # cmd_path_guess
189 #------------------------------------------------------------------------------
190  
191  
192 # return true
193 1 1;
194  
195 __END__
↑ Summary