Most unix-style command-line tools have a particular option to allow running the tool without actually doing anything -- e.g. just report the version number of the tool (like 'perl -v').

For tools that don't follow that general rule, you could use $ENV{PATH} to see if a file with a chosen name happens to exist in any of the user's current execution-path directories -- something like:

my $tool_name = "tar"; # simple example my $tool_path = ''; for my $path ( split /:/, $ENV{PATH} ) { if ( -f "$path/$tool_name" && -x _ ) { print "$tool_name found in $path\n"; $tool_path = "$path/$tool_name"; last; } } die "No $tool_name command available\n" unless ( $tool_path );
(updated to include the "tool_path" variable and the "die" statement)

I don't know, but you might need to  split /[;:]/ if you want that to work in the "standard" windows shell as well as on unix(-like) systems.

If you promise to only use a unix-like environment (incl, cygwin or uwin on windows), you could also depend on the "which" command:

my $tool_name = "tar"; my $tool_path = `which $tool_name`; die "No $tool_name command available\n" unless ( $tool_path );

(Another update: D'oh -- I should have known there was a module for this!)


In reply to Re: How to find whether an external commands exists by graff
in thread How to find whether an external commands exists by sathiya.sw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.