Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

How can I examine a Perl file (utility or module), and get list of all of the subroutines used in it, and where they come from?

Our utilities use a lot of in-house modules, and it's routine for developers to simply paste in a long list of 'use modX;' statements without checking whether or not the module is actually needed. This is the case not only for utilities, but for the modules themselves. Or, if a subroutine is no longer used in file, the use statement that pulled it in isn't updated or removed. The result is that we've accumulated a lot of unused functionality, and can't identify what we don't need. I'd like to figure out which subroutines in an imported module are actually used, so we can clean up or consolidate our libraries. I could also build a map of our entire repository of utilities and modules and give awards to those subroutines/modules that are frequently referenced ... :)

A couple of considerations:

  1. I can't execute the utility/module I'm analyzing. (perl -c is ok, tho' -- see approach 2 below) This constraint eliminated the use of some promising CPAN modules.
  2. I need to recognize object methods as subroutine calls. (Happily, we don't use class inheritance.)

There are two approaches I've considered:

  1. Write a parser to make a list of the subroutines defined or called within a file (utility or module). I started with this, but it quickly got messy, since subroutine calls may or may not use parentheses, might be nested in other calls, etc.
  2. Let Perl do the parsing, and examine the symbol table for CODE symbols. This is where perl -c is handy for compiling everything, then executing BEGIN blocks, but not executing anything else:
        cat <utility|module> begin.pl | perl -c 
        
    where begin.pl is something like:
        BEGIN {
             print "$_=$main::{$_}\n" foreach grep(*{$main::{$_}}{CODE}, sort keys %main::);
        }
        

    This does list all of the code symbols defined in the utility ... and, unfortunately, those defined in any imported modules, all listed as if belonging to main::. I'm not sure how to distinguish the imported symbols from those defined in the target file.
    Note that begin.pl is actually more complicated than the above because I want to harvest module locations from %INC, and potentially look at the module symbol tables as well.

Any ideas?


In reply to Identify subs and their source by stmullins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (10)
    As of 2014-11-27 16:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (186 votes), past polls