Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 958
zoul Avatar asked Dec 11 '08 10:12

zoul


People also ask

How do I see what modules are installed in Perl?

Check installed perl modules via terminal Available commands are: l - List all installed modules m <module> - Select a module q - Quit the program cmd? Then type l to list all the installed modules, you can also use command m <module> to select the module and get its information. After finish, just type q to quit.

What are the packages and modules in Perl?

A package is a collection or code group in Perl. Meanwhile, a module in Perl is a package defined in a file with the name “package” and the the extension . pm. We can only define the package once in a program; whereas, more than one different module can have the same name in a single code file.

CAN modules have dependencies?

The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well.

What is package in Perl script?

A Perl package is a collection of code which resides in its own namespace. Perl module is a package defined in a file having the same name as that of the package and having extension . pm. Two different modules may contain a variable or a function of the same name.


1 Answers

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).

like image 189
tsee Avatar answered Sep 23 '22 02:09

tsee