简体   繁体   English

从CPAN自动安装缺少的模块

[英]automatically install missing modules from CPAN

If I want to distribute a Perl script, what is the best way to painlessly install any required modules that are missing on the user's system? 如果我想分发Perl脚本,那么无痛地安装用户系统中缺少的任何所需模块的最佳方法是什么? Extra credit if there is a way to even install/upgrade Perl itself if it is missing or "too old". 如果有一种方法甚至可以安装/升级Perl本身,如果它丢失或“太旧”,则可获得额外的功劳。

Auto-installing software is the best way to make both end users and sysadmins very angry with you. 自动安装软件是让最终用户和系统管理员对您非常生气的最佳方式。 Forget about this approach. 忘记这种方法。

You can simply ship all your dependencies with your application distro, the inc directory is customary. 您可以使用应用程序发行版简单地发送所有依赖项, inc目录是惯例。

Usually this ends with CPAN-like package creation. 通常,这会以类似CPAN的包创建结束。 So, when you need to install all dependencies you type make installdeps 因此,当您需要安装所有依赖项时,请键入make installdeps

See perldoc perlmodlib Also Module::Install may be useful for you and some Makefile.PL example 请参阅perldoc perlmodlib另外, Module::Install可能对您和一些Makefile.PL示例有用

Makefile.PL allows you to define deps and required perl version. Makefile.PL允许您定义deps和所需的perl版本。 Also you may add 你也可以补充一下

use 5.010;

To your script in order to require minimal version of perl to run. 到您的脚本,以便要求运行最低版本的perl。 See perldoc -f use for details. 有关详细信息,请参阅perldoc -f use

Why not use pp (PAR Packager) that creates an executable. 为什么不使用创建可执行文件的pp(PAR Packager)。 No need for Perl or anything on the target machine. 不需要Perl或目标机器上的任何东西。

If you look at cpanminus, this you can install by simply executing one file: 如果你看一下cpanminus,你可以通过简单地执行一个文件来安装:

curl -L http://cpanmin.us | perl - --self-upgrade

This might be the behaviour you're looking for; 这可能是你正在寻找的行为; it's done with App::Fatpacker. 它是用App :: Fatpacker完成的。 Check it out: 看看这个:

https://metacpan.org/module/App::FatPacker https://metacpan.org/module/App::FatPacker

A semi-automated script taken from here , which should work for people with middle level bash skills and almost 0 level perl skills : 这里采取的半自动脚本,适用于具有中级bash技能和近乎0级perl技能的人:

#!/usr/bin/env perl
use strict ; use warnings ;
use 5.10.0 ;
use ExtUtils::Installed;

    #  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_checker.pl"
    #  export ret=$?
    #  test $ret -ne 0 && doExit 1 "[FATAL] perl modules not found!!!"

    my $PrintOkCheck = 1 ;

    # 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
            print "printing all installed modules :" ;
            my $c = 9 ;
            for ( my $i=0;$i<=$c;$i++){
                    print ( ( $c-$i) . '.') ;
                    sleep 1 ;
            }
            print "\n" ;
            doListAllInstalledModules();
            print "\n" ;
    }

    exit(0);

    sub doListAllInstalledModules {
            my $instmod = ExtUtils::Installed->new();
             foreach my $module ($instmod->modules()) {
                    my $version = $instmod->version($module) || "???";
                     print "found module:$module -- v$version\n";
                    }

    }
    #eof sub

    sub doCheckRequiredModules {

            my @modules = qw(
                    YAML::Any
                    Test::More
                    Spreadsheet::XLSX
                    Test::Deep
                    File::Copy::Recursive
                    IO::HTML
                    Test::More
                    Filter::Util::Call
                    Algorithm::Diff
                    Text::Diff
                    Test::Base
                    Test::CPAN::Meta::YAML
                    Test::YAML::Valid
                    Test::YAML::Meta
                    Test::YAML
                    Data::Printer
                    ExtUtils::Installed
                    Sub::StrictDecl
                    Spreadsheet::WriteExcel
                    Mojolicious::Plugin::RenderFile
                    JSON
                    Carp::Always
                    Mojolicious::Plugin::PDFRenderer
     Redis::Client
                    );

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

                            #flush the screen
                            print "\033[2J";
                            print "\033[0;0H";

                            my $msg = "\n\n\n [FATAL] did not found the following prerequisite perl module: $_ \n\n" ;
                            $msg .= "\n # == START copy paste == " ;
                            $msg .= "\n#you must install it otherwise the application will not work" ;
                            $msg .= "\n#the module could be installef by running the following commands:" ;
                            # 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# as a start configure the cpan to install dependancies first \n" ;
                            $msg .= "\n" . 'perl -MCPAN -e \'my $c = "CPAN::HandleConfig"; $c->load(doit => 1, autoconfig => 1); $c->edit(prerequisites_policy => "follow"); $c->edit(build_requires_install_policy => "yes"); $c->commit\'' . "\n" ;
                            $msg .= "\n#than install the $_ module by running: \n" ;
                            $msg .= "\nsudo perl -MCPAN -e 'install $_'\n\n\n" ;
                            $msg .= "\n # == STOP  copy paste == \n\n\n" ;
                            $msg .= "\n # == START copy paste == " ;
                            $msg .= "\n# if you seem to be stuck in circular reference kind of loop try even :\n" ;
                            $msg .= "\nsudo perl -MCPAN -e 'CPAN::Shell->force(qw( install $_));'\n" ;
                            $msg .= "\n # == STOP  copy paste == " ;
                            $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 : $_" if $PrintOkCheck == 1 ;
                     }
            }
            #eof foreach module

            return ( 0 , "all required modules found" ) ;
    }
    #eof sub
    # ??!!
    #perl -MCPAN -e 'install Module::Signature'
    # the following modules have been or might be part of the installable modules
    #PDF::WebKit
    #HTML::TreeBuilder
    #HTML::TreeBuilder::XPath
    #HTML::TableExtract
    #HTML::ElementTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM