我有一个Perl脚本,在CPAN中有两个依赖关系。我想做的是让脚本本身提示用户安装必要的依赖项,以便脚本正常运行。如果用户需要进入某种类型的身份验证来安装这些依赖关系:我正在尝试避免的是以下工作流程:
Run script -> Watch it fail -> Scour CPAN aimlessly -> Lynch the script writer
相反,我希望像:
Run script -> Auto-download script dependencies (authenticating as necessary) -> Script succeeds -> Buy the script writer a beer
这可以做吗
解决方法
每个标准构建范例都有自己的方法来指定依赖关系。在所有这些情况下,构建过程将尝试在某些上下文中自动安装依赖项。
在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之前执行一个或多个require命令:
# Makefile.PL use inc::Module::Install; ... requires 'Some::Dependency' => 0; requires 'Some::Other::Dependency' => 0.42; test_requires 'Test::More' => 0.89; ... WriteAll;