Sergej Andrejev
Sergej Andrejev

Reputation: 9413

How to include a CPAN dependency inside Perl application?

I want to distribute my Perl application which have one CPAN dependency. Can I include a check for this dependency when somebody starts the application. Through command line argument or inside perl directly?

Upvotes: 0

Views: 190

Answers (2)

David W.
David W.

Reputation: 107090

Is this a pure Perl module with no other dependencies?

I'd just package it with my code. Heck, you can even just append it to your file, if you prefer.

If this is a bit more complex, that is, there are a dozen of required modules that must be installed, and there is some compilation that is required, you'll have to use CPAN to download it. There is a CPAN::AutoINC which is suppose to download and install any modules you need from CPAN when the module is required and not in the @INC path.

However, my experience has been that you end up with a mess 'o worms. A user might start to run your program thinking it'll run for a a minute only to discover that they're spending 20 minutes downloading, compiling, and testing prerequisite modules when they really don't have time.

It's better just to fail, and give a good explanation of what is required. The user might prefer to run cpan as root, so it's available for everyone on the machine. Or, maybe they need to ask a system admin to do it for them.

I've found I can do something like this:

our $missingModuleFlag;

BEGIN {
  eval { require My::Mod; };
  our $missingModuleFlag = $@ if ($@);
}

[...]

our $missingModuleFlag;   #Package Variable -- Value is from above
if ($missingModuleFlag) {
   die <<EOM;
ERROR: You are missing module "My::Mod" which is required for
    this program. Please use "cpan" to download this module
    and install it on this server. If you have no idea what
    I am talking about, see http://www.cpan.org/modules/INSTALL.html.
    If that doesn't make any sense to you, then ask a system administrator.
EOM
}

It explains what is the issue, and what needs to be done and gives the user a choice to either go ahead with the install, or ask someone else to do it for them.

Upvotes: 3

Chas. Owens
Chas. Owens

Reputation: 64939

Besides the multiple ways discussed in this question, you might also consider bundling the prerequisite module with your code. There are several options open to you PAR, PAR::Packer, and others.

Upvotes: 5

Related Questions