I want to detect when a user is missing a required module and print a friendly error message explaining what they need to install.
So far I tried putting this at the beginning of my script:
eval {
use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
};
if ($@) {
die "Error: IO::Uncompress::Gunzip not installed: $@";
}
But Perl seems to die on the “use” line instead of the “die” line and never prints my error message.
What’s happening here is that the module is being
used at compile-time, regardless of the fact that it is inside theevalblock.This is also why naab’s suggestion to change from
eval BLOCKform toeval EXPRform works as well; the expression is evaluated at run-time.Changing the
usetorequirewill attempt to load the module at run-time:Output