0

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?

matthias krull
  • 4,389
  • 3
  • 34
  • 54
Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108
  • http://stackoverflow.com/questions/1094125/try-to-use-module-in-perl-and-print-message-if-module-not-available –  Jul 26 '11 at 21:21

2 Answers2

5

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.

Community
  • 1
  • 1
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
3

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.

David W.
  • 105,218
  • 39
  • 216
  • 337