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?
See the bootstrapping technique for how to get started. You can create a directory per user/project/company and deploy to other servers, by copying the directory (as long as you are on the same operating system and perl version). cpanm from App::cpanminus is a script to get, unpack, build and install modules from CPAN.
To install Perl modules using CPAN, you need to use the cpan command-line utility. You can either run cpan with arguments from the command-line interface, for example, to install a module (e.g Geo::IP) use the -i flag as shown.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With