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

A tool I've discovered that would be very useful to have is one that would help you explore the ways in which a subroutine could get called.

For instance, using grep it is easy to locate lines where a method or subroutine is called. However, I also want to know the name of the subroutine that call is located in. And then I want to explore how that routine can get called, and so forth.

For my needs static analysis of the program text is sufficient.

Does anyone know of a tool which can help you explore code this way?

Replies are listed 'Best First'.
Re: how can I get here?
by Anonymous Monk on Apr 30, 2009 at 21:50 UTC
      Didn't know about this - thanks for pointing it out.
Re: how can I get here?
by Corion (Patriarch) on Apr 30, 2009 at 21:41 UTC

    PPI analyzes your source code and gives you a tree representation. I guess you can then search that tree for references (by name) to your subroutine and then visit the parents of these references until you find an enclosing named subroutine.

Re: how can I get here?
by otto (Beadle) on Apr 30, 2009 at 22:29 UTC

    In someways you should first consider how much code you need to examine and your timeframe. Do you spend 5 days goofing with the computer when if you simply printed and read the code, taking notes, would provide the overview you need. And it only took 2 days.

    Look at cpan with keywords like, profile, debug, dbug, trace, etc. *grep'ing is problematic in that method calls are on objects, but you need to know what the object is to be able to know which method is meant.

    Moose provides meta info on classes etc. Code needs to be written using it...

    Debug::Trace has a unique approach.

    There have been various attempts at providing a filter for Doxygen.

Re: how can I get here?
by quinkan (Monk) on Apr 30, 2009 at 23:29 UTC
    For Perl, have a look at the OptiPerl IDE, which provides this facility (and lots of other things). Click on subroutines, find a list, select one from the list and find both source and points in code where subroutine is called. Very handy tool.
Re: how can I get here?
by Clarendon4 (Acolyte) on May 01, 2009 at 07:34 UTC

    Try peg - it's a grep-like command line tool that has an option -z to show context. It's at:

    http://cpan.mirrors.uk2.net/authors/id/A/AD/ADAVIES/peg-2.20

    You supply an expression (typically a regexp) which will be true for the context lines you want. So to recursively search files beneath your cwd you'd run:

     peg yoursubname -z "/^\s*sub /"

    Common context matching code can be stored in peg's user initialization files (see the docs for the details) and then called using a label. I use the single letters 'p' for Perl subroutine context and 'c' for C function/struct context. So in practise you would simply run:

     peg -z p yoursubname
Re: how can I get here?
by DrHyde (Prior) on May 01, 2009 at 10:05 UTC

    Some really ugly code I wrote many years ago might help. Or it might not.

    perl-dep.

Re: how can I get here?
by jvector (Friar) on May 01, 2009 at 22:32 UTC
    It's not a 'static' analysis, and therefore would p(ossi|roba)bly give you only a subset of the information you seek per trial, but Devel::NYTProf might give you a helpful view.

    "Oh.. /that/ Christmas"