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

Hello all,

Are there any call graph tools for perl scripts?

I am looking to find out callers and callees of functions in my code. I am also trying to ensure that the code that I write does not have any undefined functions. I am using 5.6.0 on Solaris.

As far as I know, undefined functions don't trigger any warnings at compile- or run-time, until there is an attempt at run-time to invoke the function.

The following code will never trigger any messages, even though there is no function called &grabels_law_violated.

|| #!/usr/bin/env perl || use 5.006; || use warnings; || use strict; || (2 == 3) ? &grabels_law_violated() : print "Whew!\n"; || __END__

Thanks in advance.

peace,
--{kr.pA}

Replies are listed 'Best First'.
Re: Any call graph tools for perl scripts?
by simonm (Vicar) on Jul 17, 2003 at 16:31 UTC
    The Devel::DProf module can produce a text outline showing which functions are called from where. dprofpp -t -p myscript.pl

    You could probably feed a parsed version of that text output into one of the Chart:: or Graph:: modules if you wanted a pretty chart...

    However, note that this only shows the actual execution path, not all of the possible options, so you'll need to re-run your script multiple times with different inputs to see the various execution trees that result. I don't believe there's any way to generate a graph showing all of the possible ways it could flow.

Re: Any call graph tools for perl scripts?
by halley (Prior) on Jul 17, 2003 at 16:35 UTC
    As you said, undefined functions don't trigger any warnings at compile- or run-time, until there is an attempt at run-time to invoke the function.

    This is a byproduct of an intentional feature: Perl is dynamic. You can define new subs even after the script has begun executing. The already-compiled code may have bytecode which calls a sub which has not yet been compiled. You can eval $script or do $script any time you like, and that new script can override or fulfill the previously mentioned set of subs. The AUTOLOAD mechanism exploits this.

    Sometimes I'd like to see a call-tree too. I'd feed trees to GraphViz to illuminate unfamiliar code.

    I think you'll have to accept one of two possible approaches: (1) generate a call-tree of only the subs defined in static script code, with some hacks to allow for AUTOLOAD examples or other detectable situations, or (2) generate the call tree from repeated investigation of the bytecode over the course of executing the script.

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

        Link gets a 404. The introductory article can now be found here.
Re: Any call graph tools for perl scripts?
by shemp (Deacon) on Jul 17, 2003 at 16:43 UTC
    if you're looking for callers, use the built-in caller() function.

    You are correct, in that there will not be an error message until the non-existant function is (attempted to be) called. This is because when the program is invoked, perl has no way to know whether or not the function will exist when it is called. Functions can be created at run time, additional code can be imported dynamically, etc. There was a thread about this the other day, but i cant locate it right now. :(