Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Check for another program availability

by Tux (Canon)
on Apr 11, 2021 at 07:32 UTC ( #11131101=note: print w/replies, xml ) Need Help??


in reply to Check for another program availability

The suggested File::Which is probably *the* solution you want.

The suggested File::Spec is probably (very) good to get used to when writing code that should work on all types of OS's.

I want to add that returning the actual path of the tool/program/script/file is a lot more useful than returning 1.

I also want to say that your approach is less than perfect as you do not skip empty $PATH elements and elements that do not exist or are not a directory. This matters a lot if you are dealing with environments where the environment cannot be trusted. I'd suggest (ignoring both mentioned modules):

use List::Util qw( first ); sub available { my $prog = shift; first { -x "$_/$prog } grep { m/\S/ && -d } split m/:+/ => $ENV{PA +TH}; }

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Check for another program availability
by hrcerq (Scribe) on Apr 11, 2021 at 08:30 UTC

    I see. Well, thank you for your suggestions, they'll be nice improvements to my approach.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11131101]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-06-15 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (74 votes). Check out past polls.

    Notices?