Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Check if module is installed

by DreamT (Pilgrim)
on Nov 16, 2009 at 10:22 UTC ( #807408=perlquestion: print w/ replies, xml ) Need Help??
DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
I've been using the following code in the beginning of my scripts to check that a module installed.
$Module_is_installed = "N"; eval { require Module::Module; }; if (!($@)) { $Module_is_installed = "J"; } if ($Module_is_installed eq "J") { # Do stuff }
But, I've noticed that it doesn't work in some cases, for example, when using
require Mail::Sendmail 0.75;
, it doesn't work, telling me that there's a syntax error. If I switch to "use", it tells me that it can't locate Mail/Sendmail.pm.
Any ideas?

Comment on Check if module is installed
Select or Download Code
Re: Check if module is installed
by keszler (Priest) on Nov 16, 2009 at 10:38 UTC
    perldoc -f require : no option for checking the version on a module like with use.

    I hate to ask a stupid question, but when troubleshooting I always start at the "is it plugged in" basics: Is Mail::Sendmail installed?

      Ok!
      No, It's not installed, the purpose was to create some "try/catch" code to catch the error if the module isn't installed, since the code is to be distributed to several machines.
        You want string eval:
        eval "use Some::Module 94.5"; if($@) { print "Oh noes, it's not installed!\n"; } else { ... }
Re: Check if module is installed
by biohisham (Priest) on Nov 16, 2009 at 10:51 UTC
    I use an array that holds the required modules and then evaluate $@, sometimes evaluating a module's existence is nice if you are sharing your code across machines
    @module = ("require Mail::Sendmail 0.75"); for(@module){ eval; if($@){ $_=~/\w+\W(\S+)/; print "Module $1 does not exist, please install it fir +st!\n"; }else{ #do stuff } };



    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Check if module is installed
by bart (Canon) on Nov 16, 2009 at 13:57 UTC

    I assume what you really want is to provide an option for the program can send mail in one of a few possible ways. Or, not at all, if it's not available. (Note that it's possible that you cannot send mail even though you have the necessary mail modules.)

    In that case, I'd write a little wrapper module that tries to load one of a few mail modules, writes down what module to use, and provides a hook to send mail using that module without exposing too much to the client program.

    Perhaps you can base that on a module like MIME::Lite, since that already has a way to transparently send mail using one of a few possible modules.

Re: Check if module is installed
by Fox (Pilgrim) on Nov 16, 2009 at 14:47 UTC
    how about use perl
    $module = "bigfloat"; qx(perl -M$module -e 'exit' 2>/dev/null); print "$module is not installed !!\n" if $? != 0;
Re: Check if module is installed
by bichonfrise74 (Vicar) on Nov 16, 2009 at 19:01 UTC
    I wonder if you can use something like this?
    #!/usr/bin/perl use strict; use ExtUtils::Installed; my $check_mod = ExtUtils::Installed->new(); die "File::Find is not installed" unless ( grep /File::Find/, $check_mod->modules() );
Re: Check if module is installed
by hecroce (Friar) on Nov 17, 2009 at 00:41 UTC
    Hi, This is what i did for a similar case:
    use strict; use warnings; use ExtUtils::Installed; # # First, check if all the required modules have # been installed inthe system this script will run on. # BEGIN { my @import_modules = ( 'Getopt::Lucid', 'Apache::Htpasswd', 'IO::CaptureOutput' ); my ($inst) = ExtUtils::Installed->new(); my (@installed_modules) = $inst->modules(); for ( @import_modules ) { eval{ $inst->validate($_) }; if($@) { print qq{\n Module $_ does not seem to be installed in this system. Please install the module and try again!\n }; exit 1; } # end 'if' } # end 'for' } # end 'BEGIN' block
use Module::Load::Conditional
by gnosti (Friar) on Nov 17, 2009 at 22:55 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://807408]
Approved by Corion
Front-paged by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-09-20 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls