Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Check for another program availability

by Tux (Canon)
on Apr 11, 2021 at 07:32 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131101]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-19 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found