What's the easiest way to install a missing Perl module?

前端 未结 24 2740
情深已故
情深已故 2020-11-21 05:28

I get this error:

Can\'t locate Foo.pm in @INC

Is there an easier way to install it than downloading, untarring, making, etc?

24条回答
  •  故里飘歌
    2020-11-21 05:31

    Seems like you've already got your answer but I figured I'd chime in. This is what I do in some scripts on an Ubuntu (or debian server)

    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    #I've gotten into the habit of setting this on all my scripts, prevents weird path issues if the script is not being run by root
    $ENV{'PATH'} = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
    
    #Fill this with the perl modules required for your project
    my @perl = qw(LWP::Simple XML::LibXML MIME::Lite DBI DateTime Config::Tiny Proc::ProcessTable);
    
    chomp(my $curl = `which curl`);
    
    if(!$curl){ system('apt-get install curl -y > /dev/null'); }
    
    chomp(my $cpanm = system('/bin/bash', '-c', 'which cpanm &>/dev/null'));
    
    #installs cpanm if missing
    if($cpanm){ system('curl -s -L http://cpanmin.us | perl - --sudo App::cpanminus'); }
    
    #loops through required modules and installs them if missing
    foreach my $x (@perl){
        eval "use $x";
        if($@){
            system("cpanm $x");
            eval "use $x";
        }
    }
    

    This works well for me, maybe there is something here you can use.

提交回复
热议问题