Can perl check if executable exists

R

Random Task

Hi ...

I have a perl scrip ...

It simple calls make ... and does some work after that ...

Before I call make I would like to see if it is in my PATH. If it is not
in my Path i would like to check for make in a couple hardcoded visual
studio directories ... i.e. VS .NET or VS6 ...

Thanks in advance,

Jim
 
P

Paul Lalli

Random said:
I have a perl scrip ...

It simple calls make ... and does some work after that ...

Before I call make I would like to see if it is in my PATH. If it is not
in my Path i would like to check for make in a couple hardcoded visual
studio directories ... i.e. VS .NET or VS6 ...

Here's a brute-force algorithm:

1) Get the PATH variable, via the %ENV hash (so, $path = $ENV{PATH} )
2) Split on whatever your path separator is to obtain a list of
directories in your PATH.
3) Loop through each directory,
3a) Each iteration of the loop, test the existance of the path composed
of the current directory and the filename
4) If still not found, test the existance of the path composed of your
hardcoded directory and the filename

Some documents to help you on this goal:
perldoc -f -X
perldoc perlvar
perldoc perlsyn

Once you've made an attempt at solving your problem, if it doesn't
work, feel free to request help. To maximize the help you're likely
to get from this newsgroup, read the Posting Guidelines (and follow
them in your reply) that are posted twice a week.

Paul Lalli
 
J

John Bokma

Random Task said:
Hi ...

I have a perl scrip ...

It simple calls make ... and does some work after that ...

Before I call make I would like to see if it is in my PATH. If it is not
in my Path i would like to check for make in a couple hardcoded visual
studio directories ... i.e. VS .NET or VS6 ...

With -e you can check if a file exists. Maybe you want to check the md5
sum of the file as well (for example) to be sure it's the one you expect.

$ENV{ PATH } gives you the contents of the PATH variable.
$ENV{ ProgramFiles } gives you the Program Files directory

(type set in a command prompt to see all, but make sure to check if they
are set by XP and not some program you've installed)

HTH,
 
J

John W. Krahn

Random said:
I have a perl scrip ...

It simple calls make ... and does some work after that ...

Before I call make I would like to see if it is in my PATH. If it is not
in my Path i would like to check for make in a couple hardcoded visual
studio directories ... i.e. VS .NET or VS6 ...

use Env '@PATH';

my $executable = 'make';

my $exec_exists = grep -x "$_/$executable", @PATH;



John
 
B

Bart Lateur

Here's a brute-force algorithm:

1) Get the PATH variable, via the %ENV hash (so, $path = $ENV{PATH} )
2) Split on whatever your path separator is to obtain a list of
directories in your PATH.
3) Loop through each directory,
3a) Each iteration of the loop, test the existance of the path composed
of the current directory and the filename
4) If still not found, test the existance of the path composed of your
hardcoded directory and the filename

What could also be helpful is path() in File::Spec, or
File::Spec::Functions. Like this:

use File::Spec;
my @path = File::Spec->path;


The only problem I see with (3a), is that you don't take care of file
extensions. For example, if you search for "nmake", you should really
test existence of "nmake.exe". That is not easy to do portable -- Unix
doesn't use executable file extensions. And I don't know of a (standard)
module that can take care of that, like File::Spec.
 
A

A. Sinan Unur

use Env '@PATH';

my $executable = 'make';

my $exec_exists = grep -x "$_/$executable", @PATH;

That is not going to work on Windows, which is what the OP is using (not
because of the directory separator, but because make on Windows will most
likely be make.exe).

Here is an alternative:

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw( catfile path );

my @make_progs = qw( make.exe dmake.exe nmake.exe );
my @potential = map {
my $prog = $_;
map { catfile $_, $prog } path()
} @make_progs;

my @exists = grep { -x } @potential;

{ local $" = "\n"; print "@exists\n"; }

__END__

D:\Home\asu1\UseNet\clpmisc> pat
C:\opt\cygwin\bin\make.exe
C:\opt\vc\bin\nmake.exe
 
L

l v

A. Sinan Unur said:
That is not going to work on Windows, which is what the OP is using (not
because of the directory separator, but because make on Windows will most
likely be make.exe).

Incorporate $ENV{'PATHEXT'} into the program and loop through the
extensions, append to the end of $executable and test for existance.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

Len
 
L

l v

Bart said:
Paul Lalli wrote:




What could also be helpful is path() in File::Spec, or
File::Spec::Functions. Like this:

use File::Spec;
my @path = File::Spec->path;


The only problem I see with (3a), is that you don't take care of file
extensions. For example, if you search for "nmake", you should really
test existence of "nmake.exe". That is not easy to do portable -- Unix
doesn't use executable file extensions. And I don't know of a (standard)
module that can take care of that, like File::Spec.

What about testing for executable permissions (-x) if on Unix OS? -x
on win2k seems to work with limited testing. As far as the windows
extensions -- I just replyed to a different post regarding the use of
$ENV{'PATHEXT'}.

Len
 
A

A. Sinan Unur

Incorporate $ENV{'PATHEXT'} into the program and loop through the
extensions, append to the end of $executable and test for existance.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

I understand your point, and that might be just what the doctor ordered
if you are writing which, but with the OP's objective in mind, I don't
it is appropriate to look for make.vbs.

On the other hand, here you go:

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw( catfile path );

use constant PATH => path;
use constant PATHEXT => split /;/, $ENV{PATHEXT};

die "Missing arguments\n" unless @ARGV;

for my $arg ( @ARGV ) {
print "$_\n" for which($arg);
}

sub which {
my $name = shift;

grep { -e } map { my $file = $_;
map { catfile $_, $file } PATH
} map { $name . lc $_ } (q{}, PATHEXT);
}

__END__

D:\Home\asu1\UseNet\clpmisc\which> which.pl perl
C:\opt\Perl\bin\perl.exe
C:\opt\cygwin\bin\perl.exe
 
J

Jürgen Exner

Random said:

Hi Random
I have a perl scrip ...

It simple calls make ... and does some work after that ...

Before I call make I would like to see if it is in my PATH. If it is
not in my Path i would like to check for make in a couple hardcoded
visual studio directories ... i.e. VS .NET or VS6 ...

What about simply running
$foo = `which make`;
If it comes back empty, then you know that there is no program 'make' in
your path.
Otherwise you will get the path to the executable.

jue

PS: Yes, 'which' is available for Windows, too.
 
P

Paul Lalli

Bart said:
What could also be helpful is path() in File::Spec, or
File::Spec::Functions. Like this:

use File::Spec;
my @path = File::Spec->path;

Or even the Env module, which has the added benefit that you can change
your path directly as well...

use ENV qw/@PATH/;

Paul Lalli
 
B

Bart Lateur

Jürgen Exner said:
What about simply running
$foo = `which make`;
If it comes back empty, then you know that there is no program 'make' in
your path.
Otherwise you will get the path to the executable.

jue

PS: Yes, 'which' is available for Windows, too.

But it doesn't work... Cygwin which needs the command line

which make.exe

Portable? Hah!
 
R

Random Task

Random said:
Hi ...

I have a perl scrip ...

It simple calls make ... and does some work after that ...

Before I call make I would like to see if it is in my PATH. If it is not
in my Path i would like to check for make in a couple hardcoded visual
studio directories ... i.e. VS .NET or VS6 ...

Thanks in advance,

Jim

Thanks all:)

I ended up calling:

system("command -help")

and then checking the return value :)

Jim
 
A

A. Sinan Unur

Thanks all:)

I ended up calling:

system("command -help")

and then checking the return value :)

How does that help?

D:\Home\asu1> command -help
Specified COMMAND search directory bad
Microsoft(R) Windows DOS
(C)Copyright Microsoft Corp 1990-2001.

D>exit

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top