perl script to check if perl modules are installed

前端 未结 4 1379
无人共我
无人共我 2021-01-20 19:27

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::Mag         


        
相关标签:
4条回答
  • 2021-01-20 20:15

    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).

    0 讨论(0)
  • 2021-01-20 20:23

    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
    
    0 讨论(0)
  • 2021-01-20 20:25

    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).

    0 讨论(0)
  • 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 : $_";
        }
    }
    
    0 讨论(0)
提交回复
热议问题