Perl 脚本可以安装自己的 CPAN 依赖项吗?

发布于 2024-12-08 04:29:59 字数 326 浏览 0 评论 0原文

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

运行脚本->看着它失败->漫无目的地搜索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:

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

Can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-12-15 04:29:59

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

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 字段:

# 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,你执行一个或多个需要命令才能调用命令来写入Makefile:

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

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.

In ExtUtils::MakeMaker, you pass a hash reference in the PREREQ_PM field to 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
        ...
    },
    ...
 );

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();

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;
仙气飘飘 2024-12-15 04:29:59

您可能可以从脚本内部执行此操作。

perl -MCPAN -e '安装 MyModule::MyDepends'

You can probably just execute this from inside your script.

perl -MCPAN -e 'install MyModule::MyDepends'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文