Unix::SearchPathGuess -- Make an educated guess for $ENV{'PATH'} in a Unixish system
# set $ENV{'PATH'} just for this sub sub mysub { local $ENV{'PATH'} = search_path_guess(); ... } # make a guess on the path to the ls program my $ls = cmd_path_guess('ls') # only run ls if a path to it was found if ($ls) { ... }
Unix::SearchPathGuess helps you make an educated guess about what a useful value for $ENV{'PATH'} would be if, for whatever reason, you don't already know. It also helps you look for a command in that path, and returns the full path to that command if it is found. Unix::SearchPathGuess is only useful on Unixish systems.
Unix::SearchPathGuess can be installed with the usual routine:
perl Makefile.PL make make test make install
@search_paths is an array of the guesses for the search path. It consists of
/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games
search_path_guess() returns a string that is a guessed value for $ENV{'PATH'}. Note that search_path_guess() DOES NOT set $ENV{'PATH'}, it just returns a string with which you can set $ENV{'PATH'} as you like.
If $ENV{'PATH'} is already defined, the guessed path is appended to it and returned.
A good practice is to only use search_path_guess() to set the local value of $ENV{'PATH'}. That way you aren't messing around with a global that some function you don't know about is relying on to be a certain way. So, for example, you could set $ENV{'PATH'} just for a single subroutine:
sub mysub { local $ENV{'PATH'} = search_path_guess(); ... }
cmd_path_guess() searches the guessed $ENV{'PATH'} for the given command. Returns the full path to that command if it was found, undef otherwise.
For example, to find the path to the ls command, you would do something like this:
ls
my $ls = cmd_path_guess('ls') # only run ls if a path to it was found if ($ls) { ... }
Note that you should check if the command was found before using it.
Copyright (c) 2014 by Miko O'Sullivan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This software comes with NO WARRANTY of any kind.
Miko O'Sullivan miko@idocs.com
Version: 0.11
Initial release
Fixed prerequisite declaration for File::Which so that it requires version 1.09.
Clarifed and cleaned up documentation.
To install Unix::SearchPathGuess, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Unix::SearchPathGuess
CPAN shell
perl -MCPAN -e shell install Unix::SearchPathGuess
For more information on module installation, please visit the detailed CPAN module installation guide.