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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho