Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Odd variable naming question

Posted by Milo (Milo), 24 July 2003
I've encountered a situation where I need to call a lot of other programs from a perl script, but need to check first that they are installed (in case the users can't/don't set this beforehand). What's needed is something like:

my @progs = qw(prog1 prog2 prog3); # etc. etc.
foreach @progs
{
   $"$_" = `which $_`;
   # then, die if `which $-` =~ /not found/;
}

Obviously, the code above is complete rubbish, but it does express the sort of thing I'm after, i.e. the ability to call

system("$prog1 -options $file")

etc. later in the script. A shell script is not appropriate as I need to use lots of perl features (esp. bioperl) elsewhere in the script.
Or, have I got the wrong idea entirely?
Thanks,
Milo.

Posted by admin (Graham Ellis), 24 July 2003
You'll find lots of comments about eval and system and backquotes and special variables that return error statuses in the books ... however, these check whether something works when you try to run it and don't check earlier whether it exists.  If you have an operating system with "which" available, yes, use code along the lines you have pseudo-coded.   Here's a real code one I wrote:

Code:
#!/usr/bin/perl

@wanted = qw(grep cp cq gerp awk);

foreach $prog(@wanted) {
       $result = `which $prog`;
       ($noword,$what) = split(/\s+/,$result);

       if ($noword eq "no") {
               print ("PROBLEM: $what\n");
       } else {
               print ("$prog in $noword\n");
       }
}


and that gave me

Code:
[localhost:~/jul03] graham% ./pdo
grep in /usr/bin/grep
cp in /bin/cp
PROBLEM: cq
PROBLEM: gerp
awk in /usr/bin/awk
[localhost:~/jul03] graham%


Only problem to watch is that which returns a third format for shell built-ins, or it returns a "no" for them, depending on the operating system.

Posted by Milo (Milo), 25 July 2003
Thanks. That almost does what I was after, so I also required users to supply the full path to the executables in a config file. The combination should do the trick.
Milo.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2012: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho