| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Unix::SearchPathGuess; |
|
2
|
1
|
|
|
1
|
|
381
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
28
|
|
|
3
|
1
|
|
|
1
|
|
386
|
use File::Which; |
|
|
1
|
|
|
|
|
759
|
|
|
|
1
|
|
|
|
|
71
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# debug tools |
|
6
|
|
|
|
|
|
|
# use Debug::ShowStuff ':all'; |
|
7
|
|
|
|
|
|
|
# use Debug::ShowStuff::ShowVar; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# version |
|
10
|
|
|
|
|
|
|
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
|
|
|
1
|
|
5
|
use base 'Exporter'; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
124
|
|
|
68
|
1
|
|
|
1
|
|
6
|
use vars qw[@EXPORT_OK %EXPORT_TAGS]; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
196
|
|
|
69
|
|
|
|
|
|
|
@EXPORT_OK = qw{search_path_guess cmd_path_guess}; |
|
70
|
|
|
|
|
|
|
%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
|
|
|
|
|
|
|
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 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
|
|
|
|
|
|
|
sub search_path_guess { |
|
135
|
2
|
|
50
|
2
|
1
|
25
|
my $rv = $ENV{'PATH'} || ''; |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# if there is a path, add a colon |
|
138
|
2
|
50
|
|
|
|
4
|
if ($rv) |
|
139
|
0
|
|
|
|
|
0
|
{ $rv .= ':' } |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# add "best guess" path |
|
142
|
2
|
|
|
|
|
9
|
$rv .= join(':', @search_paths); |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# return |
|
145
|
2
|
|
|
|
|
7
|
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 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
|
|
|
|
|
|
|
sub cmd_path_guess { |
|
176
|
1
|
|
|
1
|
1
|
100
|
my ($cmd) = @_; |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# TESTING |
|
179
|
|
|
|
|
|
|
# println subname(), ': ', $cmd; ##i |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# set local search path |
|
182
|
1
|
|
|
|
|
3
|
local $ENV{'PATH'} = search_path_guess(); |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# look for the command in the search path |
|
185
|
1
|
|
|
|
|
5
|
return which($cmd); |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
# |
|
188
|
|
|
|
|
|
|
# cmd_path_guess |
|
189
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# return true |
|
193
|
|
|
|
|
|
|
1; |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
__END__ |