Anno said:
That isn't quite right. "use" returns nothing at run time, whether it
succeeded or not.
Use() doesn't return anything, but it *does* die if the module can't be
found, and the eval() returns false if that happens. So you can put the
eval() in a conditional block, or check $@ afterwards - it works either way.
Also, I would use block eval when string eval isn't necessary. So
BEGIN {
eval { use Time::HiRes qw(time) };
warn "Couldn't load module: $@" if $@;
}
A string eval *is* necessary here because of the different semantics of
a block vs. a string eval. A string eval turns compile-time errors that
happen within the evaled string into run-time errors. A block eval won't
do that; compile-time errors within the block cause the script as a
whole to die.
Examine the output of this:
BEGIN {
unless (eval 'use NoSuchModule' ) {
warn "My custom error: $@";
}
}
This prints "My custom error: Can't locate NoSuchModule.pm in @INC (@INC
contains: ...) at (eval 1) line 2.", but it's a warning not an error,
and the program continues.
Compare that to the output from this:
BEGIN {
unless (eval {use NoSuchModule}) {
warn "My custom error: $@";
}
}
This prints "Can't locate NoSuchModule.pm in @INC (@INC contains: ...)
at ./test.pl line 7.". Note the location of the error, and the lack of
the error printed by warn() - the script as a whole failed to compile,
and eval() didn't catch the error.
sherm--