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

前端 未结 5 553
终归单人心
终归单人心 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.

提交回复
热议问题