CallGraph::Node - represent a subroutine as a node in a call graph


CallGraph documentation Contained in the CallGraph distribution.

Index


Code Index:

NAME

Top

CallGraph::Node - represent a subroutine as a node in a call graph

SYNOPSIS

Top

    my $sub1 = CallGraph::Node->new(name => 'main', type => 'internal');
    my $sub2 = CallGraph::Node->new(name => 'mysub', type => 'external');
    $sub1->add_call($sub2);
    print $sub1->dump;
    my @calls = $sub1->calls;
    my @callers = $sub2->callers;
    print $sub1->name; # prints 'main'
    print $sub1->type; # prints 'internal'

DESCRIPTION

Top

This module creates a node within a "call graph" for a program. A node corresponds to a subroutine (or function, method, or whatever it's called), and it has the properties 'name' and 'type'. Subroutines are linked to one another by 'calls'.

METHODS

Top

my $sub = CallGraph::Node->new(option => value, ...)

Creates a new node. The available options are 'name' and 'type'. These properties really don't mean anything as far as CallGraph::Node is concerned. However, CallGraph expects the name to be unique within a graph, and uses the values 'internal' or 'external'.

$sub->add_call($sub2)

Add a link implying that $sub calls $sub2.

my @calls = $sub->calls

Return a list of calls made by $sub. The items in the list are CallGraph::Node items themselves.

my @callers = $sub->callers

Return a list of calls received by $sub. The items in the list are CallGraph::Node items themselves.

my $name = $sub->name;
$sub->name($new_name);

Get or set the name of the subroutine.

my $type = $sub->type;
$sub->type($new_type);

Get or set the type of the subroutine.

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

Dump the call graph, starting from $sub, into a string representation. The options are passed to CallGraph::Dumper.

VERSION

Top

0.55

SEE ALSO

Top

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

AUTHOR

Top

Ivan Tubert <itub@cpan.org>

COPYRIGHT

Top


CallGraph documentation Contained in the CallGraph distribution.
package CallGraph::Node;

$VERSION = '0.55';

use strict;
use warnings;
use CallGraph::Dumper;

sub new {
    my ($class, %opts) = @_;
    my $self = bless {
        %opts,
    }, ref $class || $class;
    $self;
}

sub add_call {
    my ($self, $sub) = @_;
    $self->_add_call($sub);
    $sub->_add_caller($self);
}

sub _add_call {
    my ($self, $sub) = @_;
    $self->{children}{$sub->name} = $sub;
}

sub _add_caller {
    my ($self, $sub) = @_;
    $self->{parents}{$sub->name} = $sub;
}

sub calls {
    my ($self) = @_;
    @{$self->{children}}{sort keys %{$self->{children}}};
}

sub callers {
    my ($self) = @_;
    @{$self->{parents}}{sort keys %{$self->{parents}}};
}

sub name {
    my ($self) = shift;
    if (@_) {
        ($self->{name}) = @_;
        $self;
    } else {
        $self->{name};
    }
}

sub type {
    my ($self) = shift;
    if (@_) {
        ($self->{type}) = @_;
        $self;
    } else {
        $self->{type};
    }
}

sub dump {
    my ($self, %opts) = @_;
    CallGraph::Dumper->new(%opts, root => $self)->dump;
}


1;