Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Identifying Library Usage

by Itatsumaki (Friar)
on May 07, 2004 at 14:32 UTC ( #351467=perlquestion: print w/replies, xml ) Need Help??

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

Howdy Monks,

I am maintaining a CGI-based website of about 100 different scripts. It is underpinned by a handful of library modules which are not particularly organized and seem to contain a lot of deprecated functions.

Is there some automated way of checking the entire set of 100 scripts and identifying which parts of the modules are no longer accessed anywhere?

My current thinking had been to add code to every sub in every module to log who had called them, but I worry that our test-set isn't comprehensive enough to catch everything on a site (e.g. ~300 tests for 100 scripts).

Any other ideas on how to do this?


Replies are listed 'Best First'.
Re: Identifying Library Usage
by ysth (Canon) on May 07, 2004 at 15:40 UTC
    Have a look at B::Xref (used by running perl -MO=Xref for each script.). It outputs information about declarations and uses of all types of variables and subroutines.
Re: Identifying Library Usage
by Ven'Tatsu (Deacon) on May 07, 2004 at 15:01 UTC
    There is probalby a way to read through the scripts and indentify all the calls the make, but with all the diffrent ways to call subs in perl I can't imagin that it would be easy.
    You might consider perl -d:DProf, as you noted it might not catch all the subs if you don't execute every code path, but it would probably be easyer than adding loging statments to each library function.
Re: Identifying Library Usage
by eXile (Priest) on May 07, 2004 at 15:27 UTC
    Maybe Devel::SmallProf could be of use here. You can profile your code with it, and it will write how many times a line was executed in a format like this:
    <num> <time> <ctime> <line>:<text>
Re: Identifying Library Usage
by halley (Prior) on May 07, 2004 at 15:55 UTC
    I also described this need a while back. I made a pragmatic module called use logged; and I thought I posted it here but can't find a link.

    It's very similar to my module called use deprecated; and you might look at that one for some ideas. Just log a caller the first time a script loads your module, and you'll get a good sense of the user community on your network.

    Of course, the best logging approach will vary on your network setup. This was raised as the worry of spyware here last week, but within a corporate network or filesystem, I think it's fair game for the librarian to track usage occasionally.

    [ e d @ h a l l e y . c c ]

Re: Identifying Library Usage
by ColonelPanic (Friar) on May 07, 2004 at 14:54 UTC
    I suppose I would start with simply searching the code for the subroutine names, but what if you had any of the following:
    #1 print "Enter some Perl code: "; eval ($this_is_a_great_design = <STDIN>); #2 my $result = &{ $control_db->{"action"} }; #3 my @foo_functions = map (\&("foo_".$_)) @bar;
    Well, the first one is ridiculous in a CGI script, but what about the others? Well, I guess you could turn on
    use strict qw/refs/;
    and if this didn't break the existing code, I think you would be guaranteed to find the name of every sub that is used in the code somewhere.

    When's the last time you used duct tape on a duct? --Larry Wall
Re: Identifying Library Usage
by BUU (Prior) on May 07, 2004 at 21:42 UTC
    You might want to try Devel::Cover, and try to set it up so it runs for every script, then after a week or whatever, you should have plenty of information.
Re: Identifying Library Usage
by graff (Chancellor) on May 11, 2004 at 03:43 UTC
    I wrote a utility that might do something along the lines of what you want, and posted it here: Tabulate sub defs, sub calls in Perl code -- given a list of perl source code files, it will try to generate a human-readable listing of the subroutine calls and definitions found in each file. HTH.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://351467]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2020-05-31 13:31 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (173 votes). Check out past polls.