Can a Perl script install its own CPAN dependencies?

后端 未结 2 1981
挽巷
挽巷 2020-12-03 03:38

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 dependenc

相关标签:
2条回答
  • 2020-12-03 03:53

    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;
    
    0 讨论(0)
  • 2020-12-03 03:54

    You can probably just execute this from inside your script.

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

    0 讨论(0)
提交回复
热议问题