3

I'd like to be able to run this test on each module in the list. Not sure how to ger perl looping over each item.

use Module::Load;
eval {
  load Image::Magick;
  1;
} or die "you need Module to run this program";
  • Bit::Vector
  • Carp::Clan
  • Check::ISA
  • DBD::Oracle
  • DBI
  • Data::GUID
  • Data::OptList
  • Data::TreeDumper
  • Data::UUID
  • Date::Calc
  • Devel::Size
  • ExtUtils::MakeMaker
  • Log::Dispatch
  • Log::Dispatch::File::Rolling
  • Log::Dispatch::FileRotate
  • Log::Log4perl
  • Params::Util
  • Params::Validate
  • Sort::Naturally
  • Sub::Exporter
  • Sub::Install
  • Sub::Uplevel
  • Sys::Syslog
  • Term::Size
  • Test::Exception
  • Test::Simple
  • Test::use::ok
  • Tree::Simple
  • 2
    It looks like you're solving a problem the wrong way here. Learn how to [package your application as a distribution](http://p3rl.org/modstyle#Packaging), and simply [declare the dependencies in the distribution meta file](http://p3rl.org/CPAN::Meta::Spec#Prereq-Spec). At install time, the dependencies will be taken care of automatically. – daxim Feb 18 '12 at 11:21
  • Possible duplicate of http://stackoverflow.com/questions/9209413/check-the-list-of-module-installed-in-machine or http://stackoverflow.com/questions/802107/how-do-i-check-whether-a-perl-module-is-installed – dgw Feb 18 '12 at 13:22

4 Answers4

6

Have a try with:

#!/usr/bin/perl 
use 5.010;
use strict;
use warnings;

my @modules = qw(
    Bit::Vector
    Carp::Clan
    Check::ISA
    DBD::Oracle
    DBI
    Tree::Simple
);

for(@modules) {
    eval "use $_";
    if ($@) {
        warn "Not found : $_" if $@;
    } else {
        say "Found : $_";
    }
}
Toto
  • 89,455
  • 62
  • 89
  • 125
3

If you don't need to use Perl to do this, you could do this in a shell script:

#!/bin/sh
MODULES="Data::Dumper Foobar::Test"

for i in $MODULES ; do
  if $(perl -M$i -e '1;' >/dev/null 2>&1 ); do
    echo "Ok."
  else
    echo "No." 
  fi
done

You could do something else other than using echo.

The code sequence:

perl -MData::Dumper '1;'

will exit with an error value of 0 (ok) and

perl -MFoobar::Test '1;'

will exit with an error value of 2 (error occurred).

Mei
  • 1,129
  • 1
  • 11
  • 20
  • Thanks for the suggestion. Here's what I get when I run your syntax:
    perllist: line 6: syntax error near unexpected token `do'
    perllist: line 6: ` if $(perl -M$i -e '1;' >/dev/null 2>&1 ); do'
    –  Feb 18 '12 at 03:27
  • I think I may need to strip out the quotes and commas in the MODULES list.MODULES="'Bit::Vector', 'Derp::Derp', 'Carp::Clan', 'Check::ISA', 'DBD::Oracle', 'DBI Data::GUID', 'Data::OptList', 'Data::TreeDumper', 'Data::UUID', 'Date::Calc', 'Devel::Size', 'ExtUtils::MakeMaker', 'Log::Dispatch', 'Log::Dispatch::File::Rolling', 'Log::Dispatch::FileRotate', 'Log::Log4perl', 'Params::Util', 'Params::Validate', 'Sort::Naturally', 'Sub::Exporter', 'Sub::Install', 'Sub::Uplevel', 'Sys::Syslog', 'Term::Size', 'Test::Exception', 'Test::Simple', 'Test::use::ok', 'Tree::Simple'" –  Feb 18 '12 at 03:37
1

I want to echo @daxim's comment that it seems that you are looking to make a distribution of your module. For this I would look to either Module::Build or Dist::Zilla. Almost all of my modules use one of these two mechanisms, so if you need examples, feel free to thumb around my GitHub. Look for Build.PL or dist.ini files (for M::B or D::Z respectively).

Joel Berger
  • 20,180
  • 5
  • 49
  • 104
0

full script

  # a around M42's answer with some more user-kindness ...
  use strict ; use warnings ; 
  use 5.10.0 ; 

  #  quick and dirty check for prerequisites perl modules:
  #  courtesy of:http://stackoverflow.com/a/9340304/65706
  #  if you have a calling bash script call by :
  #  perl "/path/to/isg_pub_preq_cheker.pl"
  #  export ret=$?
  #  test $ret -ne 0 && doExit 1 "[FATAL] perl modules not found!!!"


  # check that all the required modules are installed
  my ( $ret , $msg ) = doCheckRequiredModules();

  unless ( $ret == 0 ) {
     print "$msg" ; 
     # give some time for the user to react
     sleep 7;
  }

  exit(0);

  sub doCheckRequiredModules {

     my @modules = qw(
        ExtUtils::MakeMaker
        Test::More
        Test::Deep
        File::Copy::Recursive
        HTML::TreeBuilder
        HTML::TreeBuilder::XPath
        HTML::TableExtract
        HTML::ElementTable
        Data::Printer
     );

     for(@modules) {
         eval "use $_";
         if ($@) {

           my $msg = "\n\n\n [FATAL] did not found the following perl module: $_ " ; 
           $msg .= "\n install it in the shell by running the following command:" ; 
           # if the user knows already the difference between the running the cmd 
           # with sudo or he / she probably knows already how-to install perl modules
           $msg .= "\n sudo perl -MCPAN -e 'install $_'\n\n\n" ; 
           $msg .= "\n if you seem to be stuck in circular reference kind of loop try even :\n" ; 
           $msg .= "\n sudo perl -MCPAN -e 'CPAN::Shell->force(qw( install $_));'\n" ; 
           $msg .= "\n You may end-up now with Ctrl + C \n\n\n" ; 

           return ( 1, "$msg")  if $@;
         } else {
             say "[INFO ] == ok == check for prerequisite perl module : $_";
         }
     }

     return ( 0 , "all required modules found" ) ;   
  }
  #eof sub
Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53