How do I find the module dependencies of my Perl script?

zoul picture zoul · Dec 11, 2008 · Viewed 24.9k times · Source

I want another developer to run a Perl script I have written. The script uses many CPAN modules that have to be installed before the script can be run. Is it possible to make the script (or the perl binary) to dump a list of all the missing modules? Perl prints out the missing modules’ names when I attempt to run the script, but this is verbose and does not list all the missing modules at once. I’d like to do something like:

$ cpan -i `said-script --list-deps`

Or even:

$ list-deps said-script > required-modules # on my machine
$ cpan -i `cat required-modules` # on his machine

Is there a simple way to do it? This is not a show stopper, but I would like to make the other developer’s life easier. (The required modules are sprinkled across several files, so that it’s not easy for me to make the list by hand without missing anything. I know about PAR, but it seems a bit too complicated for what I want.)


Update: Thanks, Manni, that will do. I did not know about %INC, I only knew about @INC. I settled with something like this:

print join("\n", map { s|/|::|g; s|\.pm$||; $_ } keys %INC);

Which prints out:

Moose::Meta::TypeConstraint::Registry
Moose::Meta::Role::Application::ToClass
Class::C3
List::Util
Imager::Color
…

Looks like this will work.

Answer

tsee picture tsee · Dec 11, 2008

Check out Module::ScanDeps and the "scandeps.pl" utility that comes with it. It can do a static (and recursive) analysis of your code for dependencies as well as the %INC dump either after compiling or running the program.

Please note that the static source scanning always errs on the side of including too many dependencies. (It is the dependency scanner used by PAR and aims at being easiest on the end-user.)

Finally, you could choose to distribute your script as a CPAN distribution. That sounds much more complicated than it really is. You can use something like Module::Starter to set up a basic skeleton of a tentative App::YourScript distribution. Put your script in the bin/ subdirectory and edit the Makefile.PL to reference all of your direct dependencies. Then, for distribution you do:

  1. perl Makefile.PL
  2. make
  3. make dist

The last step generates a nice App-YourScript-VERSION.tar.gz Now, when the client wants to install all dependencies, he does the following:

  1. Set up the CPAN client correctly. Simply run it and answer the questions. But you're requiring that already anyway.
  2. "tar -xz App-YourScript-VERSION.tar.gz && cd App-YourScript-VERSION"
  3. Run "cpan ."

The CPAN client will now install all direct dependencies and the dependencies of those distributions automatically. Depending on how you set it up, it will either follow the prerequisites recursively automatically or prompt with a y/n each time.

As an example of this, you might check out a few of the App::* distributions on CPAN. I would think App::Ack is a good example. Maybe one of the App::* distributions from my CPAN directory (SMUELLER).