Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Finding dependancies of a script

by fletcher_the_dog (Friar)
on Jul 25, 2003 at 18:34 UTC ( [id://277934]=perlquestion: print w/replies, xml ) Need Help??

fletcher_the_dog has asked for the wisdom of the Perl Monks concerning the following question:

I have a project where I need to be able to automatically find all the dependancies of a script. I have a one liner that finds all the modules that a script depends on that looks like this:
perl -e 'do $ARGV[0]; print "$_\n" for values %INC' test.pl
The problem with this is that when I "do" the file name it executes the file, which sometimes is not what I want to happen. I have a clunky way of getting all the module dependancies without running the script that works like this :
#!/usr/bin/perl use strict; my $file=shift; local $/=undef; open IN, $file; my $text=<IN>; $text.="\n".q( CHECK{ foreach my $module (values %INC) { print $module."\n"; } } ); open JUNK,">.this_is_some_junk"; print JUNK $text; close JUNK; system "perl -c .this_is_some_junk"; unlink ".this_is_some_junk";
I think that there must be a better way. Any ideas?

Replies are listed 'Best First'.
Re: Finding dependancies of a script
by BrowserUk (Patriarch) on Jul 25, 2003 at 19:40 UTC

    Same basic idea, but saves some of the futzing.

    Update: Changed die to exit. Changed it back, as exit doesn't prevent the script from continuing passed the CHECK sub?

    package Dependancies; sub CHECK { print $_, $/ for values %INC; die; } 1;

    Then just add it to the command line with the -M switch.

    P:\test>perl58 -MDependancies p1.pl8 d:/Perl/lib/warnings/register.pm d:/Perl/lib/bytes.pm d:/Perl/lib/XSLoader.pm d:/Perl/lib/Carp.pm d:/Perl/lib/Exporter.pm d:/Perl/lib/strict.pm d:/Perl/lib/warnings.pm d:/Perl/lib/overload.pm d:/Perl/lib/Shell.pm d:/Perl/lib/Data/Dumper.pm Dependancies.pm Died at Dependancies.pm line 5. CHECK failed--call queue aborted.

    It probably needs a better name. Maybe something in the Devel::* namespace?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      First, you don't need sub there. Second, you don't need die or exit. Third, you want to run it with -c on the command line.

      I think. I didn't actually test any of that... :-)

      Update: Ok, I've tested it now. The code:

      package Dependencies; CHECK { print $_, $/ for values %INC; } 1;
      And a test run (on a test script that consisted of a single line, "use CGI;"):
      $ perl -MDependencies -c ./test.pl /usr/lib/perl5/5.8.0/warnings/register.pm /usr/lib/perl5/5.8.0/Carp.pm Dependencies.pm /usr/lib/perl5/5.8.0/vars.pm /usr/lib/perl5/5.8.0/strict.pm /usr/lib/perl5/5.8.0/Exporter.pm /usr/lib/perl5/5.8.0/constant.pm /usr/lib/perl5/5.8.0/warnings.pm /usr/lib/perl5/5.8.0/CGI/Util.pm /usr/lib/perl5/5.8.0/overload.pm /usr/lib/perl5/5.8.0/CGI.pm ./test.pl syntax OK

      -sauoq
      "My two cents aren't worth a dime.";
      

        you don't need sub there

        I know, but I always do with BEGIN/INIT/CHECK/END these days.

        Basically, I find the discrepancy between not having to use sub with those, but having to with TIEHASH/FETCH/STORE etc. inconsistant, and one thathad me taering my hair out for a while a couple of months back, so now I just avoid the inconsistancy. Call it an indulgance :)

        I hadn't thought of combining the module with -c, that works really nicely++.

        If you reverse the order of the switches you can almost read it as "perl check dependancies in test.pl", which works as a nice syntax reminder.

        (I was going to use that new monic word, but I think I better stay clear of that:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: Finding dependancies of a script
by sauoq (Abbot) on Jul 25, 2003 at 19:53 UTC
    I have a project where I need to be able to automatically find all the dependancies of a script.

    Just so you know, this isn't something that can be done reliably. A script could determine some dependencies programatically and load them at runtime with require, eval, or do. Your attempts and BrowserUk's solution will only tell you what has been loaded with use. If that's your whole requirement, then you're doing fine. :-)

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Finding dependancies of a script
by hossman (Prior) on Jul 25, 2003 at 20:49 UTC
Re: Finding dependancies of a script
by fletcher_the_dog (Friar) on Jul 25, 2003 at 21:12 UTC
    Here is what I think I will do. Tell me if you think this is an evil use of the command line. I made a little module named "oduleCheck", using BrowserUks idea, that looks like this:
    package oduleCheck; CHECK { print "Modules required at compile time for $0:\n"; print " $_\n" for values %INC; } 1;
    By calling it "oduleCheck" I can run it from the command line like this if I want to actually run the script:
    perl -ModuleCheck test.pl
    Or, if I want to just check the modules or I am checking a module I can run it from the command line like this:
    perl -ModuleCheck -c File/Spec.pm

      Cute. But 1) "ModuleCheck" isn't nearly as descriptive as "Dependencies" and 2) those of us who are used to using -M regularly would have to remember not to type -MModuleCheck, so I think it is a bit too cute.

      -sauoq
      "My two cents aren't worth a dime.";
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://277934]
Approved by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found