开发者

How to include a CPAN dependency inside Perl application?

开发者 https://www.devze.com 2023-03-23 06:57 出处:网络
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.开发者_StackOverflow Through command line argume

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.开发者_StackOverflow Through command line argument or inside perl directly?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号