You usually want a program to fail if it tries to load a module that is missing or doesn't compile. Sometimes, though, you'd like to recover from that error, perhaps trying an alternative module instead. As with any other exception, you insulate yourself from compilation errors with an
eval
.

You don't want to use
eval
{
BLOCK
}
, because this only traps run-time exceptions and
use
is a compile-time event. Instead, you must use
eval
"string"
, to catch compile-time problems as well. Remember,
require
on a bareword has a slightly different meaning than
require
on a variable. It adds a
".pm"
and translates double-colons into your operating system's path separators, canonically
/
(as in URLs), but sometimes
\
,
:
, or even
.
on some systems.

If you need to try several modules in succession, stopping at the first one that works, you could do something like this: