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

Hi. I am trying to work out what modules/routines our CGI scripts are dependent on. For example, if I have forum.cgi which use()s five modules, what functions am I using in those modules?

I can get a list of routines from the modules by looking for /\s+sub\s/, but is there a module or anything out there I can use before diving in myself (looking for my $blah = new Blah followed by $blah->routine, "&Blah::routine", "&routine" or just "routine")?

Devel::Modlist just gives me the files rather then the specific subroutines within them.

Thanks for your help!

Replies are listed 'Best First'.
Re: Dependencies
by davorg (Chancellor) on Dec 07, 2000 at 17:47 UTC

    This is a seriously difficult job that you've given yourself. You're basically planning to parse your Perl code, and as we all know "only perl can parse Perl".

    One potentially bonkers way to do it would be to do something crazy with AUTOLOAD, as suggested by Kurt Starsinic in this article from LinuxMonth.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      That works to an extent - only it only reports calls done in that pass through the code, and doesn't support object calls. Hmmm.

      If I'm just going to report calls made during a call, I could always do global changes from

      sub foo {
      to
      sub foo { 
          print STDERR "foo"
      for all the modules. That would then dump out a list of used routines to STDERR (ie the web logs) as they were used. Hmmm.
Re: Dependencies
by t0mas (Priest) on Dec 07, 2000 at 18:09 UTC
    You could create a LineInfo file during a run of the program by setting the environment variable PERLDB_OPTS to NonStop=1 LineInfo=db.out AutoTrace and run the file with perl -d flag, and then parse the db.out file.
    The file will contain all the code you've "touched" plus a line that tells you where perl found it, like
    strict::bits(C:/Perl/lib/strict.pm:95): 95: foreach my $s (@_){ $bits |= $bitmask{$s} || 0; };
    You will know which functions you have used during that run, which may or may not be all the functions that your code can use.

    /brother t0mas
Re: Dependencies
by chromatic (Archbishop) on Dec 08, 2000 at 03:05 UTC
    If you're not adverse to a little sideways work, run Devel::DProf on your code as the debugger, then use dprofpp -T to print an execution graph.

    With a bit of massaging (uniq, or a five line Perl program), you can get a list of all of the unique functions called. They're reported in the form

    main::BEGIN lib::BEGIN vars::import
    Your command line would look be perl -d:DProf forum.cgi.
Re: Dependencies
by sarabob (Novice) on Dec 08, 2000 at 18:54 UTC
    Hmm. So, basically there isn't a way to find all dependencies within the code - only to find out what routines get called in a single pass through the script.

    Oh well, I'll have to code up some custom scripts to test everything, and monitor calls as they happen.

    Thanks again for all your help.