Check for Module

Is it possible to check if a module is installed and if so set $test = 1;

I want to check for LWP::Simple
mmcwAsked:
Who is Participating?
 
Kim RyanIT ConsultantCommented:
Here is a very simple solution. I know that the ExtUtils::MakeMaker has functions to detect if modules are installed. You can also specify pre-requisites (ie other modules) needed for your own module.

$module_installed = 0;
$module_name = "LWP/Simple.pm";
foreach $dir ( @INC )
{
   ( -e "$dir/$module_name" ) and $module_installed = 1;
}
if ( $module_installed )
{
   print("$module_name is installed\n");
}
else
{
   print("$module_name is not installed\n");
}
0
 
mmcwAuthor Commented:
An other little question:
 What will happen if you try to use a module that is not installed:
example: use LWP::Simple;
0
 
ozoCommented:
eval{ require LWP::Simple };
 $test = !$@;
 die $@ if $@;
0
 
guadalupeCommented:
You will get an error saying that the module cannot be dound in any of the paths in the @INC array.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.