Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to run external app only if it exists

Tags:

perl

system

I've got a Perl script that as one of its final steps creates a compressed version of a file it has created. Due to some unfortunate setups, we cannot guarantee that a given customer will have a specific compression function. I want to put something like this together:

if ($has_jar) {
    system("jar -c $compressed_file $infile");
}
elsif ($has_zip) {
    system("zip -j $compressed_file $infile");
}
else {
    copy($infile, $compressed_file);
}

Where if they don't have either of the compression apps, it will just copy the file into the location of the compressed file without compressing it.

My sticky wicket here is that I'm not quite sure what the best way is to determine if they have jar or zip. It looks like I can used exec() instead of system() and take advantage of the fact that it only returns if it fails, but the script actually does do a couple of things after this, so that wouldn't work.

I also need this to be a portable solution as this script runs on both Windows and various Unix distros. Thanks in advance.

like image 621
Morinar Avatar asked Dec 05 '25 17:12

Morinar


2 Answers

I think your best bet is File::Which.

like image 130
innaM Avatar answered Dec 08 '25 10:12

innaM


See my multi-which.

like image 25
Sinan Ünür Avatar answered Dec 08 '25 08:12

Sinan Ünür



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!