Perl 脚本可以安装自己的 CPAN 依赖项吗? [英] Can a Perl script install its own CPAN dependencies?

查看:29
本文介绍了Perl 脚本可以安装自己的 CPAN 依赖项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Perl 脚本,它具有 CPAN 中存在的两个依赖项.我想做的是让脚本本身提示用户安装必要的依赖项,这样脚本才能正常运行.如果用户需要输入某种身份验证来安装依赖项,那很好:我试图避免的是以下工作流程:

I have a Perl script that has two dependencies that exist in CPAN. What I'd like to do is have the script itself prompt the user to install the necessary dependencies so the script will run properly. If the user needs to enter in some kind of authentication to install the dependencies that's fine: what I'm trying to avoid is the following workflow:

运行脚本 -> 看着它失败 -> 漫无目的地搜索 CPAN -> Lynch 脚本编写者

Run script -> Watch it fail -> Scour CPAN aimlessly -> Lynch the script writer

相反,我希望是这样的:

Instead I'm hoping for something like:

运行脚本 -> 自动下载脚本依赖项(必要时进行身份验证) -> 脚本成功 -> 给脚本编写者买杯啤酒

Run script -> Auto-download script dependencies (authenticating as necessary) -> Script succeeds -> Buy the script writer a beer

这能做到吗?

推荐答案

每个标准构建范例都有自己指定依赖项的方式.在所有这些情况下,构建过程将尝试在某些情况下自动安装您的依赖项.

Each of the standard build paradigms has their own way of specifying dependencies. In all of these cases, the build process will attempt to install your dependencies, automatically in some contexts.

ExtUtils::MakeMaker 中,您在PREREQ_PM 字段到 WriteMakefile:

# Makefile.PL for My::Module
use ExtUtils::MakeMaker;

WriteMakefile (
    NAME => 'My::Module',
    AUTHOR => ...,
    ...,
    PREREQ_PM => {
        'Some::Dependency' => 0,             # any version
        'Some::Other::Dependency' => 0.42,   # at least version 0.42
        ...
    },
    ...
 );

Module::Build 中,您将 hashref 传递给build_requires 字段:

In Module::Build, you pass a hashref to the build_requires field:

# Build.PL
use Module::Build;
...
my $builderclass = Module::Build->subclass( ... customizations ... );
my $builder = $builderclass->new(
    module_name => 'My::Module',
    ...,
    build_requires => {
        'Some::Dependency' => 0,
        'Some::Other::Dependency' => 0.42,
    },
    ...
);
$builderclass->create_build_script();

Module::Install 中,您执行一个或多个 requires 在调用命令写入 Makefile 之前的命令:

In Module::Install, you execute one or more requires commands before calling the command to write the Makefile:

# Makefile.PL
use inc::Module::Install;
...
requires 'Some::Dependency' => 0;
requires 'Some::Other::Dependency' => 0.42;
test_requires 'Test::More' => 0.89;
...
WriteAll;

这篇关于Perl 脚本可以安装自己的 CPAN 依赖项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆