I have a problem. I need to admit it. I can’t read anything without then chasing the information down all over the internet. It’s bad. :)
Yesterday, against my better judgment, I was reading John’s blog. The post was about a Python project that converts Java to Python. I figured like most of those X2Y programs it was probably fraught with problems, but I wanted to see the parser they came up with. So off I trudged to the svn repository. Well I never did get to the parser, because I saw they were wrapping the import of psycho in a try catch block. Now psycho is a non essential module, so you could easily dismiss any exception you caught, but it got me thinking that that could be handy in other languages as well, that way you could catch the exception and exit gracefully…unlike the nasty Perl error:
Can't locate Foo.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .)
That is confusing for non perlers, and could be made nicer using something like the above.
Here is the Python example:
try:
import psyco
except (ImportError, ):
pass
else:
psyco.full()
And then in Ruby:
begin
require 'Foo'
rescue LoadError
puts "Aieee"
end
Perl was a little trickier. If only because I wasn’t sure how to do it. Eric lent me a hand and we came up with:
eval "use Foo()";
if ($@) {
print "Aieee";
}
Anyway, I am adding this knowledge to my toolkit of programmatic-fu.