Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Source-Code Analysis?

by monsieur_champs (Curate)
on May 17, 2004 at 20:29 UTC ( [id://354100]=perlquestion: print w/replies, xml ) Need Help??

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

Oh Wise Fellows-in-Perl
Today I've spent my day thinking about an old and dull problem I have at work: how to analyse and solve dependency problems with legacy source code? I mean, I need to remove old, not used anymore, libraries and references to templates, config options and the like from my source code. Its not huge at all, but I don't want to search for every reference by hand.

My question is: is there any (?:tool|library|program) that could be useful for accomplish this task? I don't even know how to name this job, so I just can't find anything usefull at google or at Seekers of Perl Wisdom.

Please, no ready answers, I need to figure things out by myself, and maybe roll my own solution out-of-the-box. Pointers, libraries, example source code and ideas are all welcome.

Ah! This is something I've coded (sorry, Bourne Again Shell, not Perl) as a proof-of-concept...:

#!/bin/bash function search-for-dependants () { pushd "$1" || exit 1 for template in `find . -type f -print | egrep -v CVS | sed 's/^\.\/ +//;'` do remove=$template if echo $template | egrep -q '\.pm$'; then template=$(echo $template | sed 's/^.*\/lib\///;s/\//::/g;s/\.pm +$//;') fi echo "========================================" echo $template echo "========================================" find ~/src/netfax \ -type f \( -exec egrep -q $template "{}" \; -a -print \) | egrep -v CVS | egrep -v $remove | sort -u echo -e "========================================\n\n" done | sed 's/^\/home\/lcampos\/src\/netfax\///;' | tee ~/tmp/files popd } search-for-dependants ~/src/netfax/components search-for-dependants ~/src/netfax/lib

Update: minor spelling error fixes.

Replies are listed 'Best First'.
Re: Source-Code Analysis?
by fglock (Vicar) on May 17, 2004 at 20:42 UTC
Re: Source-Code Analysis?
by Rex(Wrecks) (Curate) on May 17, 2004 at 21:04 UTC
    Ok, this is no help, but I ++ your node for one reason:

    "Please, no ready answers, I need to figure things out by myself, and maybe roll my own solution out-of-the-box"

    this is an attitude which I believe embraces the spirit of the Monastary. Some will reply with the reinventing the wheel arguement, but I still think learning for yourself how things work before going to ready solutions is a good thing to do.


    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
      Module::ScanDeps is one wheel that comes to mind. When reinventing the wheel, it's best to seek out existing wheels for ideas.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

      *Agrees Rex, Just enough of a pointer, or it spoils the fun, healthy attitude. @Mnsr. Champs, <spelling nazi> Analize. No No No! Analyse. Analize is _something_quite_different_altogether_. </spelling nazi>
Re: Source-Code Analysis?
by fletcher_the_dog (Friar) on May 17, 2004 at 22:05 UTC
    Below is an example of little module I use for checking what modules are loaded at compile time by a script or module. It will not show you what modules are loaded at run time using 'require' though. You probably modify it to see if the modules being loaded are old ones you want to get rid of.
    package Dependancies; =head1 NAME Dependancies =head1 SYNOPSIS perl -MDependancies -c <PERL FILE> =head1 DESCRIPTION This module is used to see what the dependancies a script or module ha +s at COMPILE time. =cut CHECK{ while (my($file,$location)=each(%INC)) { print "$file => $location\n"; } }; 1;

      Not to toot my own horn too much, but I have a module on CPAN that does this: Devel::Modlist.

      It can also reduce the number of listings down, using the CPAN info, to only reference one module per distribution (much like the "r" command in the CPAN shell).

      --rjray

        Funny you should post this - I just used your module a couple days ago to find module dependencies so I can install a webapp in a standalone situation. And it did a great job in a very quick & easy way. :-)

        The downside: it missed one module. I'm using Class::DBI, and it missed the DBIx::ContextualFetch module used by Ima::DBI. But that seems forgivable since the only mention of DBIx::CF in Ima::DBI is in the RootClass attribute passed to DBI->connect. No use statement at all. Hmm, maybe I should submit a patch...

Re: Source-Code Analysis?
by graff (Chancellor) on May 18, 2004 at 04:35 UTC
Re: Source-Code Analysis?
by periapt (Hermit) on May 18, 2004 at 12:33 UTC
    I think you are probably on the right track. I just finished a similar code clean project on one part of my code base. To start, I just wanted to narrow my search for each variable to the files it appeared in. The below code, for windows, outputs into a seperate text file for each variable a list of all files containing the reference. The text file is empty if no file contains the reference. It was actually quite simple from that point to review the specific files for redundancy etc from the list.
    echo. echo Building file list ... dir K:\WorkingData /S/b/A-D | grep ".plx" - | gawk "BEGIN{ DQ = 34 }{ +if(/Copy /) ; else printf \"%%c%%s%%c\n\", DQ, $0, DQ }" >> plxlst01_ +%1.txt dir J:\ /S/b/A-D | grep ".plx" - | gawk "{ if(/RECYCLER/) ; else print +f \"%%c%%s%%c\n\", DQ, $0, DQ }" >> plxlst01_%1.txt echo. REM REM The following command line searches (grep) each file in plxlst01_% +1.txt for the term in parentheses and outputs the file names. REM The resultant file names are piped to gawk to append a CRLF pair t +o file same. This list is then sorted (sort) and duplicate REM file names are removed (sed) and the resultant list is output to t +he file specified. Note: For some reason, sort -u and uniq don't REM work properly in Windows REM echo Searching for ChkMsgType() ... for /F %%f IN (plxlst01_%1.txt) DO grep -l "ChkMsgType" %%f | gawk "{ +print $0 }" | J:\usr\bin\sort -u >> ChkMsgType_%1.txt

    PJ
    We are drowning in information and starving for knowledge - Rutherford D. Rogers
    What good is knowledge if you have to pull teeth to get it - anonymous

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found