Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Check a script's module dependencies

by hans_moleman (Sexton)
on Oct 06, 2002 at 03:09 UTC ( #203128=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info doug at nextdimensioninc dot com
Description:

I put together this code to help migrating Perl scripts from one environment to another. The scripts are mostly CGIs, and I found it annoying and time consuming to either

  1. run the scripts to see what breaks
  2. or
  3. read through each script manually
. Using this little script you can check whether all needed modules are available.

Comments and suggestions are appreciated as always...

Update: Changed the logic in my if statement to reflect podmaster's CB suggestion...

#!/usr/bin/perl
use warnings;
use strict;

my $filename=shift || &help; # command line argument is perl script to
+ evaluate
my @modules;  # array of 'use' statements from code we are checking

open (IN,$filename) or die "couldn't open $filename for processing: $!
+\n";

while (<IN>)
{
        chomp;
        if ((/^use/) and not (/strict/ || /warnings/))
        {
                push @modules,$_;
        }
}
close IN;
for my $code (@modules)
{
        my (undef,$library)=split(/ /,$code);   # get the module name
        $library=~s/;//;                        # clean up the name
        eval $code;
        if ($@)
        {
                warn "couldn't load $library: $@","\n";
        }
        else
        {
                print "$library looks ok\n";
        };
}
sub help
{
        print <<"END";

check_perl_modules.pl

This script finds all the "use" statements loading modules in the targ
+et perl
file (specified as a command line argument) and attempts to load them.
If there are problems loading the module, the error mesage returned is
+ printed.

END
        exit;
}

Comment on Check a script's module dependencies
Download Code
Re: Check a script's module dependencies
by Aristotle (Chancellor) on Oct 06, 2002 at 11:51 UTC

    Good idea, nice effort, and probably the same route I'd take if I hadn't picked up various details during my Perl efforts. However, there are better ways to do this. Regexing sources is easy to break and your script cannot conveniently report recursive dependencies where a module a script depends on relies on other modules itself.

    I was going to post a snippet here, but thought it could be useful to more people, so I decided to post it in a more prominent spot. Check robustly list any Perl code's module dependencies for the result. Cheers :-)

    Makeshifts last the longest.

      Nicely done!

      Yes, using internal Perl tools seems like a more robust approach than parsing source code. Frankly, I've got some reading to do to understand how your code does what it does...

        It's not very complicated once you know how loading modules works: basically, the @INC array contains all the paths to any directories where perl should look for modules.

        Usually, these are only strings, but you can also put a reference to a subroutine in there; when perl is searching for modules, it will call that routine if it hasn't found the module by one of the paths that appear before it in the array. Along with various parameters passed to such a routine is the module's path in the second parameter which is in this case copied to $_ in order to massage it to my liking.

        Because this is a module, the code in its main body gets executed as soon as the module is loaded, at compile time, and therefor gets to modify the @INC array before anything else has happened.

        Makeshifts last the longest.

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://203128]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-07-10 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (216 votes), past polls