【问题标题】:automatically install missing modules from CPAN从 CPAN 自动安装缺少的模块
【发布时间】:2012-01-01 06:26:42
【问题描述】:

如果我想分发 Perl 脚本,那么轻松安装用户系统上缺少的任何必需模块的最佳方法是什么?如果 Perl 丢失或“太旧”,如果有办法安装/升级 Perl 本身,则可以额外加分。

【问题讨论】:

    标签: perl deployment cpan


    【解决方案1】:

    这通常以类似 CPAN 的包创建结束。 因此,当您需要安装所有依赖项时,请键入 make installdeps

    perldoc perlmodlib Module::Install 也可能对您有用,还有一些 Makefile.PL 示例

    Makefile.PL 允许您定义 deps 和所需的 perl 版本。 你也可以添加

    use 5.010;
    

    到您的脚本中,以便需要最低版本的 perl 才能运行。详情请见perldoc -f use

    【讨论】:

    • 这不是我心目中的“无痛”!我想分发一个 perl 脚本文件,允许用户只运行该脚本,并且脚本本身会安装任何丢失(但需要)的模块,而用户无需执行任何单独的操作。
    • @JoelFan,如果您可以让它在您的平台上运行,请参阅 Bill Ruppert 对 more “无痛”的回答。
    • 忘记痛苦......这是正确的答案,将脚本作为 perl 模块分发。知道如何管理 perl 发行版的用户可以轻松使用标准工具。对于知识较少的用户,请提供一个脚本来调用您的包的安装。总之,您的问题确实与 Perl 相关......我使用它多年并且遇到了同样的问题。我曾尝试使用 PAR 模块,但最终因为所涉及的努力而放弃了。要么接受 Perl,要么放弃它。我发现中间没有:-(
    【解决方案2】:

    为什么不使用创建可执行文件的 pp (PAR Packager)。目标机器上不需要 Perl 或任何东西。

    【讨论】:

    • PAR 非常酷。以我的经验,它从来没有像 TCL 的 starkits 和 starpacks 那样好,这非常令人失望。我了解到最好花一些时间学习如何将 perl 打包为一个模块。诚然笨重,但更容易跨多个平台提供支持。
    【解决方案3】:

    自动安装软件是让最终用户和系统管理员对您非常生气的最佳方式。忘记这种方法吧。

    您可以简单地将所有依赖项与您的应用程序发行版一起发布,inc 目录是习惯性的。

    【讨论】:

    • 虽然我同意这种观点,但我认为答案需要更多工作。您是提倡运送其他 perl 软件包还是仅仅运送他们的代码副本?这里的问题很常见。在所有语言中跟踪一个人使用的第 3 方模块是很痛苦的。作为一名分发代码的专业开发人员,我现在判断一门语言的包管理质量。 CPAN 曾经是开发人员已知的最大的软件存储库。但是...它为其他技术提供了经验教训,这些技术创建软件模块的方式不那么神秘。
    • 我都不提倡。我其实很喜欢标准工具链,yko 列出了细节。我想从表面上接受这个问题,即使它被误导了。
    【解决方案4】:

    如果您查看 cpanminus,您只需执行一个文件即可安装:

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

    这可能是您正在寻找的行为;它是通过 App::Fatpacker 完成的。看看吧:

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

    【讨论】:

      【解决方案5】:

      来自here 的半自动化脚本,它应该适用于具有中级 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
      

      【讨论】:

        猜你喜欢
        • 2020-06-03
        • 2023-03-04
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-19
        相关资源
        最近更新 更多