File Coverage

blib/lib/Unix/SearchPathGuess.pm
Criterion Covered Total %
statement 19 20 95.0
branch 1 2 50.0
condition 1 2 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 29 32 90.6


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