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
Who is Participating?
Kim RyanConnect With a Mentor IT 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/";
foreach $dir ( @INC )
   ( -e "$dir/$module_name" ) and $module_installed = 1;
if ( $module_installed )
   print("$module_name is installed\n");
   print("$module_name is not installed\n");
mmcwAuthor Commented:
An other little question:
 What will happen if you try to use a module that is not installed:
example: use LWP::Simple;
eval{ require LWP::Simple };
 $test = !$@;
 die $@ if $@;
You will get an error saying that the module cannot be dound in any of the paths in the @INC array.
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.