The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

CallGraph - create, navigate, and dump the call graph for a program

SYNOPSIS

    # note: you need a subclass for the language you are using
    use CallGraph::Lang::Fortran;
    my $graph = CallGraph::Lang::Fortran->new(files => [glob('*.f')]);
    print $graph->dump;

    # navigate the call graph...
    my $root = $graph->root;     # returns a CallGraph::Node object
    my (@calls) = $root->calls;
    
    # if you want to create your own language subclass:
    package CallGraph::Lang::MyLanguage;
    use base 'CallGraph';
    # must define the parse method
    sub parse {
        my ($self, $fh) = @_;
        while (<$fh>) {
            # add subroutines and calls by using
            # $self->new_sub and $self->add_call
        }
    }

DESCRIPTION

This module creates a "call graph" for a program. Please note that you need another module to actually parse your program and add the calls by using the CallGraph methods. The current distribution includes a module for parsing Fortran 77, CallGraph::Lang::Fortran.

METHODS

CallGraph->new(option => value, ...)

Create a new CallGraph object. The following options are available:

files => $file1
files => [$file1, $file2...]

Reads and parses the given files. $file1, etc. can be either filenames or filehandles.

lines => \@lines

Parses the array reference, which is expected to be an array of program lines. You can use this if you have already slurped your program into an array.

dump_options => {option => value, ...}

Pass the options to CallGraph::Dumper when dumping the call graph.

$graph->add_files($file1, $file2, ...)

Reads and parses the given files. $file1, etc. can be either filenames or filehandles.

$graph->add_lines(\@lines)

Parses the array reference, which is expected to be an array of program lines. You can use this if you have already slurped your program into an array.

$graph->add_call($from, $to)

Add a call (a link) to the graph. $from and $to must be subroutine names. The nodes are created automatically (by calling new_sub) if needed, with type 'external' (meaning that they haven't been defined explicitly yet).

my $sub = $graph->get_sub($sub_name)

Returns the CallGraph::Node object for the subroutine named $sub_name. Note that there can be only one subroutine with a given name in a call graph.

my @subs = $graph->subs

Returns the list of all the subroutines contained in the graph, as CallGraph::Node objects.

my $root = $graph->root;
$graph->root($new_root);

Get or set the root of the call graph. $root is a CallGraph::Node object; $new_root can be either an object or a subroutine name.

my $sub = $graph->new_sub(name => $sub_name, [type => $type])

Create and add a new subroutine (a CallGraph::Node object) to the graph. There can only be one subroutine with a given name; if new_sub is called with a name that has been used before, it returnes the previously existing object (note that the type of an existing object can be changed by this call).

If the type is not specified when a subroutine is first created, it defaults to 'external'.

my $dump = $graph->dump(option => value, ...)

Dump the call graph into a string representation. The options are passed to CallGraph::Dumper. The root of the graph must be defined for dump to work.

VERSION

0.55

SEE ALSO

CallGraph::Node, CallGraph::Dumper, CallGraph::Lang::Fortran

AUTHOR

Ivan Tubert <itub@cpan.org>

COPYRIGHT

Copyright (c) 2004 Ivan Tubert. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.