Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Check script's dependencies

by alexbio (Monk)
on May 25, 2010 at 17:17 UTC ( #841605=CUFP: print w/replies, xml ) Need Help??

Update: I've written Module::CheckDeps and checkdeps, so you can use them instead of this script.

Hi everybody. I needed a simple way to check script's dependencies.

Searching on PerlMonks I found two scripts (Check a script's module dependencies and robustly list any Perl code's module dependencies) but I think a better way to achieve that would be using the Module::ExtractUse module, so I wrote my version.

#!/usr/bin/perl # Usage: SCRIPT # Check Perl script's dependencies use Module::ExtractUse; use strict; my $script = $ARGV[0] or die "Usage: $0 SCRIPT\n"; open SCRIPT, $script or die "$!\n"; my $code = join "", <SCRIPT>; close SCRIPT; my $p = Module::ExtractUse -> new; $p -> extract_use(\$code); my @used = $p -> array; foreach my $module(@used) { eval "use $module"; next if !$@; print $module."\n"; }

It simply prints every modules not yet installed (one per line) on which the script depends, ready to feed the cpan script.

For example:

$ cpan `perl`

Any hints about the code?

Alex's Log -

Replies are listed 'Best First'.
Re: Check script's dependencies
by runrig (Abbot) on May 25, 2010 at 17:51 UTC

      I tried the script, but it takes a lot of time (also with the '-R' option active) and its output is not what I needed.

      Also the module is quite slow, so I found Module::ExtractUse more appropriate because of its "lightness".

      Alex's Log -
Re: Check script's dependencies
by Khen1950fx (Canon) on May 27, 2010 at 06:26 UTC
    Interesting, but it doesn't work for me. I added Data::Dumper and took out the foreach and replaced it with print Dumper($p->used); Now it works for me.
    #!/usr/bin/perl # Usage: SCRIPT # Check Perl script's dependencies use strict; use warnings; use Module::ExtractUse; use YAML; use YAML::Dumper; my $script = $ARGV[0] or die "Usage: $0 SCRIPT\n"; open SCRIPT, $script or die "$!\n"; my $code = join "", <SCRIPT>; close SCRIPT; my $p = Module::ExtractUse->new; $p->extract_use(\$code); my $dumper = YAML::Dumper->new; $dumper->indent_width(4); my @used = $p->array; print $dumper->dump($p->used);

      Thank you for your help, but I think your code lost the main features I needed: the test on every module to check if it is installed and the "formatted output".

      I really don't understand what's wrong in my code, I tested it on both Linux (perl 5.10.1) and Windows XP (the latest strawberry perl). Has anyone more experienced than me any ideas?

      Alex's Log -
Re: Check script's dependencies
by Anonymous Monk on Jun 15, 2010 at 18:02 UTC
    Might want to call it

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://841605]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-01-24 04:53 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (255 votes). Check out past polls.