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

前端 未结 5 529
终归单人心
终归单人心 2020-12-23 10:13

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 mak

相关标签:
5条回答
  • 2020-12-23 10:35

    For quick-and-dirty, infrequent use, the %INC is the best way to go. If you have to do this with continuous integration testing or something more robust, there are some other tools to help.

    Steffen already mentioned the Module::ScanDeps.

    The code in Test::Prereq does this, but it has an additional layer that ensures that your Makefile.PL or Build.PL lists them as a dependency. If you make your scripts look like a normal Perl distribution, that makes it fairly easy to check for new dependencies; just run the test suite again.

    Aside from that, you might use a tool such as Module::Extract::Use, which parses the static code looking for use and require statements (although it won't find them in string evals). That gets you just the modules you told your script to load. Also, once you know which modules you loaded, you can combine that with David Cantrell's CPANdeps tool that has already created the dependency tree for most CPAN modules.

    Note that you also have to think about optional features too. Your code in this case my not have them, but sometimes you don't load a module until you need it:

    sub foo
        {
        require Bar; # don't load until we need to use it
        ....
        }
    

    If you don't exercise that feature in your trial run or test, you won't see that you need Bar for that feature. A similar problem comes up when a module loads a different set of dependency modules in a different environment (say, mod_perl or Windows, and so on).

    There's not a good, automated way of testing optional features like that so you can get their dependencies. However, I think that should be on my To Do list since it sounds like an interesting problem.

    0 讨论(0)
  • 2020-12-23 10:39

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

    0 讨论(0)
  • 2020-12-23 10:48

    Another tool in this area, which is used by Dist::Zilla and its AutoPrereqs plugin, is Perl::PrereqScanner. It installs a scan-perl-prereqs program that will use PPI and a few plugins to search for most kinds of prereq declaration, using the minimum versions you define. In general, I suggest this over scanning %INC, which can bring in bogus requirements and ignores versions.

    0 讨论(0)
  • 2020-12-23 10:50

    You could dump %INC at the end of your script. It will contain all used and required modules. But of course, this will only be helpful if you don't require modules conditionally (require Foo if $bar).

    0 讨论(0)
  • 2020-12-23 10:56

    Today I develop my Perl apps as CPAN-like distributions using Dist::Zilla that can take care of the dependencies through the AutoPrereq plugin. Another interesting piece of code in this area is carton.

    0 讨论(0)
提交回复
热议问题