At 09:58 AM 4/21/2006 -0700, David Ressler wrote:
>I have a library of Perl routines in which "use Authen::SASL;" appears and is
>used in some, but not all, of my various websites. In those sites where I don't
>use it, Authen is not installed, but the script fails in these sites because
>"use" is evaluated at compile time, not run time.
12.2. Trapping Errors in require or use
Problem
You need to load in a module that might not be present on your system. This
normally results in a fatal exception.
You want to detect and trap these failures.
Solution
Wrap the require or use in an eval, and wrap the eval in a BEGIN block:
# no import
BEGIN {
unless (eval "require $mod") {
warn "couldn't load $mod: $@";
}
}
# imports into current package
BEGIN {
unless (eval "use $mod") {
warn "couldn't load $mod: $@";
}
}
There is also use if. (if.pm)
use if CONDITION, MODULE => ARGUMENTS;
--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede malis"
00000100
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs