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

Fellow Monks,

A huge bunch of old perl scripts has just arrived on my PC. My Boss wants me to analyse them. Obviously some will be out of date, and some are just not used or called anymore. I don't suppose there is a perl script which can trace through the flow of scripts, reporting on which subroutines are called or not? some kind of user-friendly perl script parser as such.... it would certainly help to save me hours (even days) of tracing through this stuff by hand, and figuring out what's going on.
P.S. no documentation came with this stuff, plus very few worthwhile comments.
P.P.S. the guy who originally did this stuff has left the company, for some time now

Thanks in advance
Jonathan.

Replies are listed 'Best First'.
Re: parsing a load of perl scripts
by Corion (Patriarch) on Oct 13, 2004 at 13:11 UTC

    If all you want, for a start, is a map of which script is called when, and all the scripts are Perl 5 scripts (and not Perl 4!!), then I would write some logging routines and load them into every Perl script via the PERL5OPT=-MMy::Logging::Script environment variable.

    Depending on your specific needs, it might already be enough to do the following:

    export PERL5OPT=-e'BEGIN{local *LOG; open LOG, ">", "$ENV{HOME}/script +log.log; print LOG "$0 started at ". localtime()}'

    This requires no modification to your existing scripts, but it needs to be available in the environment. This means that the scripts that are run through the cron program need to be modified, or you have to modify the crontab calling these scripts.

    I don't know of any call graph generator for Perl, so other than weeding out the scripts that are not immediately called via the shell, this won't be of much help and you'll have to go through the scripts with a fine comb anyway.

Re: parsing a load of perl scripts
by aquarium (Curate) on Oct 13, 2004 at 13:38 UTC
    the perl debugger "perl -d progname.pl" has an option to dump call stack. Beware though, that some functions may never get called during a particular run, depending on program input. if the code in a program is long, not well documented, and code is not trusted..then you may spend more time looking at code rather than re-writing it. For any program roughly 100 lines or more, it's impossible to keep it all in your head...so when you write/rewrite long code, more than mere comments is needed as documentation. A flow chart is so helpful when reviewing even your own code that you haven't looked at for some time.
Re: parsing a load of perl scripts
by Happy-the-monk (Canon) on Oct 13, 2004 at 14:18 UTC
Re: parsing a load of perl scripts
by Fletch (Bishop) on Oct 13, 2004 at 13:39 UTC
Re: parsing a load of perl scripts
by davido (Cardinal) on Oct 13, 2004 at 15:13 UTC

    The B::Xref module does generate some useful information on subroutine and variable usage. You may even be able to parse its output looking for intersections between scripts.


    Dave

Re: parsing a load of perl scripts
by graff (Chancellor) on Oct 14, 2004 at 01:32 UTC
    If the multiple scripts are supposed to be parts of a single app, I posted a tool here: Tabulate sub defs, sub calls in Perl code, which is somewhat simple-minded but may be useful -- it won't tell you how paths are actually followed (this would normally depend on data or conditions external to the script anyway), but it could at least make it easier to navigate among the various files. If you know of one particular file that is certainly in use, you can see what other files it entails, by virtue of the subs that are called and where they are defined. HTH.